# Counting document frequency of keywords

**URL:** https://discuss.elastic.co/t/counting-document-frequency-of-keywords/100970
**Category:** Elasticsearch
**Created:** [September 19, 2017, 7:07am UTC](https://discuss.elastic.co/t/counting-document-frequency-of-keywords/100970 "2017-09-19T07:07:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Tommy\_Monk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tommy_monk/32/83712_2.png) [@Tommy\_Monk](https://discuss.elastic.co/u/Tommy_Monk)
#### Post date: [September 19, 2017, 7:07am UTC](https://discuss.elastic.co/t/counting-document-frequency-of-keywords/100970/1 "2017-09-19T07:07:19Z")

</div>

I'm performing some statistical analysis on keywords and have been stumped trying to query for count of occurrences.

Here's a simplified example of what I wish to achieve;

**Example Documents**

```
{ "Colour": "Red" }
{ "Colour": "Green" }
{ "Colour": "Blue" }
{ "Colour": "Red" }
{ "Colour": "Red" }

```

**Output Description**

Count of documents that have Colour only occuring in 1 document  
Count of documents that have Colour only occuring in 2 document  
...  
Count of documents that have Colour only occuring in 50+ document

**Output Example**

```
[
   {
      "Key": "1",
      "Count": 2 // Green and Blue only occur in 1 document
   },
   {
      "Key": "2",
      "Count": 0
   },
   {
      "Key": "3",
      "Count": 1 // Red occurs in 3 documents
   }
]

```

I would like the result to be constrained by a query, I've considered using a Terms agg with partitions and counting in code but this seems sub-optimal.

---

<div class="post-metadata">

### Author: ![Tommy\_Monk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tommy_monk/32/83712_2.png) [@Tommy\_Monk](https://discuss.elastic.co/u/Tommy_Monk)
#### Post date: [September 20, 2017, 9:24am UTC](https://discuss.elastic.co/t/counting-document-frequency-of-keywords/100970/2 "2017-09-20T09:24:17Z")

</div>

Is there a more appropriate place that I should have asked a query related question like this ?

---

<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: [October 18, 2017, 9:24am UTC](https://discuss.elastic.co/t/counting-document-frequency-of-keywords/100970/3 "2017-10-18T09:24:24Z")

</div>

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