# Elastic Search text field aggregations

**URL:** <https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244>\
**Category:** Elasticsearch\
**Created:** [May 26, 2020, 5:43am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244 "2020-05-26T05:43:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naveena1](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Naveena1](https://discuss.elastic.co/u/Naveena1)\
**Post date:** [May 26, 2020, 5:43am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/1 "2020-05-26T05:43:12Z")

</div>

Is there anyway possible to find min or max aggregation on text fields when we group by some fields

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 26, 2020, 5:46am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/2 "2020-05-26T05:46:09Z")

</div>

It would help if you provided an example of what you are looking for here as it is not entirely clear. What is mix and max in the context of a text field?

---

<div class="post-metadata">

**Author:** ![Naveena1](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Naveena1](https://discuss.elastic.co/u/Naveena1)\
**Post date:** [May 26, 2020, 6:18am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/3 "2020-05-26T06:18:04Z")

</div>

I have Employee table with name and department as columns.

I am trying to group by dept by just selecting the min(name)  
like below

Select min(emp\_name), dept from employee group by dept.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 26, 2020, 8:03am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/4 "2020-05-26T08:03:33Z")

</div>

If you want a unique list of department names just use the ‘terms’ aggregation on the department field - set the ‘size’ parameter to a larger number or you’ll just get the default of ten results.

---

<div class="post-metadata">

**Author:** ![Naveena1](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Naveena1](https://discuss.elastic.co/u/Naveena1)\
**Post date:** [June 8, 2020, 5:48am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/5 "2020-06-08T05:48:05Z")

</div>

Thank you @Mark_Harwood for the instant reply. It helped me and I am able to use term aggregation on the department and get unique department names.

When I am fetching the department name, I would also like to fetch the minimum of employee name along with the department name.

Can I do min or max aggregations on text/ keyword fields ?. Can you please help me on this ?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 8, 2020, 9:34am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/6 "2020-06-08T09:34:33Z")

</div>

That would be another nested terms aggregation inside the department buckets, so -

```
{
  "aggs": {
	"departments": {
	  "terms": {
		"field": "department"
	  },
	  "aggs":{
		"first_employee":{
		  "terms":{
			"field": "employee_name",
			"order": {
			  "_term": "asc"
			}
		  }
		}
	  }
	}
  }
}
```

---

<div class="post-metadata">

**Author:** ![Naveena1](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Naveena1](https://discuss.elastic.co/u/Naveena1)\
**Post date:** [June 8, 2020, 11:01am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/7 "2020-06-08T11:01:13Z")

</div>

Thank you sir.

But nested aggregation for employee inside the departments aggregations will be equal to the below query ?

Select min(emp\_name), dept from employee group by dept.

I wanted to know if we can do minimum / maximum aggregation on keyword fields ?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 8, 2020, 11:03am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/8 "2020-06-08T11:03:10Z")

</div>

Ah. I forgot to add `"size":1` in the `first_employee` aggregation - that should give you the min rather than the default size=10 which would give you the first ten.

---

<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:** [July 6, 2020, 11:03am UTC](https://discuss.elastic.co/t/elastic-search-text-field-aggregations/234244/9 "2020-07-06T11:03:15Z")

</div>

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