# Query term aggregation to count occurence

**URL:** <https://discuss.elastic.co/t/query-term-aggregation-to-count-occurence/61719>\
**Category:** Elasticsearch\
**Created:** [September 28, 2016, 3:19pm UTC](https://discuss.elastic.co/t/query-term-aggregation-to-count-occurence/61719 "2016-09-28T15:19:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bellovitello](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@Bellovitello](https://discuss.elastic.co/u/Bellovitello)\
**Post date:** [September 28, 2016, 3:19pm UTC](https://discuss.elastic.co/t/query-term-aggregation-to-count-occurence/61719/1 "2016-09-28T15:19:16Z")

</div>

Hi,  
I'm trying to create a query to count string and aggregate based on occurence but I can't figure out how. I have a collection of documents containing only a time field and string. Here's the simple mapping:

```
"document":
{ "time": {
        "format": "strict_date_optional_time||epoch_millis",
        "type": "date"
      },
  "name": {
            "index": "not_analyzed",
            "type": "string"
          }
}

```

The name can contains the same values once or more, I would like to know how many "names" are present once, how many are present twice and so on for every day. Is it possible with only one query?

I tried with this query:

```
{
  "aggs":{
    "3":{
      "terms":{
        "field":"name"
      },
      "aggs":{
        "2":{
          "date_histogram":{
            "interval":"1d",
            "field":"time"
          }
        }
      }
    }
  }
}

```

I can only obtain a list of names with their occurence in the period like:

DAY 1  
A: 2 times  
B: 1 times  
C: 2 times

But I wold like to obtain somthing like:

DAY 1  
100 names are present 1 times  
50 names are present 2 times  
1 names is present 3 times

Thanks!

---

<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, 10:16pm UTC](https://discuss.elastic.co/t/query-term-aggregation-to-count-occurence/61719/2 "2017-07-05T22:16:35Z")

</div>


