意外的Geo_shape查询行为

时间:2015-10-01 14:07:19

标签: elasticsearch

我的文档有geo_shapes将它们关联到一个区域。如果我给ES(1.7)一个geo_point,我希望它能把那些点落在那个区域内的文件给我。

我使用以下玩具示例重新创建: -

# create the index
put location_test
put location_test/_mapping/place
{
  "place": {
    "properties": {
      "message": {"type": "string"},
      "coverage": {"type": "geo_shape"}
    }
  }
}
# check the mapping is correct
get location_test/place/_mapping

# location 1
put location_test/place/1 
{
  "message": "we will be in this box",
  "coverage": {
    "type" : "envelope",
        "coordinates" : [[1, 0], [0, 1] ]
    }
}

# location 2
put location_test/place/2
{
  "message": "we will be outside this box",
  "coverage": {
    "type" : "envelope",
        "coordinates" : [[2, 1], [1, 2] ]
    }
}

# all documents returned - OK 
get location_test/place/_search
{
  "query": { "match_all": {}}
}

# should only get document 1, but get both.
get location_test/place/_search 
{
  "query": {
    "geo_shape": {
      "coverage": {
        "shape": {
          "type": "point"
          "coordinates": [0.1,0.1]
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

除了您在上一次查询中"type": "point"之后丢失逗号这一事实外,在将查询发布到_search端点时,我确实得到了一个点:

curl -XPOST localhost:9200/location_test/place/_search -d '{
  "query": {
    "geo_shape": {
      "coverage": {
        "shape": {
          "type": "point",               <---- comma missing here
          "coordinates": [0.1,0.1]
        }
      }
    }
  }
}'

结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "location_test",
      "_type" : "place",
      "_id" : "1",
      "_score" : 1.0,
      "_source":{"message":"we will be in this box","coverage":{"type":"envelope","coordinates":[[1,0],[0,1]]}}
    } ]
  }
}

发送有效负载时,您应该使用POST而不是GET,因为并非所有HTTP客户端在使用GET时都会发送有效负载。