[英]Elasticsearch - Sort By Distance Not Working?
我有一個索引,其中記錄以下列格式存儲:
"_source": {
"name": "ACME Pallets",
"about": null,
"slug": "acme-pallets",
"serviceAreas": [
{
"admin1": "usa",
"admin2": null,
"admin3": null,
"admin4": null,
"countryCode": "US",
"googlePlaceId": null,
"locality": null,
"selectedLevel": "admin1"
}
],
"id": "fadsflsjdfkk3234234",
"addresses": [
{
"address1": "4342 Dietrich Rd",
"address2": null,
"city": "San Antonio",
"countryCode": "US",
"latitude": 29.44122,
"longitude": -98.34404,
"primary": true,
"name": "office",
"postal": "78219",
"province": "TX",
"location": {
"lat": 29.44156,
"lon": -98.37704
}
}
]
}
我正在嘗試從此索引返回結果,其中記錄按到我傳入的搜索點的距離排序。傳入的排序配置如下所示:
_geo_distance: {
'addresses.location': { lat: 31.75917, lon: -106.48749 },
order: 'asc',
unit: 'mi',
mode: 'min'
}
我收到的結果沒有按照距離排序。 如果我手動 plot 找出 map 上的各個位置並傳入搜索 pin,我可以看到排序是亂序的。
如果我將排序配置傳遞給我的搜索以按字母順序排序或按相關性排序(又名_score
),則返回的排序是正確的。
有誰知道為什么 ES 在按距離排序時可能會錯誤地返回我的結果?
addresses
是我索引中的一個數組。 addresses
中的每個 object 都有一個名為location
類型的屬性geo_point 。
從我讀過的所有文檔中,將'addresses.location': { lat: 31.75917, lon: -106.48749 }
到搜索中應該有效,但事實並非如此。 ES 應該足夠聰明,可以在每個 object 中找到位置地理點,並以此作為計算距離時的參考。 如果addresses
數組中有多個 object,那么 ES 默認應該獲取addresses
中所有對象的中心點,並以此計算到搜索點的距離。
在我的例子中,我沒有任何addresses
超過 object 的數據。我最終在索引構建期間在addresses
屬性之外創建了一個location
geo_point屬性,然后傳入location: { lat: 31.75917, lon: -106.48749 }
進行搜索。 這使得 ES 能夠正確地根據距離對結果進行排序。
添加location
屬性后我的新索引是什么樣子的:
"_source": {
"name": "ACME Pallets",
"about": null,
"slug": "acme-pallets",
"serviceAreas": [
{
"admin1": "usa",
"admin2": null,
"admin3": null,
"admin4": null,
"countryCode": "US",
"googlePlaceId": null,
"locality": null,
"selectedLevel": "admin1"
}
],
"id": "fadsflsjdfkk3234234",
"addresses": [
{
"address1": "4342 Dietrich Rd",
"address2": null,
"city": "San Antonio",
"countryCode": "US",
"latitude": 29.44122,
"longitude": -98.34404,
"primary": true,
"name": "office",
"postal": "78219",
"province": "TX",
"location": {
"lat": 29.44156,
"lon": -98.37704
}
}
]
"location": {
"lat": 29.44156,
"lon": -98.37704
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.