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

Sap*_*lio 6 string sum aggregation elasticsearch

在指定直方图聚合时,有没有办法将字符串转换为浮点数?因为我有文件的字段是浮点数但是没有被弹性搜索解析,当我尝试使用字符串字段进行求和时它会抛出下一个错误.

ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData 
cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}]"
Run Code Online (Sandbox Code Playgroud)

我知道我可以更改映射,但是对于我的用例,如果我在为字段编写聚合时指定类似"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"
                    }
             }
         }
    }
}  
Run Code Online (Sandbox Code Playgroud)

}

And*_*fan 15

你需要这个

{
  "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)"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

对于一个被称为的字段value:Float.parseFloat(doc['value'].value)