# Applying aggregation to query results

**URL:** <https://discuss.elastic.co/t/applying-aggregation-to-query-results/283042>\
**Category:** Elasticsearch\
**Created:** [September 1, 2021, 12:39pm UTC](https://discuss.elastic.co/t/applying-aggregation-to-query-results/283042 "2021-09-01T12:39:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![me.mohammed](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@me.mohammed](https://discuss.elastic.co/u/me.mohammed)\
**Post date:** [September 1, 2021, 12:39pm UTC](https://discuss.elastic.co/t/applying-aggregation-to-query-results/283042/1 "2021-09-01T12:39:49Z")

</div>

Hello everyone, I'm trying to apply an aggregation filter to my query, I basically have an array of objects which looks like this

```auto
{
  "productDeals": [
	{
	  "id": 1,
      "startAt": "2021-08-30T17:40:14.000Z",
	  "endAt": "2021-11-30T21:00:00.000Z",
      "createdAt": "2021-08-30T17:40:14.179Z",
	  "modifiedAt": "2021-08-30T17:40:14.179Z",
	},
	{
	  "id": 1,
      "startAt": "2021-08-30T17:40:14.000Z",
	  "endAt": "2022-10-30T21:00:00.000Z",
      "createdAt": "2021-08-30T17:40:14.179Z",
	  "modifiedAt": "2021-08-30T17:40:14.179Z",
	}
  ]
}

```

I'm trying to get the productDeal with the furthest endAt date, I was able to do so using this query

```auto
{
	"query": {
		"bool": {
			"should": [{
				"match": {
					"id": 10
				}
			}]
		}
	},
	"aggs": {
		"productDeals": {
			"nested": {
				"path": "productDeals"
			},
			"aggs": {
				"max_productDeals": {
					"max": {
						"field": "productDeals.endAt"
					}
				}
			}
		}
	}
}

```

This query works and gives the following output

```auto
{
	"product": {
		"id": 10,
		"whatever": "whatever",
		"productDeals": [{
				"id": 1,
				"startAt": "2021-08-30T17:40:14.000Z",
				"endAt": "2021-11-30T21:00:00.000Z",
				"createdAt": "2021-08-30T17:40:14.179Z",
				"modifiedAt": "2021-08-30T17:40:14.179Z"
			},
			{
				"id": 1,
				"startAt": "2021-08-30T17:40:14.000Z",
				"endAt": "2022-10-30T21:00:00.000Z",
				"createdAt": "2021-08-30T17:40:14.179Z",
				"modifiedAt": "2021-08-30T17:40:14.179Z"
			}
		]
	}
}

```

Then at the end it adds on this

```auto
 "aggregations" : {
    "productDeals" : {
      "doc_count" : 2,
      "max_productDeals" : {
        "value" : 1.669842E12,
        "value_as_string" : "2022-11-30T21:00:00.000Z"
      }
    }
  }
}

```

This is not helpful for me, instead I want to apply it to the document, I want the result to be

```auto
product: {
id....

productDeal: [
//The deal with the furthest endDate here
]
}
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 1, 2021, 2:52pm UTC](https://discuss.elastic.co/t/applying-aggregation-to-query-results/283042/2 "2021-09-01T14:52:22Z")

</div>

If you only need the top document, do you really need an aggregation or can you just return a single document, sorted by your `endAt` field?

---

<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 29, 2021, 2:52pm UTC](https://discuss.elastic.co/t/applying-aggregation-to-query-results/283042/3 "2021-09-29T14:52:51Z")

</div>

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