我已经检索了一些文档,并在elasticsearch中创建了索引。我用感觉来查询:
这是我在elasticsearch中的查询:
POST /index/_update_by_query
{
"script": {
"inline": "ctx._source.remove(\"home\")"
},
"query": {
"wildcard": {
"url": {
"value": "http://search.com/*"
}
}
}
}
这是我的Java程序:
Client client = TransportClient.builder().addPlugin(ReindexPlugin.class)
.build().addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9300));
UpdateByQueryRequestBuilder ubqrb = UpdateByQueryAction.INSTANCE
.newRequestBuilder(client);
Script script1 = new Script("ctx._source.remove" +FieldName);
BulkIndexByScrollResponse r = ubqrb.source("index").script(script1)
.filter(wildcardQuery("url", patternvalue)).get();
FieldName(将主页另存为字符串)是要从文档中删除的字段的名称。 patternvalue是存储模式“http://search.com/ *”的位置。当我运行此Java程序时,它不会从文档中删除home字段。它在我的文档中添加了一个名为remove的新字段。我可能会缺少一些东西。任何帮助,将不胜感激
最佳答案
如果FieldName是字符串home
,则表达式"ctx._source.remove" +FieldName
将等于"ctx._source.removehome"
,这不是正确的脚本。该行的正确代码是:
Script script1 = new Script("ctx._source.remove(\"" + FieldName + "\")");
这样,脚本将是:
ctx._source.remove("home")
这与您在json中编写的相同:
"inline": "ctx._source.remove(\"home\")"
(该json中的
\"
只是json语法中转义的"
)
关于elasticsearch - 如何使用Java在Elasticsearch中从文档中删除与模式匹配的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37577708/