# Update the Merge\_id field within aggregated Bucket using painless

**URL:** <https://discuss.elastic.co/t/update-the-merge-id-field-within-aggregated-bucket-using-painless/197286>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 29, 2019, 8:31am UTC](https://discuss.elastic.co/t/update-the-merge-id-field-within-aggregated-bucket-using-painless/197286 "2019-08-29T08:31:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tayyab80](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@Tayyab80](https://discuss.elastic.co/u/Tayyab80)\
**Post date:** [August 29, 2019, 8:31am UTC](https://discuss.elastic.co/t/update-the-merge-id-field-within-aggregated-bucket-using-painless/197286/1 "2019-08-29T08:31:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 30, 2019, 5:22pm UTC](https://discuss.elastic.co/t/update-the-merge-id-field-within-aggregated-bucket-using-painless/197286/2 "2019-08-30T17:22:10Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 27, 2019, 5:22pm UTC](https://discuss.elastic.co/t/update-the-merge-id-field-within-aggregated-bucket-using-painless/197286/3 "2019-09-27T17:22:11Z")

</div>

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