# Elasticsearch phraze term frequency .tf() containing multiple words

**URL:** <https://discuss.elastic.co/t/elasticsearch-phraze-term-frequency-tf-containing-multiple-words/20458>\
**Category:** Elasticsearch\
**Created:** [October 28, 2014, 9:36am UTC](https://discuss.elastic.co/t/elasticsearch-phraze-term-frequency-tf-containing-multiple-words/20458 "2014-10-28T09:36:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![valerij\_vasilcenko](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@valerij\_vasilcenko](https://discuss.elastic.co/u/valerij_vasilcenko)\
**Post date:** [October 28, 2014, 9:36am UTC](https://discuss.elastic.co/t/elasticsearch-phraze-term-frequency-tf-containing-multiple-words/20458/1 "2014-10-28T09:36:42Z")

</div>

I want to access frequency of a phraze combined from multiple words e.g.  
"green energy"

I can access tf of "green" and "energy", example:

"function\_score":  
{  
"filter" : {  
"terms" : { "content" : ["energy","green"]}  
},  
"script\_score": {  
"script": "\_index['content']['energy'].tf() +  
\_index['content']['green'].tf()",  
"lang":"groovy"  
}  
}

This works fine. However, how can I find the frequency of a term "green  
energy" as

\_index['content']['green energy'].tf() does not work

--  
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/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%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:** [October 28, 2014, 9:59am UTC](https://discuss.elastic.co/t/elasticsearch-phraze-term-frequency-tf-containing-multiple-words/20458/2 "2014-10-28T09:59:46Z")

</div>

Hello Valergi ,

This wont work , normally becuase the string would be tokenized into green  
and energy.  
If you use shingle token filter and set it as 2 , it might work.  
Or in this case , you can see the position value of both the token using  
the script and if its next to each other , you can take it as an  
occurrence.

Thanks  
Vineeth

On Tue, Oct 28, 2014 at 3:06 PM, [valerij.vasilcenko@googlemail.com](mailto:valerij.vasilcenko@googlemail.com) wrote:

> I want to access frequency of a phraze combined from multiple words e.g.  
> "green energy"
> 
> I can access tf of "green" and "energy", example:
> 
> "function\_score":  
> {  
> "filter" : {  
> "terms" : { "content" : ["energy","green"]}  
> },  
> "script\_score": {  
> "script": "\_index['content']['energy'].tf() +  
> \_index['content']['green'].tf()",  
> "lang":"groovy"  
> }  
> }
> 
> This works fine. However, how can I find the frequency of a term "green  
> energy" as
> 
> \_index['content']['green energy'].tf() does not work
> 
> --  
> 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/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%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/CAGdPd5mjK%3DbgdSEZvrsfz5d\_HnN8BTrJ5d9O4yAHQuOODE4YWQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mjK%3DbgdSEZvrsfz5d_HnN8BTrJ5d9O4yAHQuOODE4YWQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![barry](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@barry](https://discuss.elastic.co/u/barry)\
**Post date:** [October 29, 2014, 8:45am UTC](https://discuss.elastic.co/t/elasticsearch-phraze-term-frequency-tf-containing-multiple-words/20458/3 "2014-10-29T08:45:29Z")

</div>

You can also look at developing a custom analyzer so that your phrase is  
not broken up at white space.

> **[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.

Selecting the correct combination of char filters and tokenizers will  
retain phrases.

On Tuesday, October 28, 2014 10:00:01 AM UTC, vineeth mohan wrote:

> Hello Valergi ,
> 
> This wont work , normally becuase the string would be tokenized into green  
> and energy.  
> If you use shingle token filter and set it as 2 , it might work.  
> Or in this case , you can see the position value of both the token using  
> the script and if its next to each other , you can take it as an  
> occurrence.
> 
> Thanks  
> Vineeth
> 
> On Tue, Oct 28, 2014 at 3:06 PM, \<[valerij.v...@googlemail.com](mailto:valerij.v...@googlemail.com)  
> \<javascript:\>\> wrote:
> 
> > I want to access frequency of a phraze combined from multiple words e.g.  
> > "green energy"
> > 
> > I can access tf of "green" and "energy", example:
> > 
> > "function\_score":  
> > {  
> > "filter" : {  
> > "terms" : { "content" : ["energy","green"]}  
> > },  
> > "script\_score": {  
> > "script": "\_index['content']['energy'].tf() +  
> > \_index['content']['green'].tf()",  
> > "lang":"groovy"  
> > }  
> > }
> > 
> > This works fine. However, how can I find the frequency of a term "green  
> > energy" as
> > 
> > \_index['content']['green energy'].tf() does not work
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%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/8e5795d8-c5ec-4a18-a356-ccc4a7e13e43%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8e5795d8-c5ec-4a18-a356-ccc4a7e13e43%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![barry](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@barry](https://discuss.elastic.co/u/barry)\
**Post date:** [October 29, 2014, 9:15am UTC](https://discuss.elastic.co/t/elasticsearch-phraze-term-frequency-tf-containing-multiple-words/20458/4 "2014-10-29T09:15:01Z")

</div>

You can also look at developing a custom analyzer so that your phrase is  
not broken up at white space when indexed.

> **[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.

Selecting the correct combination of char filters and tokenizers will  
retain phrases.

For example, using the whitespace analyzer will separate on whitespace:

curl '192.168.w.xyz:9200/test/\_analyze?pretty=1&analyzer=whitespace' -d  
'foo bar baz'  
{  
"tokens" : [ {  
"token" : "foo",  
"start\_offset" : 0,  
"end\_offset" : 3,  
"type" : "word",  
"position" : 1  
}, {  
"token" : "bar",  
"start\_offset" : 4,  
"end\_offset" : 7,  
"type" : "word",  
"position" : 2  
}, {  
"token" : "baz",  
"start\_offset" : 8,  
"end\_offset" : 11,  
"type" : "word",  
"position" : 3  
} ]  
}

However, using the keyword analyzer will retain the entire phrase:

curl '192.168.w.xyz:9200/test/\_analyze?pretty=1&analyzer=keyword' -d 'foo  
bAr baZ'  
{  
"tokens" : [ {  
"token" : "foo bAr baZ",  
"start\_offset" : 0,  
"end\_offset" : 11,  
"type" : "word",  
"position" : 1  
} ]  
}

On Tuesday, October 28, 2014 10:00:01 AM UTC, vineeth mohan wrote:

> Hello Valergi ,
> 
> This wont work , normally becuase the string would be tokenized into green  
> and energy.  
> If you use shingle token filter and set it as 2 , it might work.  
> Or in this case , you can see the position value of both the token using  
> the script and if its next to each other , you can take it as an  
> occurrence.
> 
> Thanks  
> Vineeth
> 
> On Tue, Oct 28, 2014 at 3:06 PM, \<[valerij.v...@googlemail.com](mailto:valerij.v...@googlemail.com)  
> \<javascript:\>\> wrote:
> 
> > I want to access frequency of a phraze combined from multiple words e.g.  
> > "green energy"
> > 
> > I can access tf of "green" and "energy", example:
> > 
> > "function\_score":  
> > {  
> > "filter" : {  
> > "terms" : { "content" : ["energy","green"]}  
> > },  
> > "script\_score": {  
> > "script": "\_index['content']['energy'].tf() +  
> > \_index['content']['green'].tf()",  
> > "lang":"groovy"  
> > }  
> > }
> > 
> > This works fine. However, how can I find the frequency of a term "green  
> > energy" as
> > 
> > \_index['content']['green energy'].tf() does not work
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/2e4388a4-72d6-4933-9686-304dea0727f1%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/87fbc699-ade2-489f-b715-a987066d6cc4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/87fbc699-ade2-489f-b715-a987066d6cc4%40googlegroups.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, 12:53am UTC](https://discuss.elastic.co/t/elasticsearch-phraze-term-frequency-tf-containing-multiple-words/20458/5 "2017-07-06T00:53:15Z")

</div>


