在聚合时将字符串转换为浮点数?

Convert strings to floats at aggregation time?

在指定直方图聚合时,有什么方法可以将字符串转换为浮点数吗?因为我的文档中的字段是浮点数,但没有被 elasticsearch 解析,当我尝试使用字符串字段求和时,它会抛出下一个错误。

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

我知道我可以更改映射,但对于我的用例,如果我 可以在编写时指定 "script : _value.tofloat()" 之类的内容 该字段的聚合。

这是我的代码:

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

}

你需要这个

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

对于名为 value 的字段:Float.parseFloat(doc['value'].value)