简体   繁体   中英

Elasticsearch - How to add or edit array of strings in a JSON object using Java?

I have userid, name and type variables as int, string and arraylist in Java respectively. I want to insert it into the elasticsearch database like this :-

users
{
    "userid": 5,
    "name": "test",
    "type": ["U1", "U2"]
}

1) How can I achieve this using Java client(high level API)?

2) How can I append to the "type" field(assuming it exists)?

example: I want to add "U3" to the "type" field so it becomes like this

users
{
    "userid": 5,
    "name": "test",
    "type": ["U1", "U2", "U3"]
}

You can add a value to an array by using a script.

POST /users/_update/5
{
    "script" : {
        "source": "ctx._source.type.add(params.type)",
        "lang": "painless",
        "params" : {
            "type" : "U3"
        }
    }
}

Taken from there : https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

I don't know how to use the high level API java client, sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM