# Efficiency of aggregation in my use cases?

**URL:** https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012
**Category:** Elasticsearch
**Created:** [August 6, 2015, 10:51pm UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012 "2015-08-06T22:51:30Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)
#### Post date: [August 6, 2015, 10:51pm UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012/1 "2015-08-06T22:51:30Z")

</div>

Hello Elastic Search experts,

I have a tens of million records, which is customer ID and city ID pair. There are tens of millions of unique customer ID, and only a few hundreds unique city ID. I want to do a merge to get all city ID aggregated for a specific customer ID, and pull back all records. Wondering how to do this efficiently through ElasticSearch?

Here is an example of inputs,

CustomerID1 City1  
CustomerID2 City2  
CustomerID3 City1  
CustomerID1 City3  
CustomerID2 City4

I want output like (I want to pull all CityID aggregated records, not a record for a specific user, and wondering the most efficient way to do this),

CustomerID1 City1 City3  
CustomerID2 City2 City4  
CustomerID3 City1

thanks in advance,  
Lin

---

<div class="post-metadata">

### Author: ![kresimirus](https://avatars.discourse-cdn.com/v4/letter/k/cdc98d/32.png) [@kresimirus](https://discuss.elastic.co/u/kresimirus)
#### Post date: [August 10, 2015, 10:26pm UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012/2 "2015-08-10T22:26:16Z")

</div>

I don't think there is much you can do: create aggregation on customer field and inside of it nest aggregation for city. Something like this:

```
"aggs": {
    "customers": {
        "terms": {
            "field": "customer"
            "size": 0
        },
        "aggs": {
            "cities": {
                "terms": {
                    "size": 0,
                    "field": "city"
                }
            }
        }
    }
}

```

And fetch [only aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/1.6/search-aggregations.html#_returning_only_aggregation_results)

---

<div class="post-metadata">

### Author: ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)
#### Post date: [August 13, 2015, 10:36pm UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012/3 "2015-08-13T22:36:20Z")

</div>

Thanks @kresimirus, in your query, size:0 means?

---

<div class="post-metadata">

### Author: ![kresimirus](https://avatars.discourse-cdn.com/v4/letter/k/cdc98d/32.png) [@kresimirus](https://discuss.elastic.co/u/kresimirus)
#### Post date: [August 14, 2015, 2:36pm UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012/4 "2015-08-14T14:36:16Z")

</div>

Default aggregation size is 10, so without size 0 in your case you would get top 10 customers appearing in most pairs and for every one of them top 10 cities. Size 0 means that you specify no limit for aggregation (if that is what you want).

---

<div class="post-metadata">

### Author: ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)
#### Post date: [August 15, 2015, 7:44am UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012/5 "2015-08-15T07:44:12Z")

</div>

@kresimirus, thanks for the response. I did some study for search today, I do not quite catch what means "This means that if the number of unique terms is greater than size, the returned list is slightly off and not accurate (it could be that the term counts are slightly off and it could even be that a term that should have been in the top size buckets was not returned)"? If you could advise or show an example, it will be great. 😄

Have a good weekend, and this is what I am referring from,

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#\_size?q=size](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_size?q=size)

---

<div class="post-metadata">

### Author: ![kresimirus](https://avatars.discourse-cdn.com/v4/letter/k/cdc98d/32.png) [@kresimirus](https://discuss.elastic.co/u/kresimirus)
#### Post date: [August 17, 2015, 5:14pm UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012/6 "2015-08-17T17:14:26Z")

</div>

You have example in the same article one sentence after the one you quoted 😄

---

<div class="post-metadata">

### Author: ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)
#### Post date: [August 18, 2015, 1:47am UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012/7 "2015-08-18T01:47:36Z")

</div>

Thanks @kresimirus 😊

Where it is mentioned default size is 10? I did some search and cannot figure out. 😄

regards,  
Lin

---

<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 5, 2017, 11:55pm UTC](https://discuss.elastic.co/t/efficiency-of-aggregation-in-my-use-cases/27012/8 "2017-07-05T23:55:26Z")

</div>


