使用painless将ElasticSearch字符串拆分为数组
一、实现场景:
ES字符串类型字段imgs,有些历史数据是用逗号分隔的字符串,需要将历史数据拆分为数组形式。
示例:
1.构造测试数据:
创建索引并推送几条典型的历史数据,涵盖以下几种情况:
- 逗号分隔字符串;
- 数组类型;
- 长度为0的字符串;
- 空数组。
PUT test_cj/test/id_1
{
"imgs": "https://img2.autoimg.cn/hscdfs/g27/M08/C8/C9/autohomecar__ChcCQF2tFp-AVbd1AABUAEDjxME398.jpg,https://img2.autoimg.cn/hscdfs/g27/M00/C5/41/autohomecar__ChsEfF2tFp-AUNE9AABAAMdcvmc812.jpg,https://img2.autoimg.cn/hscdfs/g27/M06/C5/41/autohomecar__ChsEfF2tFp-AaGesAABUABSmyrM852.jpg"
}
PUT test_cj/test/id_2
{
"imgs": [
"https://img2.autoimg.cn/hscdfs/g1/M08/83/34/autohomecar__ChcCQ1wGPV6AMsb0AAD8AKsOcww068.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M03/B4/5D/autohomecar__ChsEmVwGPV-AQmnZAADMAMSUUHU068.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M00/83/34/autohomecar__ChcCQ1wGPV-ABZk0AACcAItlOsc793.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M07/B3/D1/autohomecar__ChsEj1wGPV-APTZEAABcACQZNGk338.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M0B/83/34/autohomecar__ChcCQ1wGPV-ASLK_AACgAO-S6mU461.jpg"
]
}
PUT test_cj/test/id_3
{
"imgs": ""
}
PUT test_cj/test/id_4
{
"imgs": []
}
2.确认一下数据。
GET test_cj/_search
[
{
"_index" : "test_cj",
"_type" : "test",
"_id" : "id_1",
"_score" : 1.0,
"_source" : {
"imgs" : "https://img2.autoimg.cn/hscdfs/g27/M08/C8/C9/autohomecar__ChcCQF2tFp-AVbd1AABUAEDjxME398.jpg,https://img2.autoimg.cn/hscdfs/g27/M00/C5/41/autohomecar__ChsEfF2tFp-AUNE9AABAAMdcvmc812.jpg,https://img2.autoimg.cn/hscdfs/g27/M06/C5/41/autohomecar__ChsEfF2tFp-AaGesAABUABSmyrM852.jpg"
}
},
{
"_index" : "test_cj",
"_type" : "test",
"_id" : "id_2",
"_score" : 1.0,
"_source" : {
"imgs" : [
"https://img2.autoimg.cn/hscdfs/g1/M08/83/34/autohomecar__ChcCQ1wGPV6AMsb0AAD8AKsOcww068.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M03/B4/5D/autohomecar__ChsEmVwGPV-AQmnZAADMAMSUUHU068.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M00/83/34/autohomecar__ChcCQ1wGPV-ABZk0AACcAItlOsc793.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M07/B3/D1/autohomecar__ChsEj1wGPV-APTZEAABcACQZNGk338.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M0B/83/34/autohomecar__ChcCQ1wGPV-ASLK_AACgAO-S6mU461.jpg"
]
}
},
{
"_index" : "test_cj",
"_type" : "test",
"_id" : "id_3",
"_score" : 1.0,
"_source" : {
"imgs" : ""
}
},
{
"_index" : "test_cj",
"_type" : "test",
"_id" : "id_4",
"_score" : 1.0,
"_source" : {
"imgs" : [ ]
}
}
]
3.执行painless脚本
使用painless脚本更新历史数据。有几点需要注意:
- 只更新符合某些条件的数据,可以使用_update_by_query操作,这个例子比较简单没有设置query语句。
- 执行过程中冲突处理方式,这里使用的是conflicts=proceed,表示继续执行;
- painless检测对象类型使用关键字instanceof;
- painless脚本拆分字符串,想避免使用正则表达式,而是选用了StringTokenizer实现。
POST test_cj/_update_by_query?conflicts=proceed
{
"script": {
"source": """
if(ctx._source['imgs'] instanceof String){
String s=ctx._source['imgs'];
ArrayList array=new ArrayList();
if(!s.isEmpty()){
String splitter = ",";
StringTokenizer tokenValue = new StringTokenizer(s, splitter);
while (tokenValue.hasMoreTokens()) {
array.add(tokenValue.nextToken());
}
}
ctx._source.imgs=array;
}
"""
}
}
4.如果更新数据量较大,需要执行一段时间,期间查看执行进度:
GET _tasks?detailed=true&actions=*byquery
5.查看执行结果。
GET test_cj/_search
[
{
"_index" : "test_cj",
"_type" : "test",
"_id" : "id_1",
"_score" : 1.0,
"_source" : {
"imgs" : [
"https://img2.autoimg.cn/hscdfs/g27/M08/C8/C9/autohomecar__ChcCQF2tFp-AVbd1AABUAEDjxME398.jpg",
"https://img2.autoimg.cn/hscdfs/g27/M00/C5/41/autohomecar__ChsEfF2tFp-AUNE9AABAAMdcvmc812.jpg",
"https://img2.autoimg.cn/hscdfs/g27/M06/C5/41/autohomecar__ChsEfF2tFp-AaGesAABUABSmyrM852.jpg"
]
}
},
{
"_index" : "test_cj",
"_type" : "test",
"_id" : "id_2",
"_score" : 1.0,
"_source" : {
"imgs" : [
"https://img2.autoimg.cn/hscdfs/g1/M08/83/34/autohomecar__ChcCQ1wGPV6AMsb0AAD8AKsOcww068.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M03/B4/5D/autohomecar__ChsEmVwGPV-AQmnZAADMAMSUUHU068.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M00/83/34/autohomecar__ChcCQ1wGPV-ABZk0AACcAItlOsc793.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M07/B3/D1/autohomecar__ChsEj1wGPV-APTZEAABcACQZNGk338.jpg",
"https://img2.autoimg.cn/hscdfs/g1/M0B/83/34/autohomecar__ChcCQ1wGPV-ASLK_AACgAO-S6mU461.jpg"
]
}
},
{
"_index" : "test_cj",
"_type" : "test",
"_id" : "id_3",
"_score" : 1.0,
"_source" : {
"imgs" : [ ]
}
},
{
"_index" : "test_cj",
"_type" : "test",
"_id" : "id_4",
"_score" : 1.0,
"_source" : {
"imgs" : [ ]
}
}
]
使用painless将ElasticSearch字符串拆分为数组的更多相关文章
- Swift - 将字符串拆分成数组(把一个字符串分割成字符串数组)
在Swift中,如果需要把一个字符串根据特定的分隔符拆分(split)成字符串数组,通常有如下两种方法: 1,使用componentsSeparatedByString()方法 1 2 3 4 5 l ...
- Java与JavaScript 完美实现字符串拆分(利用数组存储)与合并的互逆操作
Java: String typeStr = "1,2"; String[] typeArray = typeStr.split(","); typeStr = ...
- 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列
package com.wangcf; /** * 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列 * @author fan ...
- php将长字符串拆分为指定最大宽度的字符串数组
/** * 将字符串拆分为指定最大宽度的字符串数组.单字节字符宽度为1,多字节字符通常宽度为2 * @param string $msg 要拆分的字符串 * @param int $width 结果数 ...
- 2016/4/5 Ajax ①用户名 密码 登陆 注册 ② 判断用户名是否已存在 ③点击按钮出现民族选项下拉菜单 ④DBDA类 加入Ajaxquery方法 数组变字符串 字符串拆分
①登陆 注册 查表匹配 0405Ajax.php ②判断用户名是否存在 <!DOCTYPE html> <html lang="en"> ...
- java 将字符串拆分成块装数组
split 将字符串拆分 regex=???,根据???以其为界进行拆分. public String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串. 该方法的作用 ...
- Java字符串拆分和字符串连接
Java字符串拆分/连接 public class LierString{ //------------------------------------------------------------ ...
- PHP第八课 字符串拆分经常使用函数
课程概要: 通过这节课可以对字符串进行主要的操作. 字符串知识点: 1.字符串的处理介绍 2.经常使用的字符串输出函数 3.经常使用的字符串格式化函数 4.字符串比較函数 5.正則表達式在字符串中的应 ...
- [NOI2016]优秀的拆分(SA数组)
[NOI2016]优秀的拆分 题目描述 如果一个字符串可以被拆分为 \(AABB\) 的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 \(aabaaba ...
随机推荐
- spring:bean的生命周期
1.spring中bean的生命周期 (1)概念 在spring框架中,所有的bean对象都有生命周期,就是指bean的创建.初始化.服务.销毁的一个过程. (2)bean的生命周期 bean的定义 ...
- org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null
通过这个简单的案例,手把手教给你分析异常信息(适合初学者看) org.springframework.dao.InvalidDataAccessApiUsageException: The given ...
- 2020 巅峰极客 WP_ Re
第一题:virus 是一个win32 的题,没给加壳. 主函数: int __cdecl main(int argc, const char **argv, const char **envp) { ...
- linux循环定时任务
crond定时任务 centos # 重启服务 service crond restart ----------------------------------------- chkconfig cr ...
- spark textfile rdd 日记
批量处理模板方法, 核心处理方法为内部方法 def batchProces(sc: SparkContext, locationFlag: Int, minid: Int, maxid: Int, n ...
- springboot项目根据不同的环境启动不同的配置,如开发环境dev,测试环境sit,生产环境application
在项目开发中,会有多个环境,如在开发环境开发完,然后在测试环境测试,最后到生产环境,每个环境的配置是不一样的,如数据库配置:还好spring提供了一个管理配置的方式:
- Python-嵌套列表变成普通列表
如何把[1, 5, 6, [2, 7, [3, [4, 5, 6]]]]变成[1, 5, 6, 2, 7, 3, 4, 5, 6]? 思考: -- for循环每次都遍历列表一层 -- 把取出的单个值加 ...
- Layman PHP+JavaScript 实现图片无刷新上传
html文件代码 <!-- ajax文件上传开始 --> <script type="text/javascript" src="/imageuploa ...
- Android作业10/07
1.多个Activity界面实现数据的传递 <?xml version="1.0" encoding="utf-8"?> <androidx. ...
- shell-的特殊变量-位置变量$0 $n $* $# $@详解
一:shell特殊变量 1. 位置变量 $0 获取当前执行的shell脚本的文件名,包括路径 $n 获取当前执行的shell脚本的第n个参数值,n=1..9,当n为0时表示脚本的文件名,如果n大于9 ...