Brandsley
Brandsley

Reputation: 97

Elasticsearch - Rounding numbers by functions min and max

I have data in this format.

First data:

"properties" : [
  {
    "char_id" : 347,
    "char_value_string" : "107",
    "char_value_int" : 107
  },
  {
    "char_id" : 906,
    "char_value_string" : "8.5",
    "char_value_int" : 8.5
  },
  {
    "char_id" : 908,
    "char_value_string" : "Question_V1",
    "char_value_int" : null
  }

}

Second data:

"properties" : [
  {
    "char_id" : 347,
    "char_value_string" : "110",
    "char_value_int" : 110
  },
  {
    "char_id" : 906,
    "char_value_string" : "18",
    "char_value_int" : 18
  },
  {
    "char_id" : 908,
    "char_value_string" : "Question_V2",
    "char_value_int" : null
  }
}

Third data:

"properties" : [
  {
    "char_id" : 347,
    "char_value_string" : "220",
    "char_value_int" : 220
  },
  {
    "char_id" : 906,
    "char_value_string" : "54",
    "char_value_int" : 54
  },
  {
    "char_id" : 908,
    "char_value_string" : "Question_V3",
    "char_value_int" : null
  }
}

I use aggregation to find the minimum and maximum value of char_value_int for each char_id. This aggregation works but if char_value_int is float, then this max or min is rounded to integer (as example, 8.5 is rounded to 8). I need to get exactly 8.5, not 8.

{
  "size": 0,
  "aggs": {
    "char_ids": {
      "terms": {
        "field": "properties.char_id"
      },
      "aggs": {
        "min_char_values_int": {
          "min": {
            "field": "properties.char_value_int"
          }
        },
        "max_char_values_int": {
          "max": {
            "field": "properties.char_value_int"
          }
        }
      }
    }
  }
}

Upvotes: 0

Views: 402

Answers (1)

Val
Val

Reputation: 217464

Your value is stored as a long integer, hence the decimal part is gone at indexing time... even though it says 8.5 in your source document.

You need to change your mapping to float instead and reindex your data.

Then you're going to get the expected min/max values

Upvotes: 4

Related Questions