使用elasticsearch进行地理查询

时间:2016-03-04 11:42:21

标签: elasticsearch

我已经创建了索引并将其作为示例教程的索引,在这里......

https://www.elastic.co/guide/en/elasticsearch/reference/2.0/geo-point.html

具体写作如下:

curl -PUT 'localhost:9200/my_index?pretty' -d '
{
  "mappings": {
    "my_type": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}'

我还添加了两点作为数据

curl -PUT 'localhost:9200/my_index/my_type/1?pretty' -d'
{
  "text": "first geo-point",
  "location": { 
    "lat": 41.12,
    "lon": -71.34
  }
}'

curl -PUT 'localhost:9200/my_index/my_type/1?pretty' -d'
{
  "text": "second geo-point",
  "location": { 
    "lat": 41.13,
    "lon": -71.35
  }
}'

页面上的示例地理边界框查询起作用(即):

curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        "top_left": {
          "lat": 42,
          "lon": -72
        },
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}'

但是此页面中的示例(https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-geo-bounding-box-query.html)无效:

我尝试过的内容如下:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
    "bool" : {
        "must" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_bounding_box" : {
                "my_type.location" : {
                    "top_left" : {
                        "lat" : 42,
                        "lon" : -72
                    },
                    "bottom_right" : {
                        "lat" : 40,
                        "lon" : -74
                    }
                }
            }
        }
    }
}'

我得到的错误如下:

"error" : {
    "root_cause" : [ {
      "type" : "search_parse_exception",
      "reason" : "failed to parse search source. unknown search element [bool]",
      "line" : 3,
      "col" : 5
    } ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [ {
      "shard" : 0,
      "index" : "my_index",
      "node" : "0qfvkynhTRyjHFRurBLJeQ",
      "reason" : {
        "type" : "search_parse_exception",
        "reason" : "failed to parse search source. unknown search element [bool]",
        "line" : 3,
        "col" : 5
      }
    } ]
  },
  "status" : 400
}

我希望它只是一个简单的错误,所以想知道我做错了什么?

1 个答案:

答案 0 :(得分:6)

您需要指定整个事情是一个查询:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
   "query": {
      "bool" : {
          "must" : {
              "match_all" : {}
          },
          "filter" : {
              "geo_bounding_box" : {
                  "my_type.location" : {
                        "top_left" : {
                          "lat" : 42,
                          "lon" : -72
                      },
                      "bottom_right" : {
                          "lat" : 40,
                          "lon" : -74
                      }
                  }
              }
          }
      }
   }
}'

但据我所知,使用bool with must和filter是旧的做事方式。在以前的版本中,地理查询被认为是"过滤器",因此您必须首先运行match_all查询以返回所有结果,然后使用地理边界框进行过滤。在Elasticssearch 2.0+中,过滤器和查询之间没有分离 - 一切都是查询。因此,您可以直接运行地理查询:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        "top_left": {
          "lat": 42,
          "lon": -72
        },
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}'