3

My documents have geo_shapes to associate them to an area. If I give ES (1.7) a geo_point I'm wanting it to give me back the documents where the point falls within that area.

I've recreated with the following toy example:-

# 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]
        }
      }
    }
  }
}
Martin Clarke
  • 5,636
  • 7
  • 38
  • 58

1 Answers1

2

Besides the fact that you're missing a comma after "type": "point" in your last query, I do get a single point when POSTing the query to the _search endpoint:

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

Results:

{
  "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]]}}
    } ]
  }
}

When sending a payload you should use POST instead of GET as not all HTTP clients send payload when using GET.

Val
  • 207,596
  • 13
  • 358
  • 360
  • Just for background, I was doing this in sense - where I had a number of regular queries which seemed to work with GET rather than POST; but it seems like the Geo queries are very particular on it. Probably a bug with sense that it doesn't alway send the payload. – Martin Clarke Oct 07 '15 at 08:53
  • 1
    Yes, it's not always clear. As the principle of least surprise goes, I'm always using POST when sending my query in a payload. Even though sending payload via GET is allowed, there is never a good reason for doing so (see [this other question for more info](http://stackoverflow.com/questions/978061/http-get-with-request-body) on this) – Val Oct 07 '15 at 09:04
  • 1
    I also recommend that when using Sense, you uppercase your HTTP methods, i.e. `GET` instead of `get`. – Val Oct 09 '15 at 12:02