PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

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

 October 24, 2022     elasticsearch, elasticsearch-query     No comments   

Issue

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.

  • note - there are records between that range because when I grab the whole index I can see the records with longitude values between the range.

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

Example :

enter image description here


Solution

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
        }
      }
    ]


Answered By - Amit
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing