Convert strings to floats at aggregation time?

16,080

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)

Share:
16,080

Related videos on Youtube

Sapikelio
Author by

Sapikelio

Software Engineer/Full Stack developer. Agile Methodologies lover. I use mostly Javascript and Java to develop WebApps and Android Apps, but I also know C++, Python, C#, Typescrip and more.

Updated on October 09, 2022

Comments

  • Sapikelio
    Sapikelio over 1 year

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

    }

  • Justin
    Justin over 7 years
    which value is the field and which is the field accessor?
  • Andrei Stefan
    Andrei Stefan over 7 years
    doc['value'] is the accessor and .value is its value.