elasticsearch 当不使用_geo_distance排序时,是否有方法返回geo距离?

mzsu5hc0  于 2022-12-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(131)

我需要返回地理位置结果中的计算距离。但不使用排序当前我正在使用排序,但它忽略了现有字段
以下是我疑问:

"query": {
    "bool": {
        "filter": [
            {
                "match": {
                    "field 1": "value"
                }
            },
            {
                "match": {
                    "field2": "A"
                }
            },
            {
                "geo_distance": {
                    "distance": "20km",
                    "location": "34,-2.99"
                }
            }
        ], "should": [
            {
                "exists": {
                    "field": "field3",
                    "boost": 10000
                }
            }
        ]
    }
},
"size": 500,
"sort": [
    {
        "_geo_distance": {
            "location": {
                "lat": 34,
                "lon": 2.99
            },
            "order": "asc",
            "unit": "km",
            "mode": "min",
            "distance_type": "arc",
            "ignore_unmapped": "true"
        }
    }
]

我需要如果字段3存在,它会获得更高的排名

x8diyxa7

x8diyxa71#

如果我没理解错你的问题的话,这个查询应该可以:

"query": {
    "bool": {
        "filter": [
            {
                "match": {
                    "field 1": "value"
                }
            },
            {
                "match": {
                    "field2": "A"
                }
            },
            {
                "geo_distance": {
                    "distance": "20km",
                    "location": "34,-2.99"
                }
            }
        ]
    }
},
"size": 500,
"sort": [
    {
        "_script": {
            "type": "number",
            "script": {
                "source": "doc['field3'].size() > 0 ? 10000 : 0"
            },
            "order": "asc"
        }
    },
    {
        "_geo_distance": {
            "location": {
                "lat": 34,
                "lon": 2.99
            },
            "order": "asc",
            "unit": "km",
            "mode": "min",
            "distance_type": "arc",
            "ignore_unmapped": "true"
        }
    }
]

我正在检查字段是否存在并提高其分数

相关问题