# Reduce number of results according to max result per field

**URL:** https://discuss.elastic.co/t/reduce-number-of-results-according-to-max-result-per-field/246946
**Category:** Elasticsearch
**Created:** [August 31, 2020, 12:14pm UTC](https://discuss.elastic.co/t/reduce-number-of-results-according-to-max-result-per-field/246946 "2020-08-31T12:14:45Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![emr](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@emr](https://discuss.elastic.co/u/emr)
#### Post date: [August 31, 2020, 12:14pm UTC](https://discuss.elastic.co/t/reduce-number-of-results-according-to-max-result-per-field/246946/1 "2020-08-31T12:14:45Z")

</div>

We have an index that has over 100 million data. it has a structure like this:

```auto
{
	// ...
	"first_name": "John",
	"last_name": "Doe",
	"company": "X Company",
	// ...
}

```

We want to return results let's say 3 items per company eventhough there are much more records than 3.

Here is the records:

```auto
first_name, last_name, company
John, Doe, X Company
Jane, Doe, X Company
George, Doe, X Company
William, Doe, X Company
Jack, Doe, X Company
Ellen, Doe, Y Company
Harper, Doe, Z Company
Mason, Doe, Z Company
Ella, Doe, Z Company
Scarlett, Doe, Z Company

```

Here is the expected query result:

```auto
first_name, last_name, company
John, Doe, X Company
Jane, Doe, X Company
George, Doe, X Company
Ellen, Doe, Y Company
Harper, Doe, Z Company
Mason, Doe, Z Company
Ella, Doe, Z Company

```

How can we write this kind of query? We don't want to use terms aggregation with a top hits sub-aggregation since we have 100 million records. Or do you have any idea for a performant aggregation for this kind of huge data?

---

<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 28, 2020, 12:14pm UTC](https://discuss.elastic.co/t/reduce-number-of-results-according-to-max-result-per-field/246946/2 "2020-09-28T12:14:45Z")

</div>

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