加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 00:51:15 所属栏目:百科 来源:网络整理
导读:在指定直方图聚合时,有没有办法将字符串转换为浮点数?因为我有文件的字段是浮点数但没有被弹性搜索解析,当我尝试使用字符串字段进行求和时它会抛出下一个错误. ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData canno
在指定直方图聚合时,有没有办法将字符串转换为浮点数?因为我有文件的字段是浮点数但没有被弹性搜索解析,当我尝试使用字符串字段进行求和时它会抛出下一个错误.
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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读