Update the Merge_id field within aggregated Bucket using painless

I am new to elasticsearch and Painless, so need some guidance

I want to update values of Merge_ID in bucket result according to minimum of merge_ID in a bucket group members' and apply that rest of bucket members Merge_id using painless?

POST /index_test1/_update_by_query
{

			"aggs": {
				"GroupByType": {
        			"terms": {
             			"field": "FirstName.first_key"
            		},
            		"aggs": {
            			"Group": {
            				"top_hits":{
            					"size":12, 
            					"_source": {
            							"include": ["Email", "CustomerNo", "FirstName", "LastName", "Phoneno","Merge_ID"]
            					},
            					"sort":[{
    								"CustomerNo.keyword": {
    								"order": "asc"
    								}
    							}]
            				}
            			}
            		}
        		}
    		}
    	,

"size": 0,
"script_fields": {
"my_field_name" : {
"script": {
"lang": "painless",
"source": "int min = min(Merge_ID); ctx._source_Merge_ID=min;"
}
}
}
}

Kindly if someone knows how to do it.

This isn't going to work as you intend. An update by query request streams the results of the query back to be updated, thus we won't have the results of the aggregation to use. In fact, doing an aggregation inside an update_by_query request is completely useless; the results will never be seen (I created https://github.com/elastic/elasticsearch/issues/46192 to give better messaging up front here).

I suggest you run your aggregation in a separate search request, and then use the result to form your update_by_query request.

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