我正在尝试了解弹性搜索在内部如何支持地理空间搜索。
对于基本搜索,它使用倒排索引;但是它如何与其他搜索条件(例如在特定半径内搜索特定文本)结合在一起。
我想了解如何存储和查询索引以支持这些查询的内部信息
答案 0 :(得分:0)
文本和地理查询彼此独立运行。让我们举一个具体的例子:
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
查询仅分配真/假值(-> score = 1;仅检查位置是否在给定半径内),因此可能要应用高斯function_score
使位置更接近给定的原点。
最后,可以使用_geo_distance
排序来覆盖这些分数,从而仅按接近程度排序(当然,请保持match
查询的完整性):
...
"query: {...},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 40.7388,
"lon": -73.9982
},
"order": "asc"
}
}
]
}