【问题标题】:Why am I not able to query by geolocation in elastic search?为什么我无法在弹性搜索中按地理位置进行查询?
【发布时间】:2014-12-13 07:53:14
【问题描述】:

所以我正在试验 ElasticSearch,这就是我所做的。

1) 在我的索引上添加了一个名为“test”的 geo_point 映射

curl -XPOST localhost:9200/test -d '{
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false },
            "properties" : {
                "location" : { "type" : "geo_point", "index" : "not_analyzed" }
            }
        }
    }
}'

2) 为测试中的文档建立索引:

curl -XPUT 'localhost:9200/test/type1/1?pretty' -d '{
 "location" : {
                "lat" : 74,           
                "lon" : 90            
              }                      
}'  

3) 然后通过地理位置过滤器编写一个查询,如下所示:

curl -XPOST 'localhost:9200/test2/_search?pretty' -d '{
    "filtered" : {
        "query" : {
            "match_all" : {}
},
        "filter" : {
            "geo_distance" : {
                "distance" : "200km",
                "location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}'

为此我得到:

"error" : "SearchPhaseExecutionException[未能执行阶段 [查询],所有分片失败;分片失败 {[QpGEHtdcReeYmt8X2tG26g][test2][0]: RemoteTransportException[[Jester][inet[/10.58.91.21:9301]][search/phase/query]]; 嵌套:SearchParseException[[test2][0]: from[-1],size[-1]: Parse 失败[无法解析源[na]]];嵌套: ElasticsearchParseException [无法从 org.elasticsearch.common.bytes.ChannelBufferBytesReference@60d8bc76];

【问题讨论】:

  • 你能在没有地理信息的情况下运行简单的查询吗?
  • 是的,前两个成功了。但第三个失败了。

标签: elasticsearch geolocation


【解决方案1】:

首先,在您的查询(即第三段代码)中,localhost:9200/test2/_search?pretty 应该是 localhost:9200/test/_search?pretty,即您没有查询正确的索引。

那么您的查询只是缺少query 关键字(即filtered 应包含在query 中),它应该如下所示:

curl -XPOST 'localhost:9200/test/_search?pretty' -d '{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "geo_distance": {
                    "distance": "200km",
                    "location": {
                        "lat": 40,
                        "lon": -70
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    相关资源
    最近更新 更多