Search code examples
phpelasticsearchlogstashelastic-stackelk

How to update some values only in bulk in elasticsearch?


In elasticsearch I have data something like post_en as index and multiple posts saved in that which has fields like id, title, description, price, status.

Now how I can update specific fields only for each post with the use of bulk. (With single post I can easily update the fields.)

Here is a sample of json for bulk request.

{"_index":"post_en","_id":"966156"}
{"id":966156,"status":2}
{"_index":"post_en","_id":"966157"}
{"id":966157,"title":"some title","status":1}

Using this json it is updating the given fields but its also removing the other existing fields, meaning whole object is replaced with the given updated object. but i just want to update the given fields and keep remaining fields as it is.


Solution

  • You possibly will have to do update as opposed to insert (doc_as_upsert option)

    Quoting the documentation here

    https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#bulk-update

    The update action payload supports the following options: doc (partial document), upsert, doc_as_upsert, script, params (for script), lang (for script), and _source. See update documentation for details on the options.

    {
        "properties": {
            "id": {
                "type": "long"
            },
            "status": {
                "type": "integer"
            }
        }
    }
    

    Created index with mapping

    Indexed 2 documents

    {"id":966157,"status":2}
    {"id":966157,"status":3}
    

    Performed a bulk operation

    { "update" : {"_id" : "ISpCB4IBCOifrGItSDgU", "_index" : "post_en"} }
    { "doc" : {"status" : "4"} }
    { "update" : {"_id" : "ICpCB4IBCOifrGItKTgF", "_index" : "post_en"} }
    { "doc" : {"status" : "5"} }
    

    On search

            "hits": [
                {
                    "_index": "post_en",
                    "_id": "ISpCB4IBCOifrGItSDgU",
                    "_score": 1.0,
                    "_source": {
                        "id": 966157,
                        "status": "4"
                    }
                },
                {
                    "_index": "post_en",
                    "_id": "ICpCB4IBCOifrGItKTgF",
                    "_score": 1.0,
                    "_source": {
                        "id": 966156,
                        "status": "5"
                    }
                }
            ]