# Grouping by a field and getting documents with lowest value for a given property

**URL:** <https://discuss.elastic.co/t/grouping-by-a-field-and-getting-documents-with-lowest-value-for-a-given-property/292185>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [December 16, 2021, 4:33pm UTC](https://discuss.elastic.co/t/grouping-by-a-field-and-getting-documents-with-lowest-value-for-a-given-property/292185 "2021-12-16T16:33:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgmullor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jgmullor/32/99131_2.png) [@jgmullor](https://discuss.elastic.co/u/jgmullor)\
**Post date:** [December 16, 2021, 4:33pm UTC](https://discuss.elastic.co/t/grouping-by-a-field-and-getting-documents-with-lowest-value-for-a-given-property/292185/1 "2021-12-16T16:33:50Z")

</div>

Hello!

I am trying to find in an index that contains multiple documents related to courses, having a structure like:

```json
[
   {
      "course_id":"ADE778",
      "subscriptions":91,
      "source":"page1.com"
   },
   {
      "course_id":"ADE778",
      "subscriptions":32,
      "source":"page2.com"
   },
   {
      "course_id":"ADE778",
      "subscriptions":14,
      "source":"page3.com"
   },
   {
      "course_id":"ADE778",
      "subscriptions":77,
      "source":"page4.com"
   },
   {
      "course_id":"ADE778",
      "subscriptions":92,
      "source":"page5.com"
   },
   {
      "course_id":"ADE778",
      "subscriptions":33,
      "source":"page6.com"
   },
   {
      "course_id":"45KFF2",
      "subscriptions":12,
      "source":"page7.com"
   },
   {
      "course_id":"45KFF2",
      "subscriptions":41,
      "source":"page8.com"
   },
   {
      "course_id":"45KFF2",
      "subscriptions":16,
      "source":"page9.com"
   },
   {
      "course_id":"45KFF2",
      "subscriptions":18,
      "source":"page10.com"
   },
   {
      "course_id":"45KFF2",
      "subscriptions":79,
      "source":"page11.com"
   },
   {
      "course_id":"45KFF2",
      "subscriptions":44,
      "source":"page12.com"
   }
]

```

What I was trying to get is getting for each `course_id`, the document with the highest `subscriptions` value, something like:

```json
[
   {
      "course_id":"ADE778",
      "subscriptions":92,
      "source":"page5.com"
   },
   {
      "course_id":"45KFF2",
      "subscriptions":79,
      "source":"page11.com"
   }
]

```

I was trying with something like this query:

```json
{
	"aggs": {
		"group_by_course_id": {
			"terms": {
				"field": "course_id.keyword"
			},
			"aggs": {
				"group_by_course_id": {
					"terms": {
						"field": "course_id.keyword"
					},
					"aggs": {
						"best_subscriptions": {
							"top_hits": {
								"size": 1,
								"sort": [
									{
										"subscriptions": {
											"order": "desc"
										}
									}
								]
							}
						}
					}
				}
			}
		}
	}
}

```

But I have almost 100K different `course_id` values, so I do not know what kind of aggregation should I be using.

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:** [January 13, 2022, 4:34pm UTC](https://discuss.elastic.co/t/grouping-by-a-field-and-getting-documents-with-lowest-value-for-a-given-property/292185/2 "2022-01-13T16:34:40Z")

</div>

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