Convert strings to floats at aggregation time?

Sapikelio picture Sapikelio · Jun 8, 2015 · Viewed 13.7k times · Source

Is there any way to convert strings to floats when specifying a histogram aggregation? Because I have documents with fields that are floats but are not parsed by elasticsearch as such, and when I attempt to do a sum using a string field It throws the next error.

ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData 
cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}]"

I know I could change the mapping, but for the usage case that I have, it would be more handy if I could specify something like "script : _value.tofloat()" when writing the aggregation for the field.

This is my code:

{
"query" : {
    "bool": {"
         must": [
            {"match": { "sensorId":  "D14UD021808ARZC" }},
            {"match": { "variableName": "CAUDAL"}}
        ]
    }
},      
"aggs" : {
    "caudal_per_month" : {
          "date_histogram" : {
                  "field" : "timestamp",
                  "interval" : "month"
          },
          "aggs": {
             "totalmonth": {
                    "sum": {
                        "field": "value",
                        "script" : "_value*1.0"
                    }
             }
         }
    }
}  

}

Answer

Andrei Stefan picture Andrei Stefan · Jun 8, 2015

You need this

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "sensorId": "D14UD021808ARZC"
          }
        },
        {
          "match": {
            "variableName": "CAUDAL"
          }
        }
      ]
    }
  },
  "aggs": {
    "caudal_per_month": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "month"
      },
      "aggs": {
        "totalmonth": {
          "sum": {
            "script": "Float.parseFloat(doc['value'].value)"
          }
        }
      }
    }
  }
}

For a field that's called value: Float.parseFloat(doc['value'].value)