Elastic Search text field aggregations

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

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?

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.

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.

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 ?

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"
			}
		  }
		}
	  }
	}
  }
}

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 ?

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.

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