1

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.

0

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

5 Comments

Is there replaceAll() method?
What do you want to achieve?
change {"class": "ccc","id": 2} to {"class": "ccc12","id": 2}
I suggest you create another question as this is a different issue from the topic of this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.