本文介绍了如何使用java更改Elasticsearch中文档的field属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Elasticsearch中写了一个文件: {
_index:test,
_type:document,
_id:1,
_score:1,
_source:{
class ,
id:1,
items:[{
class:aaa,
id:1
},{
class:ccc,
id:2
}],
bianhao:123
}
}
我想替换 {class:ccc,id :code>code,code,ccc12,id:2}
,如何实现?
解决方案
你可以这样做:
映射< String,Object> params = new HashMap<>();
params.put(idParam,2);
params.put(classParam,ccc12);
client.prepareUpdate(test,document,1)
.setScript(new script(
ctx._source.items.find {it.id == idParam} ['class'] = classParam,
ScriptService.ScriptType.INLINE,null,params))
.get();
更新
如果要更改多个字段,可以这样做:
Map< String,Object> params = new HashMap<>();
params.put(idParam,2);
params.put(classParam,ccc12);
params.put(field1Param,value2);
params.put(field2Param,value3);
client.prepareUpdate(test,document,1)
.setScript(new script(
def item = ctx._source.items.find { it.id == idParam}; if(item){item ['class'] = classParam; item ['field1'] = field1Param; item ['field2'] = field2Param},
ScriptService.ScriptType。 INLINE,null,params))
.get();
I hava a document in Elasticsearch:
{
"_index": "test",
"_type": "document",
"_id": "1",
"_score": 1,
"_source": {
"class": "aaa",
"id": 1,
"items": [{
"class": "aaa",
"id": 1
}, {
"class": "ccc",
"id": 2
}],
"bianhao": "123"
}
}
I want to replace {"class": "ccc","id": 2}
of items
with {"class": "ccc12","id": 2}
,how to achieve that?
解决方案
You can do it like this:
Map<String, Object> params = new HashMap<>();
params.put("idParam", 2);
params.put("classParam", "ccc12");
client.prepareUpdate("test", "document", "1")
.setScript(new Script(
"ctx._source.items.find{ it.id == idParam }['class'] = classParam",
ScriptService.ScriptType.INLINE, null, params))
.get();
UPDATE
And if you want to change multiple fields, you can do it like this:
Map<String, Object> params = new HashMap<>();
params.put("idParam", 2);
params.put("classParam", "ccc12");
params.put("field1Param", "value2");
params.put("field2Param", "value3");
client.prepareUpdate("test", "document", "1")
.setScript(new Script(
"def item = ctx._source.items.find{ it.id == idParam}; if (item) {item['class'] = classParam; item['field1'] = field1Param; item['field2'] = field2Param}",
ScriptService.ScriptType.INLINE, null, params))
.get();
这篇关于如何使用java更改Elasticsearch中文档的field属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!