Yao Pan
Yao Pan

Reputation: 524

how to delete field property of document in Elasticsearch using java?

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
        }],
        "lastUpdated": "2016-07-22T11:26:56Z",
        "processInstance": {
            "class": "bbb"
        },
        "bianhao": "123"
    }
}

how to delete id or class of items using java? how to delete {"class": "ccc","id": 2} using java?

I know how to delete a field,using:

client.prepareUpdate("test", "document", "1")
        .setScript(new Script(
                "ctx._source.remove(\"bianhao\")",
                ScriptService.ScriptType.INLINE, null, null))
        .get();

the content of items is json array,I have not found the method.

Upvotes: 1

Views: 874

Answers (1)

Val
Val

Reputation: 217274

You can achieve it like this:

Map<String, Object> params = new HashMap<>();
params.put("classParam", "ccc");
params.put("idParam", 2);

client.prepareUpdate("test", "document", "1")
    .setScript(new Script(
            "ctx._source.items.removeAll{ it['class'] == classParam && it.id == idParam }",
            ScriptService.ScriptType.INLINE, null, params))
    .get();

Upvotes: 1

Related Questions