查询geo_point上的完全匹配

时间:2018-06-06 05:47:54

标签: java elasticsearch

我的索引中有一个字段,位置,数据类型为geo_point。现在我想运行一个查询,它给出了所有指定纬度和经度的文档。

我正在尝试运行这样的查询 -

GET garage/_search
{
    "query":{
      "bool" : {
        "must" : [
          {
            "match" : {
              "garage_type" : {
                "query" : "2 Wheeler",
                "operator" : "OR",
                "prefix_length" : 0,
                "max_expansions" : 50,
                "fuzzy_transpositions" : true,
                "lenient" : false,
                "zero_terms_query" : "NONE",
                "boost" : 1.0
              }
            }
          }
        ],
        "filter" : [
          {
            "geo_shape" : {
              "location" : {
                "shape" : {
                  "type" : "point",
                  "coordinates" : [
                    73.56,
                    93.4
                  ]
                },
                "relation" : "intersects"
              },
              "ignore_unmapped" : false,
              "boost" : 1.0
            }
          }
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
      }
    }
}

但运行上面的查询会给我以下错误 -

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "Field [location] is not of type [geo_shape] but of type [geo_point]",
        "index_uuid": "R0rAusMyT4uHVnSedsPWdQ",
        "index": "well"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "garage",
        "node": "TKNxQLqfQl-cadDJkx9Hhw",
        "reason": {
          "type": "query_shard_exception",
          "reason": "Field [location] is not of type [geo_shape] but of type [geo_point]",
          "index_uuid": "R0rAusMyT4uHVnSedsPWdQ",
          "index": "well"
        }
      }
    ]
  },
  "status": 400
}

哪个有道理。但我无法找到任何替代方案。甚至可以像这样查询geo_point字段吗?

1 个答案:

答案 0 :(得分:0)

不,不是。您正在geo_point字段上使用geo_shape查询。 您可以做的是地理点距离查询。它就像这样

GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "pin.location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}

您可以更改距离以适合您的使用案例。 您可以找到更多here