【问题标题】:Convert strings to floats at aggregation time?在聚合时将字符串转换为浮点数?
【发布时间】:2015-08-22 18:07:29
【问题描述】:

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

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

我知道我可以更改映射,但是对于我的用例,如果我这样做会更方便 在编写 字段的聚合。

这是我的代码:

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

}

【问题讨论】:

    标签: string elasticsearch sum aggregation


    【解决方案1】:

    你需要这个

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

    【讨论】:

    • 哪个value 是字段,哪个是字段访问器?
    • doc['value'] 是访问者,.value 是它的值。
    猜你喜欢
    • 2011-11-25
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    相关资源
    最近更新 更多