8

Consider the following document

{
  "title":  "My first blog entry",
  "text":   "Starting to get the hang of this...",
  "tags": [ "testing" ], 
  "views":  0 
}

I need to run kind of an upsert operation. If I encounter data like

{
    "id": 1,
    "tags": [ "new tag" ]
}

I want to update the existing document with same id. So result should be :

{
    "id": 1,
    "title":  "My first blog entry",
    "text":   "Starting to get the hang of this...",
    "tags": [ "testing", "new tag" ], 
    "views":  0 
}

If the document with same id does not exist, I want to create a new one.

Now in databases like mongoDB, I could use update with $addToSet or $push operation. I could not find similar operation in Elasticsearch.

I read that it can be done by writing scripts in groovy. However, this needs to be done on a file containing 200 million records. I am not sure if I can use groovy in combination with bulk API. Is it possible ?

2
  • Have a look at the HttpBuilder project (github.com/jgritman/httpbuilder) Commented Sep 25, 2015 at 1:33
  • Can you show an excerpt of your file containing the records? Commented Sep 25, 2015 at 3:24

1 Answer 1

8

You dont need to use bulk API for this. You can use an upsert request. Upsert request can ALSO be embedded in the bulk request.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  "script": "if (ctx._source.tags.contains(\"tags\")) {ctx._source.tags += tag;} else {ctx._source.tags = [tag]}",
  "params": {
    "tag": "newTag"
  },
  "upsert": {
    "title": "My first blog entry",
    "text": "Starting to get the hang of this...",
    "tags": [
      "newTag"
    ],
    "views": 0
  }
}'
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Vineet. This worked well with bulk API. However, there was a slight change in my requirement. The upsert document should contain the same tag in its tag array as the tag in params. Also, if condition should be modified to include the condition of if (!ctx._source.tags.contains(tag)). Please modify your answer so that I can accept it and close it.
I have made the change
in the same scenario if i want to update array of documents. how can i pass params. please help me out..thank you
@PavanKumarVarma - can you post that as a separate question and ping me
@VineethMohan Is there any example on how to use upsert with bulk api?

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.