弹性搜索地理空间搜索实施 [英] Elastic Search Geo Spatial search implementation

查看:60
本文介绍了弹性搜索地理空间搜索实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解弹性搜索在内部如何支持地理空间搜索.

I am trying to understand how elastic search supports Geo Spatial search internally.

对于基本搜索,它使用倒排索引;但是它如何与其他搜索条件(例如在特定半径内搜索特定文本)结合在一起.

For the basic search, it uses the inverted index; but how does it combine with the additional search criteria like searching for a particular text within a certain radius.

我想了解如何存储和查询索引以支持这些查询的内部信息

I would like to understand the internals of how the index would be stored and queried to support these queries

推荐答案

文本&地理位置查询功能彼此独立.让我们举一个具体的例子:

Text & geo queries function separately of one another. Let's take a concrete example:

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"
  ]
}

然后将匹配一个文本字段,使用 geo_distance 过滤器:

One would then match a text field, use a geo_distance filter:

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 可以将位置提升到更接近给定的起点.

Since the geo_distance query only assigns a true/false value (--> score=1; only checking if the location is within a given radius), one may want to apply a gauss function_score to boost the locations closer to a given origin.

最后,通过使用 _geo_distance 排序可以覆盖这些分数,从而仅按接近度排序(当然,保持 match 查询不变):

Finally, these scores are overridable by using a _geo_distance sort whereby you'd order by the proximity only (while of course keeping the match query intact):

...
  "query: {...},
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 40.7388,
          "lon": -73.9982
        },
        "order": "asc"
      }
    }
  ]
}

这篇关于弹性搜索地理空间搜索实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆