# Need help with query to aggregate and get data across objects

**URL:** <https://discuss.elastic.co/t/need-help-with-query-to-aggregate-and-get-data-across-objects/357569>\
**Category:** Elasticsearch\
**Tags:** painless, aggregations\
**Created:** [April 17, 2024, 6:39am UTC](https://discuss.elastic.co/t/need-help-with-query-to-aggregate-and-get-data-across-objects/357569 "2024-04-17T06:39:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nser-melwin](https://avatars.discourse-cdn.com/v4/letter/n/8c91f0/32.png) [@nser-melwin](https://discuss.elastic.co/u/nser-melwin)\
**Post date:** [April 17, 2024, 6:39am UTC](https://discuss.elastic.co/t/need-help-with-query-to-aggregate-and-get-data-across-objects/357569/1 "2024-04-17T06:39:27Z")

</div>

I have data in an index in this format:

doc1

```auto
doc1
{
	ports: {
		"80": 2,
		"443": 10,
		"23": 12
	}
}

```

doc2

```auto
{
	ports: {
		"80": 32,
		"44": 6,
		"22": 1
	}
}

```

I am trying to query and get an aggregate of data which calculates the total sum of these ports across different documents and get me the total. Something like this as shown below.

```auto
{
	topPorts: {
		"80": 34,
		"443": 10,
		"23": 12,
		"44": 6,
		"22": 1
	}
}

```

I tried various queries, going through a lot of forums and couldn't get anything that gave me desired results. Is there any way this can be solved using the Elasticsearch query or is it that the only solution to this is using painless script.

I am using Elasticsearch 7.10.2
