使用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 ...
随机推荐
- [视频]iNeuOS 自主可控工业互联网一体化解决方案 整体介绍
演示地址:http://demo.ineuos.net (注:自己注册) iNeuOS 自主可控工业互联网操作系统,提供全新解决方案 核心组件包括:边缘网关(iNeuLink).设备容器(iNeuK ...
- 基于python的webUI自动化-小白基础篇
最近打算研究一下基于python的webUI自动化,先自学了一下相关基础知识,大概用了一个多月的时间,主要是找视频,一边看视频或者文档一边对照着敲代码运行. 重点强调:一定要一边看一边对照着敲代码运行 ...
- Spring 集成 RabbitMQ
pom.xml <dependency> <groupId>org.springframework.amqp</groupId> <artifactId> ...
- js自动生成条形码插件-JsBarcode
JsBarcode.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- unity inspector 自动装载Commont和Prefab属性
在使用unity的过程中,经常遇到这样的问题:每次都需要手动为序列化属性拖拽赋值.像这样: 试着找了找,真的找到了一份代码,但是缺少自动装载Prefab的功能.之后我花了点时间添加这个功能. 使用方法 ...
- requests和正则表达式爬取猫眼电影Top100练习
1 import requests 2 import re 3 from multiprocessing import Pool 4 from requests.exceptions import R ...
- FOV
来源:https://blog.csdn.net/chepwavege/article/details/98876550 视场 (视图字段) 是指现场对面相机镜头的立体角.图如下图所示︰ 高频通气︰ ...
- Zookeeper基础理论
Zookeeper是分布式开源协调服务, 主要用来解决分布式集群中应用系统的一致性问题. 本质上是分布式小文件存储系统. 特性 全局数据一致性(集群中每个服务器保存一份相同的数据副本,Client ...
- 阅读源码,从ArrayList开始
前言 为啥要阅读源码?一句话,为了写出更好的程序. 一方面,只有了解了代码的执行过程,我们才能更好的使用别人提供的工具和框架,写出高效的程序.另一方面,一些经典的代码背后蕴藏的思想和技巧很值得学习,通 ...
- 3.Android网络编程-http介绍
1.HTTP请求方法 根据HTTP标准,HTTP请求可以使用多种请求方法. HTTP1.0定义了三种请求方法: GET(查), POST(改)和 HEAD(获取报头,一般用来测试链接是否正常)方法. ...