Search code examples
elasticsearchelasticsearch-aggregation

Elasticsearch range aggregation ignores negative values


I'm trying to bucket a range aggregation and achieve the following buckets:

<=0 - 0 including negative numbers
>=1 - 1 and above

field type = Integer

Query:

GET _search
{
 "aggs": {
    "stock_ranges": {
      "range": {
        "field": "Facets_Integer_Stock",
        "ranges": [
          {
            "to": 0
          },
          {
            "from": 1
          }
        ]
      }
    }
  }
}

Mapping for field Facets_Integer_Stock :

"Facets_Integer_Stock" : {
   "type" : "integer"
},

I'm expecting 2 counts in the first bucket <0. It should include the hits with -1 and 0.

Response

{
  "took" : 26,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "stock_ranges" : {
      "buckets" : [
        {
          "key" : "*-0.0",
          "to" : 0.0,
          "doc_count" : 1
        },
        {
          "key" : "1.0-*",
          "from" : 1.0,
          "doc_count" : 2
        }
      ]
    }
  }
}


Solution

  • In the range aggregation, to is always exclusive, you should use lte and gte instead:

    GET _search
    {
     "aggs": {
        "stock_ranges": {
          "range": {
            "field": "Facets_Integer_Stock",
            "ranges": [
              {
                "lte": 0
              },
              {
                "gte": 1
              }
            ]
          }
        }
      }
    }