Steve020
Steve020

Reputation: 134

How to Handle Elastic Search rage query that has a negative value such as a longitude point

I have a index with values of customers I need to be able to filter the records between ranges for longitude values on a map. The filter for latitude works. I was wondering if it wasn't filtering longitude because of negative values.

After some research I found out that I could try using an escape key because '-' could be interpreted wrong. Neither with or without the escape characters worked.

I have no clue what else I can try. Any advice would be greatly appreciated.

Example :

enter image description here

Upvotes: 0

Views: 1311

Answers (1)

Amit
Amit

Reputation: 32386

Not sure the data-type of your longitude field, as geo-point data type doesn't support the range queries, and if you use the normal integer than it works, as shown below.

Index sample documents with default integer mapping

put my-idx-number-range/_doc/4
{
  "longitutde" : -10
}
put my-idx-number-range/_doc/4
{
  "longitutde" : 20
}
put my-idx-number-range/_doc/4
{
  "longitutde" : 10
}
put my-idx-number-range/_doc/4
{
  "longitutde" : 50
}

Search query with ranges

POST my-idx-number-range/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "longitutde": {
              "gte": -73,
              "lte": 20
            }
          }
        }
      ]
    }
  }
}

And search result

 "hits" : [
      {
        "_index" : "my-idx-number-range",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "longitutde" : -20
        }
      },
      {
        "_index" : "my-idx-number-range",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "longitutde" : 20
        }
      },
      {
        "_index" : "my-idx-number-range",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "longitutde" : 10
        }
      }
    ]

Upvotes: 2

Related Questions