这个geo_shape elasticsearch查询有什么问题?

时间:2014-04-06 13:41:28

标签: elasticsearch

我正在尝试对具有位置的文档执行geo_shape查询(请参阅下面的示例文档),并且当尝试通过Chrome Addon" Sense"使用ElasticSearch执行它时,我得到NullPointerException。确切的结果是这个问题的底部......

任何有关获得正确的geo_shape查询的帮助都非常感谢。

查询抛出异常

{
   "query": {         
     "geo_shape": {
        "location": {
           "shape": {
              "type": "polygon", 
              "coordinates": [[-87.6363976, 41.8772528], [-87.6363976, 41.8902152],[-87.62131840000001, 41.8902152], [-87.62131840000001, 41.8772528], [-87.6363976, 41.8772528]]
           }
        }

      }

    }
 }

示例文档

{
    "id": 1483,
    "name": "Independance Day Movie day",
    "location": [
              41.9527,
              -87.725
           ]
}

来自Sense的确切结果

{
"error": "SearchPhaseExecutionException[Failed to execute phase [query_fetch], all shards failed; shardFailures {[qFv-xNqxSI2oATfvmnVo8w][dev][0]: SearchParseException[[dev][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n   \"query\": { \n       \"geo_shape\": {\n          \"location\": {\n             \"shape\": {\n                \"type\": \"polygon\", \n                \"coordinates\": [[-87.6363976, 41.8772528], [-87.6363976, 41.8902152],[-87.62131840000001, 41.8902152], [-87.62131840000001, 41.8772528], [-87.6363976, 41.8772528]]\n             }\n          }\n          \n       \n       }\n        \n   \n   }\n}\n]]]; nested: NullPointerException; }]",
"status": 400
}

1 个答案:

答案 0 :(得分:4)

这里有两件事不对。首先,您的location字段应该是一个包含typecoordinates字段的对象,我希望该字段应为:

"location": {
  "type" : "point",
  "coordinates" : [ 41.9527, -87.725 ]
}

请注意,使用数组格式,您将它们指定为(X,Y)或(经度,纬度)。

其次,您的查询非常接近正确,但它需要polygon的额外级别的数组:

{
  "query": {         
    "geo_shape": {
      "location": {
        "shape": {
          "type": "polygon", 
          "coordinates": [
              [[-87.6363976, 41.8772528], [-87.6363976, 41.8902152],
               [-87.62131840000001, 41.8902152], [-87.62131840000001, 41.8772528],
               [-87.6363976, 41.8772528]]
          ]
        }
      }
    }
  }
}

额外嵌套层的目的是允许为像孔这样的东西指定内部多边形。此外,您的多边形中的值将包含您要搜索的点,并且其中一个值的X(Y)值相反。

如果您还没有这样做,那么您还需要设置GeoShape映射以使其正常工作(其中TYPE_NAME将是特定的index' s type,例如curl -XGET localhost:9200/index/type):

{
  "mappings" : {
    "TYPE_NAME" : {
      "properties" : {
        "id" : { "type" : "long" },
        "name" : { "type" : "string" },
        "location" : { "type" : "geo_shape" }
      }
    }
  }
}