0

Say I have documents stored like below.

document 1
    {
        id : '1',
        title : "This is a test document1",
        list : ["value1" , "value2"],
        ...
    }
document 2
    {
        id : '2',
        title : "This is a test document2",
        valueList : ["value1" , "value2"],
        ...
        }

I need to add some more elements to the valueList in the documents with a list of document ids using bulk api. The resulting should look like

document 1
    {
        id : '1',
        title : "This is a test document1",
        list : ["value1" , "value2", "value3"],
        ...
    }
document 2

    {
        id : '2',
        title : "This is a test document2",
        valueList : ["value1" , "value2" , "value3"],
        ...
        }

What can I do to achieve this? I tried using the scripts but it only updates a single document.

Sorry am really new to elastic search. I could even be stupid on this question. Please forgive and make me clear with this question.

1 Answers1

1

See Updating Document. It should be straightforward. You need to use _update and just to give you an idea, even though the documentation is nearly perfect, it could look like this:

POST /your_index/your_type/document1/_update

{
    id : '1',
    title : "This is a test document1",
    list : ["value1" , "value2", "value3"]
 }

This will update document1.

In case of bulk updates you should read Batch Processing and have a look at the Bulk API.

From the docs:

POST /your_index/your_type/_bulk
{ "update" : {"_id" : "document1", "_type" : "your_type", "_index" : "your_index"}}
{ "doc" : {"myfield" : "newvalue"} }
{ "update" : {"_id" : "document2", "_type" : "your_type", "_index" : "your_index"}}
{ "doc" : {"myfield" : "newvalue"} }

Please note that you can just use _update for Partial Updates.

The simplest form of the update request accepts a partial document as the doc parameter, which just gets merged with the existing document. Objects are merged together, existing scalar fields are overwritten, and new fields are added.

Diyarbakir
  • 1,999
  • 18
  • 36
  • Update is straight forward in the documents. Its given for updating the entire value. But what I exactly need is not to replace the entire list. I need to add another element with the already present elements. also the operation is a bulk operation. – Melvin Ruban Nov 30 '17 at 11:38
  • Update will merge the documents. I've added this to the answer. You cannot btw update only part of a field. Update replaces the whole field value in a document when updating. – Diyarbakir Nov 30 '17 at 11:47
  • Got the solution. Thanks by the way for your effort. – Melvin Ruban Nov 30 '17 at 11:50
  • @Diyarbakir it's too late but can you share your solution? – Nakul Bharti Apr 16 '22 at 20:07