首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ElasticSearch地理距离过滤器与阵列中的多个位置-可能吗?

ElasticSearch地理距离过滤器与阵列中的多个位置-可能吗?
EN

Stack Overflow用户
提问于 2013-04-20 05:01:31
回答 2查看 12.6K关注 0票数 24

假设我们在ElasticSearch索引中有一堆文档。每个文档在数组中都有多个位置,如下所示:

代码语言:javascript
复制
{
  "name": "foobar",
  "locations": [
    {
      "lat": 40.708519,
      "lon": -74.003212
    },
    {
      "lat": 39.752609,
      "lon": -104.998100
    },
    {
      "lat": 51.506321,
      "lon": -0.127140
    }
  ]
}

根据ElasticSearch reference guide的说法

geo_distance过滤器可以处理每个文档的多个位置/点。一旦单个位置/点与筛选器匹配,该文档将包含在筛选器中。

那么,有没有可能创建一个地理距离过滤器来检查数组中的所有位置?

不幸的是,这似乎不起作用:

代码语言:javascript
复制
{
  "filter": {
    "geo_distance": {
      "distance": "100 km",
      "locations": "40, -105"
    }
  }
}

抛出"QueryParsingException[[myIndex] failed to find geo_point field [locations]“,因为locations不是单个geo_point,而是一个geo_point数组。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-20 08:17:09

您是否为文档指定了geo_point mapping

代码语言:javascript
复制
curl -XDELETE 'http://localhost:9200/twitter/'
curl -XPUT 'http://localhost:9200/twitter/'

curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d '
{
    "tweet" : {
        "properties" : {
            "locations" : {"type" : "geo_point"}
        }
    }
}'

curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '
{ 
    "user": "kimchy", 
    "postDate": "2009-11-15T13:12:00", 
    "message": "Trying out Elastic Search, so far so good?",
    "locations" : [{
        "lat" : 50.00,
        "lon" : 10.00
    },
    {
        "lat" : 40.00,
        "lon" : 9.00
    }]
}'

curl -XPUT 'http://localhost:9200/twitter/tweet/2' -d '
{ 
    "user": "kimchy", 
    "postDate": "2009-11-15T13:12:00", 
    "message": "Trying out Elastic Search, so far so good?",
    "locations" : [{
        "lat" : 30.00,
        "lon" : 8.00
    },
    {
        "lat" : 20.00,
        "lon" : 7.00
    }]
}'

curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
    "query": {
        "filtered" : {
            "query" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "20km",
                    "tweet.locations" : {
                        "lat" : 40.00,
                        "lon" : 9.00
                    }
                }
            }
        }
    }
}'
票数 31
EN

Stack Overflow用户

发布于 2017-02-17 15:47:44

对于Elasticsearch版本5.1,对于上面的索引,查询将是这样的,

代码语言:javascript
复制
curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '
{
    "query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_distance": {
                    "distance": "200km",
                    "locations": {
                        "lat": 40.00,
                        "lon": 9.00
                    }
                }
            }
        }
    }
}'
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16113439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档