Geo_distance query always returns results, regardless of distance, latitude, or longitude

I've been trying out the geo_distance query on the latest version of Elasticsearch (6.6), but it seems I always get hits/results back. If I follow the example here and change either the latitude/longitude to something way off with a distance of 1 km, I'm still getting hits back.

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html

For example, the following query is still returning a result:

GET http://localhost:9200/my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "1km",
                    "pin.location" : [30, 0]
                }
            }
        }
    }
}

Is there something obvious I'm missing here??? I've attached the full Elasticsearch build information below

{
  "name" : "PrZXr5l",
  "cluster_name" : "elasticsearch_luke",
  "cluster_uuid" : "VJKApkuYTx6KIfIx3O23Ig",
  "version" : {
    "number" : "6.6.1",
    "build_flavor" : "oss",
    "build_type" : "tar",
    "build_hash" : "1fd8f69",
    "build_date" : "2019-02-13T17:10:04.160291Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

What is a typical result you are getting you should not get back?

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

David,

Thanks for the fast response! The reproduction script is in your documentation in the link in my previous post... I can copy/paste it here if you'd like though for completeness later on?

I expect to not get any results back if I'm specifying a distance with a latitude/longitude outside of the given data set. As I mentioned in my previous post, if I'm at a longitude/latitude of 30/0 with a 1 kilometer distance, I definitely shouldn't be returning a point back with a latitude of 40.12 and longitude -71.34... that's significantly further away than 1 km...

Copy/pasting Elastic's documentation here for future reference/readers

PUT /my_locations
{
    "mappings": {
        "_doc": {
            "properties": {
                "pin": {
                    "properties": {
                        "location": {
                            "type": "geo_point"
                        }
                    }
                }
            }
        }
    }
}

PUT /my_locations/_doc/1
{
    "pin" : {
        "location" : {
            "lat" : 40.12,
            "lon" : -71.34
        }
    }
}

GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "1km",
                    "pin.location" : [30, 0]
                }
            }
        }
    }
}

This is returning a hit when the data point being returned is definitely not within 1 kilometer of longitude 30, latitude 0

I just tried this script (in 7.0.0-beta1 though but I'm going to try in 6.6):

DELETE /my_locations
PUT /my_locations
{
  "mappings": {
    "properties": {
      "pin": {
        "properties": {
          "location": {
            "type": "geo_point"
          }
        }
      }
    }
  }
}

PUT /my_locations/_doc/1
{
    "pin" : {
        "location" : {
            "lat" : 40.12,
            "lon" : -71.34
        }
    }
}

GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "1km",
                    "pin.location" : [30, 0]
                }
            }
        }
    }
}

And this gives:

{
  "took" : 104,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

Same in 6.6.1. No hits returned.

Are you sure that:

  • You are running exactly this script?
  • You are using the latest 6.6.1 version?

David,

Thank you for the super fast responses! If I try your following script below, I get an error:

PUT /my_locations
{
  "mappings": {
    "properties": {
      "pin": {
        "properties": {
          "location": {
            "type": "geo_point"
          }
        }
      }
    }
  }
}

I noticed yours doesn't have a "_doc" key in the payload for the mappings.. is this a caveat of 7.0.0-beta1 for you? If I try this on 6.6.1 it returns a 400 with the error message:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "Root mapping definition has unsupported parameters:  [pin : {properties={location={type=geo_point}}}]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [properties]: Root mapping definition has unsupported parameters:  [pin : {properties={location={type=geo_point}}}]",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "Root mapping definition has unsupported parameters:  [pin : {properties={location={type=geo_point}}}]"
    }
  },
  "status": 400
}

Is there something else off here? I've included the build version higher in this thread. Is there a known issue with that release version?

Yeah. It's because I'm using 7.0 and not 6.x.

You can add _doc in the mapping between mappings and properties and that will work on 6.x

Okay, then that's verbatim the script I have above and I'm still getting back a response:

Request
GET http://localhost:9200/my_locations/_search

{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "1km",
                    "pin.location" : [30, 0]
                }
            }
        }
    }
}

Response

{
    "took": 79,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
            "_index": "my_locations",
            "_type": "_doc",
            "_id": "1",
            "_score": 1,
            "_source": {
                "pin": {
                    "location": {
                        "lat": 40.12,
                        "lon": -71.34
                    }
                }
            }
            }
        ]
    }
}

Are there any additional tweaks/plugins/things I have to have installed in my Elasticsearch instance? I'm trying to figure out what we're doing differently here.

I suspect that you put a blank line in Kibana console between:

GET http://localhost:9200/my_locations/_search

and

{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "1km",
                    "pin.location" : [30, 0]
                }
            }
        }
    }
}

Did you?

Or that you are not running this exact script in Kibana dev console.

Figured it out! This is an issue with the REST client I'm using (Paw from https://paw.cloud). Apparently, it's stripping out the body on GET requests. When I tried the above with Postman, it sent the payload and got the response correctly. Thanks for the help @dadoonet!!

For other people that hit this problem with Paw, here's how you enable the payload on any request:

You should have tell me from the start.
Actually Kibana transforms GET with body to POST requests as many browsers if not all do not support GET with a body.

Good to know. Thank you @dadoonet

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.