# Hit Counts within a Document

**URL:** https://discuss.elastic.co/t/hit-counts-within-a-document/19643
**Category:** Elasticsearch
**Created:** [September 5, 2014, 1:02pm UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643 "2014-09-05T13:02:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Darren\_Trzynka](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Darren\_Trzynka](https://discuss.elastic.co/u/Darren_Trzynka)
#### Post date: [September 5, 2014, 1:02pm UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643/1 "2014-09-05T13:02:14Z")

</div>

In our current application, it is important to know the number of times  
hits were found within a document for a given search. We are considering  
using elasticsearch but this is one area I have yet to find a solution for  
with elasticsearch. The only thing I have found remotely possible is  
getting the highlighted hits, then counting them. This of course could be  
rather time consuming possibly having to parse 1000s of documents just to  
get the number of hits within a document. The data is there of course  
within elasticsearch as it is able to highlight the hits, I just need to  
know how to get access to those hit numbers per document.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)
#### Post date: [September 5, 2014, 1:15pm UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643/2 "2014-09-05T13:15:49Z")

</div>

Hello Darren ,

What do you mean by number of hits ?  
Is it the number of occurrence of a term in a document ?

Thanks  
Vineeth

On Fri, Sep 5, 2014 at 6:32 PM, Darren Trzynka [darrentrzynka@gmail.com](mailto:darrentrzynka@gmail.com)  
wrote:

> In our current application, it is important to know the number of times  
> hits were found within a document for a given search. We are considering  
> using elasticsearch but this is one area I have yet to find a solution for  
> with elasticsearch. The only thing I have found remotely possible is  
> getting the highlighted hits, then counting them. This of course could be  
> rather time consuming possibly having to parse 1000s of documents just to  
> get the number of hits within a document. The data is there of course  
> within elasticsearch as it is able to highlight the hits, I just need to  
> know how to get access to those hit numbers per document.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5nkq53EzDCDBMvuDqnAa4MbWyJ6MyXYGUVnAgD0eSiHOw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5nkq53EzDCDBMvuDqnAa4MbWyJ6MyXYGUVnAgD0eSiHOw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)
#### Post date: [September 5, 2014, 1:37pm UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643/3 "2014-09-05T13:37:52Z")

</div>

Hello Darren ,

If its term frequency of a word that you are looking for , you can use  
script fields -

{  
"fields": [  
"text"  
],  
"query": {  
"term": {  
"text": "god"  
}  
},  
"script\_fields": {  
"tf": {  
"script": "\_index['text']['god'].tf()"  
}  
}  
}

SCRIPTING -

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

SCRIPT FIELDS -

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Thanks  
Vineeth

On Fri, Sep 5, 2014 at 6:45 PM, vineeth mohan [vm.vineethmohan@gmail.com](mailto:vm.vineethmohan@gmail.com)  
wrote:

> Hello Darren ,
> 
> What do you mean by number of hits ?  
> Is it the number of occurrence of a term in a document ?
> 
> Thanks  
> Vineeth
> 
> On Fri, Sep 5, 2014 at 6:32 PM, Darren Trzynka [darrentrzynka@gmail.com](mailto:darrentrzynka@gmail.com)  
> wrote:
> 
> > In our current application, it is important to know the number of times  
> > hits were found within a document for a given search. We are considering  
> > using elasticsearch but this is one area I have yet to find a solution for  
> > with elasticsearch. The only thing I have found remotely possible is  
> > getting the highlighted hits, then counting them. This of course could be  
> > rather time consuming possibly having to parse 1000s of documents just to  
> > get the number of hits within a document. The data is there of course  
> > within elasticsearch as it is able to highlight the hits, I just need to  
> > know how to get access to those hit numbers per document.
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8e7a6b6c-a4c4-4d41-8fa4-035b19341c69%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mgG2Sh50A64FWCfZp7Abwa1qX\_qs5k-qUx1pJowpp2uQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mgG2Sh50A64FWCfZp7Abwa1qX_qs5k-qUx1pJowpp2uQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Darren\_Trzynka](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Darren\_Trzynka](https://discuss.elastic.co/u/Darren_Trzynka)
#### Post date: [September 8, 2014, 12:54pm UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643/4 "2014-09-08T12:54:36Z")

</div>

Vineeth,  
Thanks for responding. What I am looking for is provided I perform a  
search for various terms, how given the search result can I understand the  
frequency of the hits within documents. For example, I perform a full text  
search on cat. 5 documents are returned. I could today get the terms that  
were found highlighted but that is of course quite nasty. Instead what I  
would like returned is the documents but something like for each document  
saying:  
Document 1 (group: 1): cat - 5  
Document 2 (group: 2): cat - 3  
Document 3 (group: 1): cat - 2  
...  
Document n - cat - #

Also, there is other metadata that it would be nice to aggregate on too so  
I could get an answer for the above scenario:  
group : 1 - cat - 7  
group : 2 - cat - 3

Thanks  
Darren

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)
#### Post date: [September 8, 2014, 4:28pm UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643/5 "2014-09-08T16:28:46Z")

</div>

Hello Darren ,

Following query does what you have asked for ( replace FIELD with the field  
you are looking for) -

{  
"fields": [  
"text"  
],  
"query": {  
"term": {  
"text": "god"  
}  
},  
"script\_fields": {  
"tf": {  
"script": "\_index['FIELD']['cat'].tf()"  
}  
}  
}

For the second one , use -

{  
"query": {  
"term": {  
"FIELD": "CAT"  
}  
},  
"aggs": {  
"groupName": {  
"terms": {  
"field": "GROUP\_FIELD"  
},  
"aggs": {  
"catStats": {  
"sum": {  
"script": "\_index['FIELD']['CAT'].tf()"  
}  
}  
}  
}  
}  
}

Thanks  
Vineeth

On Mon, Sep 8, 2014 at 6:24 PM, Darren Trzynka [darrentrzynka@gmail.com](mailto:darrentrzynka@gmail.com)  
wrote:

> Vineeth,  
> Thanks for responding. What I am looking for is provided I perform a  
> search for various terms, how given the search result can I understand the  
> frequency of the hits within documents. For example, I perform a full text  
> search on cat. 5 documents are returned. I could today get the terms that  
> were found highlighted but that is of course quite nasty. Instead what I  
> would like returned is the documents but something like for each document  
> saying:  
> Document 1 (group: 1): cat - 5  
> Document 2 (group: 2): cat - 3  
> Document 3 (group: 1): cat - 2  
> ...  
> Document n - cat - #
> 
> Also, there is other metadata that it would be nice to aggregate on too so  
> I could get an answer for the above scenario:  
> group : 1 - cat - 7  
> group : 2 - cat - 3
> 
> Thanks  
> Darren
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK\_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Darren\_Trzynka](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Darren\_Trzynka](https://discuss.elastic.co/u/Darren_Trzynka)
#### Post date: [September 9, 2014, 3:57pm UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643/6 "2014-09-09T15:57:24Z")

</div>

Vineeth,  
I just saw your response today and I came to the same conclusion yesterday  
after you gave me a nice working example! I took it a step further doing  
the same grouping by the field that you did and it came out nicely.  
Something is sinking in anyways with me..-)

Besides some possible language support issues, the biggest thing I see for  
challenges could be if stemming is involved (you search for federal and  
hits are returned on federal, federalizing, etc... so if you just look for  
federal in the term count, it wouldn't find all the matches) and then  
dealing with case sensitivity when looking at the term frequencies (the  
user typed in "Federal cases" which matches by default on federal and  
cases) it seems you would need to lower case the lookup for the term  
frequencies. What do you think about these cases?

Thanks!  
Darren

On Mon, Sep 8, 2014 at 11:28 AM, vineeth mohan [vm.vineethmohan@gmail.com](mailto:vm.vineethmohan@gmail.com)  
wrote:

> Hello Darren ,
> 
> Following query does what you have asked for ( replace FIELD with the  
> field you are looking for) -
> 
> {  
> "fields": [  
> "text"  
> ],  
> "query": {  
> "term": {  
> "text": "god"  
> }  
> },  
> "script\_fields": {  
> "tf": {  
> "script": "\_index['FIELD']['cat'].tf()"  
> }  
> }  
> }
> 
> For the second one , use -
> 
> {  
> "query": {  
> "term": {  
> "FIELD": "CAT"  
> }  
> },  
> "aggs": {  
> "groupName": {  
> "terms": {  
> "field": "GROUP\_FIELD"  
> },  
> "aggs": {  
> "catStats": {  
> "sum": {  
> "script": "\_index['FIELD']['CAT'].tf()"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Thanks  
> Vineeth
> 
> On Mon, Sep 8, 2014 at 6:24 PM, Darren Trzynka [darrentrzynka@gmail.com](mailto:darrentrzynka@gmail.com)  
> wrote:
> 
> > Vineeth,  
> > Thanks for responding. What I am looking for is provided I perform a  
> > search for various terms, how given the search result can I understand the  
> > frequency of the hits within documents. For example, I perform a full text  
> > search on cat. 5 documents are returned. I could today get the terms that  
> > were found highlighted but that is of course quite nasty. Instead what I  
> > would like returned is the documents but something like for each document  
> > saying:  
> > Document 1 (group: 1): cat - 5  
> > Document 2 (group: 2): cat - 3  
> > Document 3 (group: 1): cat - 2  
> > ...  
> > Document n - cat - #
> > 
> > Also, there is other metadata that it would be nice to aggregate on too  
> > so I could get an answer for the above scenario:  
> > group : 1 - cat - 7  
> > group : 2 - cat - 3
> > 
> > Thanks  
> > Darren
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/vRxbDxqjxVg/unsubscribe](https://groups.google.com/d/topic/elasticsearch/vRxbDxqjxVg/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK\_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK\_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAFkmSJ-8joV%3DzAu6rRNU9GQ100yzGYXqY4QG2gAod1pniu5qzw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAFkmSJ-8joV%3DzAu6rRNU9GQ100yzGYXqY4QG2gAod1pniu5qzw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)
#### Post date: [September 10, 2014, 10:41am UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643/7 "2014-09-10T10:41:16Z")

</div>

Hello Darren ,

I am glad that my solution worked for you.

The approach there is to use multi fields.  
One field , keep the raw data by declaring the analyzer as not\_analyzed.  
Example is sited in this link -

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Thanks  
Vineeth

On Tue, Sep 9, 2014 at 9:27 PM, Darren Trzynka [darrentrzynka@gmail.com](mailto:darrentrzynka@gmail.com)  
wrote:

> Vineeth,  
> I just saw your response today and I came to the same conclusion yesterday  
> after you gave me a nice working example! I took it a step further doing  
> the same grouping by the field that you did and it came out nicely.  
> Something is sinking in anyways with me..-)
> 
> Besides some possible language support issues, the biggest thing I see for  
> challenges could be if stemming is involved (you search for federal and  
> hits are returned on federal, federalizing, etc... so if you just look for  
> federal in the term count, it wouldn't find all the matches) and then  
> dealing with case sensitivity when looking at the term frequencies (the  
> user typed in "Federal cases" which matches by default on federal and  
> cases) it seems you would need to lower case the lookup for the term  
> frequencies. What do you think about these cases?
> 
> Thanks!  
> Darren
> 
> On Mon, Sep 8, 2014 at 11:28 AM, vineeth mohan [vm.vineethmohan@gmail.com](mailto:vm.vineethmohan@gmail.com)  
> wrote:
> 
> > Hello Darren ,
> > 
> > Following query does what you have asked for ( replace FIELD with the  
> > field you are looking for) -
> > 
> > {  
> > "fields": [  
> > "text"  
> > ],  
> > "query": {  
> > "term": {  
> > "text": "god"  
> > }  
> > },  
> > "script\_fields": {  
> > "tf": {  
> > "script": "\_index['FIELD']['cat'].tf()"  
> > }  
> > }  
> > }
> > 
> > For the second one , use -
> > 
> > {  
> > "query": {  
> > "term": {  
> > "FIELD": "CAT"  
> > }  
> > },  
> > "aggs": {  
> > "groupName": {  
> > "terms": {  
> > "field": "GROUP\_FIELD"  
> > },  
> > "aggs": {  
> > "catStats": {  
> > "sum": {  
> > "script": "\_index['FIELD']['CAT'].tf()"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > Thanks  
> > Vineeth
> > 
> > On Mon, Sep 8, 2014 at 6:24 PM, Darren Trzynka [darrentrzynka@gmail.com](mailto:darrentrzynka@gmail.com)  
> > wrote:
> > 
> > > Vineeth,  
> > > Thanks for responding. What I am looking for is provided I perform a  
> > > search for various terms, how given the search result can I understand the  
> > > frequency of the hits within documents. For example, I perform a full text  
> > > search on cat. 5 documents are returned. I could today get the terms that  
> > > were found highlighted but that is of course quite nasty. Instead what I  
> > > would like returned is the documents but something like for each document  
> > > saying:  
> > > Document 1 (group: 1): cat - 5  
> > > Document 2 (group: 2): cat - 3  
> > > Document 3 (group: 1): cat - 2  
> > > ...  
> > > Document n - cat - #
> > > 
> > > Also, there is other metadata that it would be nice to aggregate on too  
> > > so I could get an answer for the above scenario:  
> > > group : 1 - cat - 7  
> > > group : 2 - cat - 3
> > > 
> > > Thanks  
> > > Darren
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/76044495-afc9-4c51-b3f3-6ea7e636bc01%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/vRxbDxqjxVg/unsubscribe](https://groups.google.com/d/topic/elasticsearch/vRxbDxqjxVg/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK\_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK\_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DfRzwke0GK_pn8WxPBJ6c%2B97yOyDPmkXcWkQJf%3Dy5rfA%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAFkmSJ-8joV%3DzAu6rRNU9GQ100yzGYXqY4QG2gAod1pniu5qzw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAFkmSJ-8joV%3DzAu6rRNU9GQ100yzGYXqY4QG2gAod1pniu5qzw%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAFkmSJ-8joV%3DzAu6rRNU9GQ100yzGYXqY4QG2gAod1pniu5qzw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAFkmSJ-8joV%3DzAu6rRNU9GQ100yzGYXqY4QG2gAod1pniu5qzw%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mqWNojht2KxBtQQgTrnFg1j%3D4VUuYbKkYwUJkAaX%2B%2BTA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mqWNojht2KxBtQQgTrnFg1j%3D4VUuYbKkYwUJkAaX%2B%2BTA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:03am UTC](https://discuss.elastic.co/t/hit-counts-within-a-document/19643/8 "2017-07-06T01:03:24Z")

</div>


