使用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 ...
随机推荐
- PJzhang:CVE-2020-1472微软NetLogon权限提升漏洞~复现
猫宁~~~ 虚拟机上进行 安装windows 2008 R2 查看服务器ip 本地连接属性,取消ipv6,ip设置为192.168.43.158,子网掩码255.255.255.0,网关192.168 ...
- Centos-跟踪数据传输路由状态-traceroute
traceroute 显示网卡数据包传输到指定主机的路径信息,追踪数据传输路由状况,默认数据包大小38字节 相关选项 -i 使用指定网络接口发送数据 -n 使用IP而不使用主机名 -v 显示命令的 ...
- 02 ArcPython的使用大纲
一.什么情况下使用ArcPython? 1.现有工具实现不了,可以用python 2.流程化需要时,可以使用python 3.没有AE等二次开发环境 4.其他特殊场景 二.ArcPython在ArcG ...
- 如何使用微软提供的TCHAR.H头文件?
转载:https://www.cnblogs.com/flyingspark/archive/2012/03/16/2399788.html 如何使用微软提供的TCHAR.H头文件? 如果你现在写的代 ...
- 1个LED灯闪烁的Arduino控制
控制任务和要求 让一个LED灯闪烁 接线 程序设计 1 int half_cycle=1000; // define the cycle time of LED blink 2 int LED_pin ...
- P5091 【模板】扩展欧拉定理
题目链接 昨天考试考到了欧拉公式,结果发现自己不会,就来恶补一下. 欧拉公式 \(a^b \bmod p = a^{b}\) \(b < \varphi(p)\) \(a^b \bmod p = ...
- Spring Boot入门系列(二十)快速打造Restful API 接口
spring boot入门系列文章已经写到第二十篇,前面我们讲了spring boot的基础入门的内容,也介绍了spring boot 整合mybatis,整合redis.整合Thymeleaf 模板 ...
- linux查看进程内存使用情况,以及将线程情况输出文件
用jmap把进程内存使用情况dump到文件中,再用jhat分析查看.jmap进行dump命令格式如下: jmap -dump:format=b,file=/tmp/dump.dat 21711 -- ...
- Python+Appium自动化测试(14)-yaml配置Desired capabilities
一,前言 在之前的appium自动化测试示例中,我们都是把构造driver实例对象的数据(即Desired Capabilities)写在业务代码里,如下: # -*- coding:utf-8 -* ...
- FreeType2使用总结(转)
一.FreeType2简介 1. 是一个免费.开源.可移植且高质量的字体引擎: 2. 支持多种字体格式文件,并提供了统一的访问接口: 3. 支持单色位图.反走样位图渲染,这使字体显示质量达到Mac的水 ...