4

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"]
}
10
  • 2
    Both of your strings are not in valid JSON format. Commented Jan 15, 2020 at 10:09
  • @LHCHIN could you please elaborate? I am unable to understand. Commented Jan 15, 2020 at 10:15
  • For example, all field names should be wrapped by double-quote. You can validate your JSON string with an validator such as JSON Formatter & Validator. Commented Jan 15, 2020 at 10:22
  • @LHCHIN edited. ignore the 'users' there Commented Jan 15, 2020 at 10:26
  • @DanteAdams can you please show your code and what have u tried and what error/issues ur facing with your code Commented Jan 15, 2020 at 11:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.