Rahul Saini
Rahul Saini

Reputation: 937

Auto Increment a field value every time a doc is updated in elasticsearch

This is payload

 {
  "videourl": "*****",
"name": "ABCqq",
"description": "AAAnb",
"tags": "#AAAzx",
"uploadedtime": "2020-02-24T05:48:37.527Z",
"uploadedby": "Dr AAAgh",
"thumbnail": "http://",
"duration": "5:32",
"postedby": "AAAdf",
"doctorimage": "AAA12",
"doctorname": "nnn",
}

Result in the form

{"_index": "rwe",
"_type": "_doc",
"_id": "8wEed3ABcYN_H8khP4hB",
"_score": 1,
"_source": {
    "videourl": "*****",
    "name": "ABCqq",
    "description": "AAAnb",
    "tags": "#AAAzx",
    "uploadedtime": "2020-02-24T05:48:37.527Z",
    "uploadedby": "Dr AAAgh",
    "thumbnail": "http://",
    "duration": "5:32",
    "postedby": "AAAdf",
    "doctorimage": "AAA12",
    "doctorname": "nnn"
}
}

This is a document where I want to increment the count value of the field every time when this doc gets updated. we have to add new field which has the name counter_value.

Expected Resultt

{"_index": "rwe",
"_type": "_doc",
"_id": "8wEed3ABcYN_H8khP4hB",
"_score": 1,
"_source": {
    "videourl": "*****",
    "name": "ABCqq",
    "description": "AAAnb",
    "tags": "#AAAzx",
    "uploadedtime": "2020-02-24T05:48:37.527Z",
    "uploadedby": "Dr AAAgh",
    "thumbnail": "http://",
    "duration": "5:32",
    "postedby": "AAAdf",
    "doctorimage": "AAA12",
    "doctorname": "nnn",
    "counter_value": 1
}
}

Upvotes: 1

Views: 702

Answers (1)

Stefano Branco
Stefano Branco

Reputation: 658

You can just increment the counter via scripting, see here and here. However, elastic already has a version field. Depending on your usecase, it might be enough to add the version parameter to your query, as described here:

curl -XGET 'http://localhost:9200/rwe/_search?version=true'

Upvotes: 1

Related Questions