# How to use the aggregation buckets to update the documents

**URL:** <https://discuss.elastic.co/t/how-to-use-the-aggregation-buckets-to-update-the-documents/287809>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 27, 2021, 2:38pm UTC](https://discuss.elastic.co/t/how-to-use-the-aggregation-buckets-to-update-the-documents/287809 "2021-10-27T14:38:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rui\_Goncalves](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rui_goncalves/32/72197_2.png) [@Rui\_Goncalves](https://discuss.elastic.co/u/Rui_Goncalves)\
**Post date:** [October 27, 2021, 2:38pm UTC](https://discuss.elastic.co/t/how-to-use-the-aggregation-buckets-to-update-the-documents/287809/1 "2021-10-27T14:38:37Z")

</div>

Hi, I'm new to painless and needed some assistance.  
Having this query :

```auto
GET index1/_search/
{
  "size": 0,
  "aggs": {
    "attrs_root": {
      "nested": {
        "path": "business_index_jd_list_agg"
      },
      "aggs": {
        "attrs": {
          "terms": {
            "field": "jdl_id"
          },
          "aggs": {
     "sumOfQuantity" : {
        "sum" : {
           "field" : "value"
              }
            }
          }
        }
      }
    }
  }
}

```

and these results from that query :

```auto
[...] 
aggregations" : {
    "attrs_root" : {
      "doc_count" : 5,
      "attrs" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : -666,
            "doc_count" : 1,
            "sumOfQuantity" : {
              "value" : 55.0
            }
          },
          {
            "key" : 93,
            "doc_count" : 1,
            "sumOfQuantity" : {
              "value" : 25.0
            }
          },
          {
            "key" : 94,
            "doc_count" : 1,
            "sumOfQuantity" : {
              "value" : 35.0
            }
          },
          {
            "key" : 112,
            "doc_count" : 1,
            "sumOfQuantity" : {
              "value" : 45.0
            }
          },
          {
            "key" : 158,
            "doc_count" : 1,
            "sumOfQuantity" : {
              "value" : 40.0
            }
          }
        ]
      }
    }
  }
}

```

How can I use that query and navigate through those results using a painless script to achieve to update each document in the index with that agregated info. Something like this:

```auto
          {
            "jdl_id" : -666,
              "value" : 55.0
            }
          },
          {
            "jdl_id" : 93,
              "value" : 25.0
            }
          },
          {
            "jdl_id" : 94,
              "value" : 35.0
            }
          },
          {
            "jdl_id" : 112,
              "value" : 45.0
            }
          },
          {
            "jdl_id" : 158,
              "value" : 40.0
            }

```

Thank you.

---

<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:** [November 24, 2021, 2:39pm UTC](https://discuss.elastic.co/t/how-to-use-the-aggregation-buckets-to-update-the-documents/287809/2 "2021-11-24T14:39:36Z")

</div>

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