我想了解弹性搜索如何在内部支持地理空间搜索。
对于基本搜索,它使用了倒置索引;但它如何与额外的搜索条件相结合,比如搜索一定半径内的特定文本。
我想了解内部是如何存储和查询索引以支持这些查询的。
文本&geo查询的功能是相互独立的。我们举个具体的例子。
PUT restaurants
{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
},
"menu": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
POST restaurants/_doc
{
"name": "rest1",
"location": {
"lat": 40.739812,
"lon": -74.006201
},
"menu": [
"european",
"french",
"pizza"
]
}
POST restaurants/_doc
{
"name": "rest2",
"location": {
"lat": 40.7403963,
"lon": -73.9950026
},
"menu": [
"pizza",
"kebab"
]
}
我们可以这样做 match
文本字段,使用 geo_distance
过滤器。
GET restaurants/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"menu": "pizza"
}
},
{
"geo_distance": {
"distance": "0.5mi",
"location": {
"lat": 40.7388,
"lon": -73.9982
}
}
},
{
"function_score": {
"query": {
"match_all": {}
},
"boost_mode": "avg",
"functions": [
{
"gauss": {
"location": {
"origin": {
"lat": 40.7388,
"lon": -73.9982
},
"scale": "0.5mi"
}
}
}
]
}
}
]
}
}
}
由于... geo_distance
查询只分配一个truefalse值(--> score=1; 只检查位置是否在给定的半径内),我们可能想应用一个高斯的 function_score
来提高更接近给定原点的位置。
最后,这些分数是可以通过使用 _geo_distance
排序,因此您只需按近似度排序(当然,您可以保留 match
查询完整)。)
...
"query: {...},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 40.7388,
"lon": -73.9982
},
"order": "asc"
}
}
]
}