Stpete111
Stpete111

Reputation: 3427

ElasticSearch - query to append a string to an existing string in a document field

All of our ElasticSearch documents contain a field called Categories which contains a string of comma-separated values. The length of each value between the commas is 5 characters, and the field can be anywhere from 1 to 12 5-character values.

I need to run a query against the index which says append ,ABCDE to the end of Categories field of all documents, unless the field already contains the value ABCDE in it, in which case do not append anything to the field.

Upvotes: 0

Views: 1694

Answers (1)

Ashraful Islam
Ashraful Islam

Reputation: 12830

You can use ElasticSearch _update_by_query like below

POST my_index/my_type/_update_by_query
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "Categories": "ABCDE"
          }
        }
      ]
    }
  },
  "script": {
    "inline": "ctx._source.Categories += ', ABCDE'",
    "lang": "painless"
  }
}

The above query will append string ', ABCDE' to Categories if Categories doesn't contains the term ABCDE

Upvotes: 2

Related Questions