首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用groovy脚本反转elasticsearch文档中数组中的所有字符串?

如何使用groovy脚本反转elasticsearch文档中数组中的所有字符串?
EN

Stack Overflow用户
提问于 2016-05-23 20:49:06
回答 1查看 150关注 0票数 2

我有一个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:

代码语言:javascript
复制
new org.elasticsearch.action.update.UpdateRequest(index, type, docId).script(script);

我得到了一个异常,比如脚本中不允许的方法调用。

代码语言:javascript
复制
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]
EN

回答 1

Stack Overflow用户

发布于 2016-05-23 21:17:19

尝尝这个

代码语言:javascript
复制
temp=[];

ctx._source.array.each{

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

ctx._source.array = temp
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37391474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档