Search code examples
elasticsearchelasticsearch-pluginelasticsearch-5

Range does not filter my documents properly


I am a kind of confused. I have a category_id in my elastic search document like:

[
 {
"id": "80029",
"categoryId": "43227",
"channelId": "54322",
"channelName": "xxxxx2222",

Now in elastic search when I search based on the range on category id as follows:

  {
 "query": {
   "range": {
    "category_id": {
    "lt": 1000032270000
    }
  }
}
 }

I get nothing back but it is obvious that 1000032270000 is greater than 43227 and I expecting to get the record back. More surprisingly whe I change the 1000032270000 to 9000032270000 I get the result. So what is my problem?how can I use filter my documents properly? (I am looking for sth like all documents grater that 4000 and less than 10000)


Solution

  • Assuming this is because of a string mapping. I will edit if this isn't the case.

    Make sure you have the mapping for "category_id" set to be numeric. Try putting this as the mapping of that field (you will either need to re-create the index with the mapping before you ingest documents, or reindex to another index)

    PUT {YOUR_INDEX}
    {
      "mappings" :{
        "{YOUR_TYPE}":{
          "properties": {
            "category_id": {
              "type": "long"
            }
          }
        }
      }
    }