elasticsearch - 如何使用常规脚本反转Elasticsearch文档中数组内的所有字符串?

标签 elasticsearch groovy

我有一个Elasticsearch文件
{ "_index": "testindex", "_type": "testtype", "_id": "doc1", "_version": 1, "found": true, "_source": { "array": [ "abc", "def", "ghi" ] } }
如何反转文档中数组内部的所有字符串?我尝试通过以下脚本使用更新API
temp = []; for(String item : ctx._source.array) temp << item.reverse(); ctx._source.array = temp;
更新Java中的api:

new org.elasticsearch.action.update.UpdateRequest(index, type, docId).script(script);

我在脚本中收到不允许进行方法调用之类的异常。
Bulk request failure, id: [doc4], message: ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptCompilationException[MultipleCompilationErrorsException[startup failed:
General error during canonicalization: Method calls not allowed on [java.lang.String]

最佳答案

click too see the pic 试试这个

temp=[];

ctx._source.array.each{

    it -> temp.add(it.reverse())
}

ctx._source.array = temp

关于elasticsearch - 如何使用常规脚本反转Elasticsearch文档中数组内的所有字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37391474/

相关文章:

eclipse - 在 Eclipse 中运行 groovy 脚本

jenkins - 获取Jenkinsfile(Groovy)中给定文件夹中的文件名列表

c# - ElasticSearch 5.x上下文建议器-单个上下文多个值

elasticsearch - 使用来自不同索引的数据进行 Elasticsearch 计算

ruby - 使用 Elasticsearch 处理超出内存限制的文档处理

spring - 如何将属性值注入(inject)到 Spock 测试中?

java - Gradle中的String.ReplaceAlli缺少要转义的字符

elasticsearch - 在 elastica 中搜索多个索引

Elasticsearch查询嵌套字段时返回错误

java - 向多个元素字符串输出添加逗号的更好方法是什么?