Home > Enterprise >  Elasticsearch range aggregation ignores negative values
Elasticsearch range aggregation ignores negative values

Time:05-26

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

CodePudding user response:

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

CodePudding user response:

To solve

POST /_bulk/
{"index":{"_index":"72376077"}}
{"data": -1}
{"index":{"_index":"72376077"}}
{"data":-2}
{"index":{"_index":"72376077"}}
{"data":0}
{"index":{"_index":"72376077"}}
{"data":1}
{"index":{"_index":"72376077"}}
{"data":2}

GET /72376077/_search
{
  "size": 0, 
  "aggs": {
    "stock_ranges": {
      "range": {
        "field": "data",
        "ranges": [
          {
            "to": 0.1
          },
          {
            "from": 1
          }
        ]
      }
    }
  }
}

It give those buckets:

{
  "buckets": [
    {
      "key": "*-0.1",
      "to": 0.0,
      "doc_count": 3
    },
    {
      "key": "1.0-*",
      "from": 1.0,
      "doc_count": 2
    }
  ]
}

Which seems absolutely fine ?

  • Related