Naman Gupta
Naman Gupta

Reputation: 155

Elasticsearch max constraint on integer field?

I am trying to implement a system where one of the fields, (say count) is to incremented +1 via updates but is supposed to be constrained to have a maximum value of let's say 100. I have tried googling and have found nothing. Do these kind of updates need to be done through a painless script perhaps which will check against the current value before updating count? Or is there a way elasticsearch can take a "max" constraint for this field?

Any answeres will be highly appreciated!

Thanks

Upvotes: 0

Views: 213

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9099

In Elasticsearch you cannot define field size or Check constraint like SQL.

You need to handle this while indexing or updating

Update query

POST <indexname>/_update_by_query
{
  "script": {
    "source": "if(ctx._source.count==100) return; else  ctx._source.count++",
    "lang": "painless"
  },
  "query": {  --> to filter docs
    "term": {
      "id": 2
    }
  }
}

Upvotes: 1

Related Questions