Wrong elasticsearch geo location query

155 views Asked by At

Having a mapping as:

"mappings": {
     "gyms": {
        "properties": {
           "location": {
              "type": "geo_point"
           },
           "name": {
              "type": "string"
           }
        }
     }
  }

I'm trying to retrieve all gyms located at 10km from a given lat,long. I'm using this query:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": 10,
          "distance_unit": "km",
          "location": {
            "lat": 41.3819756,
            "lon": 2.17
          }
        }
      }
    }
  }
}

But something is wrong because I have some gyms near from the lat lon given and elasticsearch response has no documents. What could be happening?

UPDATE The query:

"query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "10km",
          "location": {
            "lat":   41.3819756,
            "lon": 2.17
          }
        }
      }
    }
  }

Works as expected!

As an example of documents that should be appear:

{
        "_index": "g4l",
        "_type": "gyms",
        "_score": 1,
        "_source": {
           "gymName": "sds",
           "location": {
              "lat": 41.3994375,
              "lon": 2.16152349999993
           }
        }
     },
     {
        "_index": "g4l",
        "_type": "gyms",
        "_score": 1,
        "_source": {
           "gymName": "sdf",
           "location": {
              "lat": 41.4040134,
              "lon": 2.201350400000024
           }
        }
     }
1

There are 1 answers

0
Shreyas Rao B On
  • In query change in filter part from "location" to "gyms.location"
  • this should work for you.