0

我正在尝试使用 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
                }
            }
        ]
    }

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

注意:我不想替换全部文档。

4

1 回答 1

0

试试这个:

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

弹簧 - 未测试

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);

使用脚本更新

于 2019-11-05T12:24:05.087 回答