记录一次painless script
Posted beaconsky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录一次painless script相关的知识,希望对你有一定的参考价值。
//添加一条记录
PUT /dark-soul/_doc/1 { "owner": "omwNIByhrz", "common_data": { "testdx01_field_neizhi01": { "builtInType": "provinceCity" }, "testdx01_field_duoxuan": [ "bj1" ], "testdx01_field_danxuan": [ "beijin" ], "testdx01_field_tupian": [], "testdx01_field_wenben": "中文05" } }
//查询mapping
GET /dark-soul/_mapping
{ "dark-soul" : { "mappings" : { "properties" : { "common_data" : { "properties" : { "testdx01_field_danxuan" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "testdx01_field_duoxuan" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "testdx01_field_neizhi01" : { "properties" : { "builtInType" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }, "testdx01_field_wenben" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }, "ext" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "ext1" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "ext2" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "owner" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } }
//使用painless脚本
POST /dark-soul/_update_by_query { "query": { "match_all": {} }, "script": { "source": """ ctx._source.ext = ctx._source.common_data.toString(); String result = ""; for(entry in ctx._source.common_data.entrySet()){ if (entry.getValue() instanceof List) { result += ""; continue; } else if (entry.getValue() instanceof Map) { if (entry.getValue().get("displayValue") != null && entry.getValue().get("displayValue") != "") { result += (String)entry.getValue().get("displayValue") + " "; } continue; } else if(entry.getValue() != null && entry.getValue() != ""){ result += entry.getValue().toString() + " "; continue; } result += ""; } ctx._source.ext2 = result; """, "lang": "painless" } }
//查询变更后的记录
GET /dark-soul/_doc/1
以上是关于记录一次painless script的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch:Painless scripting 编程实践
Elasticsearch Painless script编程
Elasticsearch:painless script 语法基础和实战
ElasticSearch实战(三十七)-Ingest Pipeline + Painless Script (多管道脚本处理器)
ElasticSearch实战(三十七)-Ingest Pipeline + Painless Script (多管道脚本处理器)
elasticsearch Regexes are disabled. Set [script.painless.regex.enabled] to [true]