栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch使用script更新long类型的数据

Elasticsearch使用script更新long类型的数据

项目里有人用script,更新部分字段
大概是这样的

POST sz_index_mean/_update_by_query
{
  "query": {
    "match_phrase": {
      "sz.sz_title": "Java加密技术"
    }
  },
  "script": {
    "source": "ctx._source.sz['sz_top_time'] = long类型的数据",
  }
}

当long类型的数据在int类型范围内时,

"source": "ctx._source.sz['sz_top_time'] = 123"

是可以更新成功的。
然而,当long类型的数据不在int类型范围内时,

"source": "ctx._source.sz['sz_top_time'] = 1637563531010"

则报了错

"caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Invalid int constant [1637563531010]. If you want a long constant then change it to [1637563531010L]."
    }

意思是1637563531010是一个无效的int类型常量,如果你期望一个long类型的常量应修改为1637563531010L

当我修改为

"source": "ctx._source.sz['sz_top_time'] = 1637563531010L"

时,发现更新成功了。

显然这种需要关心数据类型是否在末尾加"L"的写法是令人抓狂的,下面用params优雅地实现一下
以 params.top_time 代替上面直接拼接的内容

POST sz_index_mean/_update_by_query
{
  "query": {
    "match_phrase": {
      "sz.sz_title": "Java加密技术"
    }
  },
  "script": {
    "source": "ctx._source.sz['sz_top_time'] = params.top_time",
    "params": {
      "top_time": 1637563531010
    }
  }
}

嗯,也成功了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/774407.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号