# Count Occurrence of Text in Inner Document

**URL:** <https://discuss.elastic.co/t/count-occurrence-of-text-in-inner-document/5768>\
**Category:** Elasticsearch\
**Created:** [November 4, 2011, 9:38am UTC](https://discuss.elastic.co/t/count-occurrence-of-text-in-inner-document/5768 "2011-11-04T09:38:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davet](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@davet](https://discuss.elastic.co/u/davet)\
**Post date:** [November 4, 2011, 9:38am UTC](https://discuss.elastic.co/t/count-occurrence-of-text-in-inner-document/5768/1 "2011-11-04T09:38:31Z")

</div>

Hi,

I wonder if this is possible. Given a number of documents:

{  
Age: 22  
Hobbies: [  
{  
Name: "golf"  
},  
{  
Name: "horse racing"  
},  
{  
Name: "car racing"  
}  
]  
}

Is it possible to count the number of times the word "racing" appears  
in the Name field across all documents? I can query easily enough to  
find all the documents that contain "racing" but I can't see how to  
count the occurrences.

Thanks

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 8, 2011, 3:05am UTC](https://discuss.elastic.co/t/count-occurrence-of-text-in-inner-document/5768/2 "2011-11-08T03:05:35Z")

</div>

Thats a bit tricky, cause there are two aspects here. Lets first assume you  
have a single Name field (not an array of them). In this case, you can use  
terms facet to get the counts for the terms the Name field has. Note  
though, that it ends up loading all terms to memory, so it can be expensive.

The other aspect is the fact that Name has multiple values per document,  
and the question is if racing exists in two names within the same document  
should be counted as 1 or 2. If its 2, then terms facet will work as is. If  
it should count as 1, it gets a bit more complicated, so ping if we need to  
go there (you basically have two potions, one of them is to use terms  
stats, another is to mark Hobbies as a nested document in the mapping).

On Fri, Nov 4, 2011 at 11:38 AM, mordant [asmint3@hotmail.co.uk](mailto:asmint3@hotmail.co.uk) wrote:

> Hi,
> 
> I wonder if this is possible. Given a number of documents:
> 
> {  
> Age: 22  
> Hobbies: [  
> {  
> Name: "golf"  
> },  
> {  
> Name: "horse racing"  
> },  
> {  
> Name: "car racing"  
> }  
> ]  
> }
> 
> Is it possible to count the number of times the word "racing" appears  
> in the Name field across all documents? I can query easily enough to  
> find all the documents that contain "racing" but I can't see how to  
> count the occurrences.
> 
> 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 6, 2017, 3:49am UTC](https://discuss.elastic.co/t/count-occurrence-of-text-in-inner-document/5768/3 "2017-07-06T03:49:39Z")

</div>


