Elasticsearch:地理距离查询错误

时间:2016-01-31 22:01:27

标签: rest elasticsearch location

这是我的映射:

$ curl -XGET http://localhost:9200/test-index/dev/_mapping?pretty
{
  "test-index" : {
    "mappings" : {
      "dev" : {
        "properties" : {
          "Coordinates" : {
            "type" : "double"
          },
          "DisplayName" : {
            "type" : "string"
          },
          "Location" : {
            "type" : "string"
          },
          "RawLocation" : {
            "type" : "string"
          },
          "Reputation" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

我的目标是在坐标上运行地理距离查询。

我使用这种坐标格式:

"location": [ -73.983, 40.719 ]

以下是一些结果:

$ curl -XGET http://localhost:9200/test-index/dev/_search?pretty
...
{
      "_index" : "test-index",
      "_type" : "dev",
      "_id" : "Arindam Nayak",
      "_score" : 1.0,
      "_source":{"RawLocation": "Pune, India", "Reputation": "101", "DisplayName": "Arindam Nayak", "Location": "Pune, Maharashtra 411001, India", "Coordinates": [73.8567437, 18.5204303]}
 }, 
...

我尝试了很多方法来获取一个坐标周围的命中列表,但无济于事:

$ curl -XGET http://localhost:9200/test-index/dev/_search -d '
{           
"filter" : { 
      "geo_distance" : {
       "distance" : "20km",
                    “Coordinates : {
                        "lat" : 40.00,
                        "lon" : 9.00
                    }
                }
            }
        }
'

这是悲伤的结果:

"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[6W3CA5U5TqStabDolHcIng][test-index][0]: SearchParseException[[test-index][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\"filter\" : {\n      \"geo_distance\" : {\n       \"distance\" : \"20km\",\n                    “Coordinates : {\n                        \"lat\" : 40.00,\n                        \"lon\" : 9.00\n                    }\n                }\n            }\n        }\n]]]; nested: QueryParsingException[[test-index] failed to find geo_point field [“Coordinates]]; }{[6W3CA5U5TqStabDolHcIng][test-index][1]: SearchParseException[[test-index][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\"filter\" : {\n      \"geo_distance\" : {\n       \"distance\" : \"20km\",\n                    “Coordinates : {\n                        \"lat\" : 40.00,\n                        \"lon\" : 9.00\n                    }\n                }\n            }\n        }\n]]]; nested: QueryParsingException[[test-index] failed to find geo_point field [“Coordinates]]; }{[6W3CA5U5TqStabDolHcIng][test-index][2]: SearchParseException[[test-index][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\"filter\" : {\n      \"geo_distance\" : {\n       \"distance\" : \"20km\",\n                    “Coordinates : {\n                        \"lat\" : 40.00,\n                        \"lon\" : 9.00\n                    }\n                }\n            }\n        }\n]]]; nested: QueryParsingException[[test-index] failed to find geo_point field [“Coordinates]]; }{[6W3CA5U5TqStabDolHcIng][test-index][3]: SearchParseException[[test-index][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\"filter\" : {\n      \"geo_distance\" : {\n       \"distance\" : \"20km\",\n                    “Coordinates : {\n                        \"lat\" : 40.00,\n                        \"lon\" : 9.00\n                    }\n                }\n            }\n        }\n]]]; nested: QueryParsingException[[test-index] failed to find geo_point field [“Coordinates]]; }{[6W3CA5U5TqStabDolHcIng][test-index][4]: SearchParseException[[test-index][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\"filter\" : {\n      \"geo_distance\" : {\n       \"distance\" : \"20km\",\n                    “Coordinates : {\n                        \"lat\" : 40.00,\n                        \"lon\" : 9.00\n                    }\n                }\n            }\n        }\n]]]; nested: QueryParsingException[[test-index] failed to find geo_point field [“Coordinates]]; }]","status":400}

任何帮助?

1 个答案:

答案 0 :(得分:1)

作为错误统计信息,ES无法找到Coordinates类型的geo_point字段。您需要将Coordinates字段映射为geo_point才能执行任何位置查询。

{
  "test-index": {
    "mappings": {
      "dev": {
        "properties": {
          "Coordinates": {
            "type": "geo_point"  <--- change type from double to geo_point
          },
          "DisplayName": {
            "type": "string"
          },
          "Location": {
            "type": "string"
          },
          "RawLocation": {
            "type": "string"
          },
          "Reputation": {
            "type": "string"
          }
        }
      }
    }
  }
}

由于您使用的是array format,因此您需要将坐标插入[lon, lat]

您必须reindex您的数据。之后,您的查询将起作用。

希望这有帮助!