1

Is it possible to make the key of the resulting aggregation be the int value returned by the script instead of a string?

See this example, but using dayOfMonth or hourOfDay instead of dayOfWeek, so there are more than 10 values, so the result ends up being ordered, "1", "10", "11", ..." instead of1, 2, 3,...`.

Here's an example of the full call:

POST /sales/_search?size=0
{
    "aggs": {
        "dayOfMonth": {
            "terms": {
                "script": {
                    "lang": "painless",
                    "source": "doc['date'].value.dayOfMonth"
                }
            }
        }
    }
}

And an example response:

{
  ...
  "aggregations": {
    "dayOfWeek": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "1",
          "doc_count": 4
        },
        {
          "key": "10",
          "doc_count": 3
        },
        {
          "key": "2",
          "doc_count": 2
        }
      ]
    }
  }
}
Dave Johansen
  • 889
  • 1
  • 7
  • 23

1 Answers1

0

Setting the value_type parameter can resolve the issue by coercing the unmapped field into the correct type.

{
  "aggs": {
    "ip_addresses": {
      "terms": {
        "script": "doc['date'].value.dayOfMonth",
        "value_type": "long"
      }
    }
  }
}

Failed Trying to Format Bytesedit

sadpepe
  • 168
  • 10