t_-_t
t_-_t

Reputation: 128

Number ranges / filters in Elasticsearch

I am trying to build an index in elasticsearch and search the numerical fields afterwards. The resultset is empty, even thouhg the logical result would be to have a 1 record resultset.

Below the actions to reproduce (using sense)

Create the index

PUT playground

Create a document

POST playground/doc
{
   "value": {
      "textlabel": "Lorem Ipsum",
      "numerlabel": 37.0,
      "datelabel":"1978-10-26T00:00:00+02:00"
   }
}

The autogenerated mapping file seems to provide the proper data types

{
   "playground": {
      "mappings": {
         "doc": {
            "properties": {
               "value": {
                  "properties": {
                     "datelabel": {
                        "type": "date",
                        "format": "dateOptionalTime"
                     },
                     "numerlabel": {
                        "type": "double"
                     },
                     "textlabel": {
                        "type": "string"
                     }
                  }
               }
            }
         }
      }
   }
}

Searching on date ranges works fine, returning the expected data

POST playground/doc/_search
{
    "query": {         
        "filtered": {           
           "filter": {
               "range" : {
                    "value.datelabel" : {                        
                        "lte": "now-28y" 
                    }
                }
           }
        }
    }
}

But numeric ranges do not return any results

POST playground/doc/_search
{
    "query": {
      "filtered": {           
           "filter": {
               "range":  {
                    "value.numberlabel" : {             
                        "lte": 100
                    }
                }
           }
        }
    }
}

results in

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

Any suggestions why?

Upvotes: 2

Views: 3310

Answers (2)

Andrei Stefan
Andrei Stefan

Reputation: 52368

You have a typo: numerlabel - numberlabel. The correct query, given that mapping is:

{
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "value.numerlabel": {
            "lte": 100
          }
        }
      }
    }
  }
}

Upvotes: 2

Sloan Ahrens
Sloan Ahrens

Reputation: 8718

You just had a misspelling. "numerlabel" in your document, but "value.numberlabel" in your query.

After I ran your set-up code, this works:

POST playground/doc/_search
{
    "query": {
      "filtered": {           
           "filter": {
               "range":  {
                    "value.numerlabel" : {             
                        "lte": 100
                    }
                }
           }
        }
    }
}
...
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "playground",
            "_type": "doc",
            "_id": "AU5UiwngQAg_uFp56nys",
            "_score": 1,
            "_source": {
               "value": {
                  "textlabel": "Lorem Ipsum",
                  "numerlabel": 37,
                  "datelabel": "1978-10-26T00:00:00+02:00"
               }
            }
         }
      ]
   }
}

Upvotes: 2

Related Questions