Search code examples
elasticsearchgeolocationkibanageospatial

Elasticsearch geospatial queries returning no hits


I'm using Kibana to look at a geospatial dataset in Elasticsearch for a feature currently under development. There is a index of positions which contains field "loc.coordinates", which is a geo_point, and has as data as such:

loc.coordinates         25.906958000000003, 51.776407000000006

However when I run the following query I get no results:

Query

GET /positions/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "2000km",
                    "loc.coordinates" : {
                        "lat" : 25,
                        "lon" : 51
                    }
                }
            }
        }
    }
}

Response

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 6,
    "successful": 6,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

I'm trying to understand why this is, as there are over 250,000 datapoints in the index, and I'm getting no hits regardless of how big the search area is. When I look in the position index mapping I see the following:

"loc": {
  "type": "nested",
  "properties": {
    "coordinates": {
      "type": "geo_point"
    },
    "type": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
},

I'm new to Elasticsearch and have been making my way through the documentation, but so far I don't see why my geo queries aren't working as expected. What am I doing wrong?


Solution

  • Your loc field is of type nested, so you need to query that field accordingly with a nested query:

    GET /positions/_search
    {
        "query": {
            "bool" : {
                "filter" : {
                    "nested": {
                       "path": "loc",
                       "query": {
                          "geo_distance" : {
                             "distance" : "2000km",
                             "loc.coordinates" : {
                                "lat" : 25,
                                "lon" : 51
                             }
                          }
                       }
                    }
                }
            }
        }
    }