gpt4 book ai didi

java - 使用spring esTemplate删除doc中的字段

转载 作者:行者123 更新时间:2023-12-01 22:00:14 26 4
gpt4 key购买 nike

我正在尝试使用ElasticsearchTemplate来更新我的es数据,代码如下:

UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(RiskViewDevStatistics.ESIndex);
updateRequest.type(RiskViewDevStatistics.ESType);
try {
updateRequest.doc(XContentFactory.jsonBuilder().startObject()
.field("owner", ownerId)
.endObject());
UpdateQuery updateQuery = new UpdateQueryBuilder().withId("docId")
.withClass(RiskViewDevStatistics.class).withUpdateRequest(updateRequest).build();
esTemplate.update(updateQuery);

代码类似于:

update owner from xx where id = xx

当我传入的参数ownerId为null时,es中的数据为:

"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "st-riskview",
"_type": "riskviewdevstatistics",
"_id": "AW47Wy7cEY_O-MqaGSGL",
"_score": 1.0,
"_source": {
"owner": null // null
}
}
]
}

我的问题是:如何删除该字段而不是将其设置为空?

注意:我不想替换整个文档。

最佳答案

试试这个:

POST st-riskview/riskviewdevstatistics/AW47Wy7cEY_O-MqaGSGL/_update
{
"script" : "ctx._source.remove('owner')"
}

Spring - 未测试

UpdateRequest updateRequest = new UpdateRequest(RiskViewDevStatistics.ESIndex,RiskViewDevStatistics.ESType,"docid");
Map<String, Object> parameters = singletonMap("field", "owner");
Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.remove(params.field)",parameters);
updateRequest.script(inline);

Updates with a script

关于java - 使用spring esTemplate删除doc中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58711065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com