Elasticsearch 2.0 geo distance search not working with location array

I am facing an issue with geo_distance query in case of multiple locations within a single document. While filtering, it is always considering the first one in the list and ignoring the rest of the locations within the list. So if the first location does not match, the document is not appearing in the search result. Here is my example:

ES version: 2.0

Index:

 {
    "locations" : 
     {
        "type" : "geo_point"
     }
 }

Document:

{"locations" : [{"lat": 12.9306989,"lon": 77.6870448}, {"lat": 12.9133638,"lon": 77.6870448}]}

Search DSL:

{

"query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_distance": {
                    "distance": "1km",
                    "locations": {"lat" : 12.9165699, "lon" : 77.6393489}
                }
            }
        }
    }

}

But the document is not coming in search result even the second location is within 1 km radius of the query location.

I think your example is may be incorrect.

I tried with this:

DELETE test
PUT test
{
  "mappings": {
    "doc": {
      "properties": {
        "locations": {
          "type": "geo_point"
        }
      }
    }
  }
}
PUT test/doc/1
{
  "locations": [
    {
      "lat": 13.9306989,
      "lon": 78.6870448
    },
    {
      "lat": 12.9133638,
      "lon": 77.6870448
    }
  ]
}
GET test/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "distance": "1km",
          "locations": {
            "lat": 12.9133638,
            "lon": 77.6870448
          }
        }
      }
    }
  }
}
GET test/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "distance": "1km",
          "locations": {
            "lat": 13.9306989,
            "lon": 78.6870448
          }
        }
      }
    }
  }
}

This is giving expected results:

# GET test/_search
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "locations": [
            {
              "lat": 13.9306989,
              "lon": 78.6870448
            },
            {
              "lat": 12.9133638,
              "lon": 77.6870448
            }
          ]
        }
      }
    ]
  }
}

# GET test/_search
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "locations": [
            {
              "lat": 13.9306989,
              "lon": 78.6870448
            },
            {
              "lat": 12.9133638,
              "lon": 77.6870448
            }
          ]
        }
      }
    ]
  }
}

Can you please try to execute using CURL command? I am getting empty results for the same.

curl -XGET 'localhost:9200/test/_search?pretty' -H 'Content-Type: application/json' -d'
{

"query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_distance": {
                    "distance": "1km",
                    "locations": {"lat" : 12.9165699, "lon" : 77.6393489}
                }
            }
        }
    }

}'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

May be your point is not within 1km?

One location {"lat": 12.9133638,"lon": 77.6870448} is within 1km from the given location, but another one is more than one.

You longitude starts 77.63 while the example David provided and the data stored starts 77.68, so it is possible your point is not within 1km.

Yes. It was my bad data :grimacing: Sorry for the wrong alarm.

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