gpt4 book ai didi

Elasticsearch 地理距离查询

转载 作者:行者123 更新时间:2023-12-02 22:20:25 29 4
gpt4 key购买 nike

我有一个地点列表,在 geo_point 的正确映射中,这些地点的纬度和经度与其相关联

我还有一个查询成功返回基于地理距离的结果,如下所示:

{
"filter": {
"geo_distance": {
"distance": "30mi",
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
}
}
},
"sort": {
"_geo_distance": {
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
},
"order": "asc",
"unit": "mi",
"mode": "min"
}
},
"from": 0,
"size": 500
}

因此,目前这会返回所提供纬度和经度 30 英里范围内的结果。这很好用。

我正在努力进行下一步,我希望有人能为我指明正确的方向。

每个地方都有一个名为distance 的字段,它是一个整数。这是一个地方愿意到达客户的最大距离。因此,如果距离为 20(英里)但经纬度计算结果超过 20 英里,则应将其排除在结果之外。

返回的结果是这样的:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "places",
"_id": "AUtvK2OILrMWSKLclj9Z",
"_score": null,
"_source": {
"id": "1",
"name": "Chubby Company",
"summary": "",
"content": "",
"locations": [
{
"lat": 51.8200763,
"lon": 0.5264076
}
],
"address": [
{
"addr1": "xxxx",
"addr2": "",
"town": "MyTown",
"county": "Essex",
"postcode": "XX1 2XX",
"tel1": "01111 111111",
"tel2": "",
"email": null
}
],
"website": "",
"media": {
"logo": "",
"image": "",
"video": ""
},
"product_ids": [
"1",
"3",
"2"
],
"distance": "20"
},
"sort": [
0.031774582056958885
]
}
]
}
}

sort 对象是以英里为单位的距离,所以上面的结果是距客户端 0.03 英里。

我正在尝试利用它来检查使用结果的记录以将其从结果中排除,但这是我失败的地方。

我尝试了不同的组合:

"script": {
"script": "doc['distance'].value < doc['sort'].value"
}

结合查询看起来像这样:

{
"filter": {
"geo_distance": {
"distance": "30mi",
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
}
}
},
"sort": {
"_geo_distance": {
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
},
"order": "asc",
"unit": "mi",
"mode": "min"
}
},
"filtered": {
"filter": {
"script": {
"script": "doc['distance'].value < doc['sort'].value"
}
}
},
"from": 0,
"size": 500
}

但是我得到一个错误:

SearchPhaseExecutionException[Failed to execute phase [query], all shards failed ... Parse Failure [No parser for element [filtered]

任何建议都会很棒。

更新

尝试这个也失败了:

{
"filter": {
"geo_distance": {
"distance": "30mi",
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
}
},
"script": {
"script": "_source.distance < sort.value"
}
},
"sort": {
"_geo_distance": {
"companies.locations": {
"lat": "51.8801595",
"lon": "0.577141"
},
"order": "asc",
"unit": "mi",
"mode": "min"
}
},
"from": 0,
"size": 500
}

nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"script\"]; }]","status":400}

最佳答案

我有一个类似的问题,我想为存储在数据中的每条记录设置一个条件距离(对我来说是 searchRadius)。我最终在 Java 中使用了 AndFilterBuilder 和 ScriptFilterBuilder 类,因此 AndFilterBuilder 有一个包含 GeoDistanceFilterBuilder 和 ScriptFilterBuilder 的数组。

{
"from": 0,
"size": 20,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": {
"filters": [
{
"geo_distance": {
"location": [
-104.99230194091797,
39.74000930786133
],
"distance": "3000mi"
}
},
{
"script": {
"script": "doc['location'].arcDistanceInMiles(39.74000930786133, -104.99230194091797) < doc['searchRadius'].value"
}
}
]
}
}
}
},
"fields": "_source",
"script_fields": {
"distance": {
"script": "doc['location'].distance(39.74000930786133, -104.99230194091797)"
}
},
"sort": [
{
"_geo_distance": {
"location": [
-104.99230194091797,
39.74000930786133
],
"unit": "mi"
}
}
]
}

关于Elasticsearch 地理距离查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28677001/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com