Update By Query Limitation

Got error when try to update the documents using update_by_query

[script] Too many dynamic script compilations within one minute, max: [15/min]; please use on-disk, 
indexed, or scripts with parameters instead; this limit can be changed by the
[script.max_compilations_per_minute

The query that used for updating is

update_query = {
		"query": {
			"match": {
				"user.id": user.id
			}
		},
		"script": {
			"inline": "ctx._source.user.subscriber_count = " + str(user.subscriber_count) +
				"; ctx._source.user.is_approved = true"
		}
	}

How to avoid this error ??

I'm assuming you're running this inside some kind of iterator over a number of users? The problem here is, is that you're sending Elasticsearch a unique script for each user (as user.subscriber_count will be different each time). This causes Elasticsearch to recompile the script with each request, and as a result you're exceeding the default max number of compilations per minute.

The solution is probably to rewrite your script using parameters:

update_query = {
  "query": {
    "match": {
      "user.id": user.id
    }
  },
  "script": {
    "inline": "ctx._source.user.subscriber_count = params.subscriber_count; ctx._source.user.is_approved = true",
    "params": {
      "subscriber_count": user.subscriber_count
    }
  }
}
2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.