Elasticsearch-js: Bulk actions for Index and Update: index or update with doc_as_upsert?

Created on 19 Nov 2018  路  3Comments  路  Source: elastic/elasticsearch-js

I'm doing a index / update bulk call using the client.bulk api:

When I do the insert action

{
                index: 'myIndex',
                type: '_doc',
                body: [
                    // action description
                    { index: { _id: docIDOne } },
                    // the document to index
                    itemOne,
                    // action description
                    { index: { _id: docIDTwo } },
                    // the document to index
                    itemTwo,
                ]
            }

The index action works ok. At second attempt of the index action with the same items it works as well and I get a updated value for the result field in the response:

{
      "index": {
        "_index": "entities",
        "_type": "_doc",
        "_id": "TGVCcm9u",
        "_version": 4,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 5,
        "_primary_term": 1,
        "status": 200
      }

Does this mean that that doc has been updated?
I have tried also to use the update action with the doc_as_upsert flag like:

elastic.bulk({
                index: indexName,
                type: '_doc',
                body: [
                    // action description
                    { update: { _id: docIDOne } },
                    // the document to index
                    { doc_as_upsert : true, doc: itemOne },
                    // action description
                    { update: { _id: docIDTwo } },
                    // the document to index
                    { doc_as_upsert : true, doc: 
 },
                ]
            });

and it gives me back a created result the first attempt, and a noop the second one. I suppose I will get a updated if it updates the content.

So the point is: which api call shall I use to perform something closer to a insert OR update: index or update with the doc_as_upsert flag?

question

Most helpful comment

You should use update with doc_as_upsert :)

This issue tracker should be used only for questions or issues related to the JavaScript client. If you have other general questions about Elastic I suggest you use our fantastic forum, where your question will probably help others as well :)

https://discuss.elastic.co/

All 3 comments

You should use update with doc_as_upsert :)

This issue tracker should be used only for questions or issues related to the JavaScript client. If you have other general questions about Elastic I suggest you use our fantastic forum, where your question will probably help others as well :)

https://discuss.elastic.co/

@delvedor thank you, I was not sure that the JavaScript API was supporting it, since in the docs there is not reference to it, I mean here.

It's not documented because only querystring parameters are documented, all supported body keys are described in https://www.elastic.co/guide/en/elasticsearch/reference/6.5/docs-update.html (you can arrive at that URL from the js docs in the body section) ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mfaizanse picture mfaizanse  路  3Comments

drakejin picture drakejin  路  4Comments

ablakey picture ablakey  路  4Comments

lizpark picture lizpark  路  3Comments

kaustavghosh06 picture kaustavghosh06  路  5Comments