# Calculation of whymatch in elasticsearch

**URL:** https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658
**Category:** Elasticsearch
**Created:** [February 7, 2014, 6:35am UTC](https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658 "2014-02-07T06:35:33Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![usha2626](https://avatars.discourse-cdn.com/v4/letter/u/4bbf92/32.png) [@usha2626](https://discuss.elastic.co/u/usha2626)
#### Post date: [February 7, 2014, 6:35am UTC](https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658/1 "2014-02-07T06:35:33Z")

</div>

Hi,

How do we implement whymatch concept in elasticsearch by finding the total  
number of fields in which the search term occurs and the frequency of that  
search term??

--  
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/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Britta\_Weber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/britta_weber/32/1113_2.png) [@Britta\_Weber](https://discuss.elastic.co/u/Britta_Weber)
#### Post date: [February 7, 2014, 1:28pm UTC](https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658/2 "2014-02-07T13:28:44Z")

</div>

If you know the fields that are contained in the document, you could  
use a function\_score query . For counting the number of fields a word  
is contained in, you can use function\_score with a boost\_factor like  
this:

```auto
{
   "query": {
      "function_score": {
         "functions": [
            {
               "filter": {
                  "term": {
                     "field1": "searchterm"
                  }
               },
               "boost_factor": 1
            },
            {
               "filter": {
                  "term": {
                     "field2": "searchterm"
                  }
               },
               "boost_factor": 1
            },
            .... (here be more filters)
         ],
         "boost_mode": "replace",
         "score_mode": "sum"
      }
   }
}

```

This will add 1 to the score for each field (field1, field2,... ) that  
has the term "searchterm" and the final score for each document will  
be the number of fields in the document containing the term. Is this  
what you want?

For getting the term frequencies, you can checkout text scoring in scripts here:

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

See also this thread:  
[https://groups.google.com/forum/#!msg/elasticsearch/9fOEN1uArIY/7bVZP22zYg8J](https://groups.google.com/forum/#!msg/elasticsearch/9fOEN1uArIY/7bVZP22zYg8J)

Cheers,  
Britta

On Fri, Feb 7, 2014 at 7:35 AM, [usha2626@gmail.com](mailto:usha2626@gmail.com) wrote:

> Hi,
> 
> How do we implement whymatch concept in elasticsearch by finding the total  
> number of fields in which the search term occurs and the frequency of that  
> search term??
> 
> --  
> 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/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CALhJbBji5u0oQxJB3Te-k4wKZ108pbn4an5kk%2BDDbjxB4%2BWJnQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALhJbBji5u0oQxJB3Te-k4wKZ108pbn4an5kk%2BDDbjxB4%2BWJnQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [February 7, 2014, 1:36pm UTC](https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658/3 "2014-02-07T13:36:32Z")

</div>

I was thinking of putting together a simple "highlighter" that just returns  
if a field contains a match or not. This sounds like a nice logic  
extension to that. It probably wouldn't actually be a "highlighter" but I  
imagine it'd run during the highlight phase and function similarly. It'd  
need something like the highlight\_query, for example, to function  
properly. Anyway, I believe the short answer is, if you are looking for  
something specific, file an issue on github.

Nik

On Fri, Feb 7, 2014 at 8:28 AM, Britta Weber \<[britta.weber@elasticsearch.com](mailto:britta.weber@elasticsearch.com)

> wrote:

> If you know the fields that are contained in the document, you could  
> use a function\_score query . For counting the number of fields a word  
> is contained in, you can use function\_score with a boost\_factor like  
> this:
> 
> ```auto
> {
> "query": {
> "function_score": {
> "functions": [
> {
> "filter": {
> "term": {
> "field1": "searchterm"
> }
> },
> "boost_factor": 1
> },
> {
> "filter": {
> "term": {
> "field2": "searchterm"
> }
> },
> "boost_factor": 1
> },
> .... (here be more filters)
> ],
> "boost_mode": "replace",
> "score_mode": "sum"
> }
> }
> }
> 
> ```
> 
> This will add 1 to the score for each field (field1, field2,... ) that  
> has the term "searchterm" and the final score for each document will  
> be the number of fields in the document containing the term. Is this  
> what you want?
> 
> For getting the term frequencies, you can checkout text scoring in scripts  
> here:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-advanced-scripting.html)
> 
> See also this thread:
> 
> [Redirecting to Google Groups](https://groups.google.com/forum/#!msg/elasticsearch/9fOEN1uArIY/7bVZP22zYg8J)
> 
> Cheers,  
> Britta
> 
> On Fri, Feb 7, 2014 at 7:35 AM, [usha2626@gmail.com](mailto:usha2626@gmail.com) wrote:
> 
> > Hi,
> > 
> > How do we implement whymatch concept in elasticsearch by finding the  
> > total  
> > number of fields in which the search term occurs and the frequency of  
> > that  
> > search term??
> > 
> > --  
> > 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/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com)  
> .
> 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 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/CALhJbBji5u0oQxJB3Te-k4wKZ108pbn4an5kk%2BDDbjxB4%2BWJnQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALhJbBji5u0oQxJB3Te-k4wKZ108pbn4an5kk%2BDDbjxB4%2BWJnQ%40mail.gmail.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CAPmjWd06gQg-RDGHaD4ScWzLRshUQ6NBybHfTnD1ufDUUH\_CMA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd06gQg-RDGHaD4ScWzLRshUQ6NBybHfTnD1ufDUUH_CMA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Britta\_Weber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/britta_weber/32/1113_2.png) [@Britta\_Weber](https://discuss.elastic.co/u/Britta_Weber)
#### Post date: [February 7, 2014, 1:47pm UTC](https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658/4 "2014-02-07T13:47:03Z")

</div>

Hi Nik,

would a script field also work for that? Something like:

{  
"script\_fields": {  
"field\_match1": {  
"script": "if(\_index['field1']['searchterm'].tf() \> 0){  
return 1;} else {return 0;}"  
},  
"field\_match2": {  
"script": "if(\_index['field2']['searchterm'].tf() \> 0){  
return 1;} else {return 0;}"  
},  
....  
}  
}

Or did I get that wrong?

On Fri, Feb 7, 2014 at 2:36 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com) wrote:

> I was thinking of putting together a simple "highlighter" that just returns  
> if a field contains a match or not. This sounds like a nice logic extension  
> to that. It probably wouldn't actually be a "highlighter" but I imagine  
> it'd run during the highlight phase and function similarly. It'd need  
> something like the highlight\_query, for example, to function properly.  
> Anyway, I believe the short answer is, if you are looking for something  
> specific, file an issue on github.
> 
> Nik
> 
> On Fri, Feb 7, 2014 at 8:28 AM, Britta Weber  
> [britta.weber@elasticsearch.com](mailto:britta.weber@elasticsearch.com) wrote:
> 
> > If you know the fields that are contained in the document, you could  
> > use a function\_score query . For counting the number of fields a word  
> > is contained in, you can use function\_score with a boost\_factor like  
> > this:
> > 
> > ```auto
> > {
> > "query": {
> > "function_score": {
> > "functions": [
> > {
> > "filter": {
> > "term": {
> > "field1": "searchterm"
> > }
> > },
> > "boost_factor": 1
> > },
> > {
> > "filter": {
> > "term": {
> > "field2": "searchterm"
> > }
> > },
> > "boost_factor": 1
> > },
> > .... (here be more filters)
> > ],
> > "boost_mode": "replace",
> > "score_mode": "sum"
> > }
> > }
> > }
> > 
> > ```
> > 
> > This will add 1 to the score for each field (field1, field2,... ) that  
> > has the term "searchterm" and the final score for each document will  
> > be the number of fields in the document containing the term. Is this  
> > what you want?
> > 
> > For getting the term frequencies, you can checkout text scoring in scripts  
> > here:
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-advanced-scripting.html)
> > 
> > See also this thread:
> > 
> > [Redirecting to Google Groups](https://groups.google.com/forum/#!msg/elasticsearch/9fOEN1uArIY/7bVZP22zYg8J)
> > 
> > Cheers,  
> > Britta
> > 
> > On Fri, Feb 7, 2014 at 7:35 AM, [usha2626@gmail.com](mailto:usha2626@gmail.com) wrote:
> > 
> > > Hi,
> > > 
> > > How do we implement whymatch concept in elasticsearch by finding the  
> > > total  
> > > number of fields in which the search term occurs and the frequency of  
> > > that  
> > > search term??
> > > 
> > > --  
> > > 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/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > 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/CALhJbBji5u0oQxJB3Te-k4wKZ108pbn4an5kk%2BDDbjxB4%2BWJnQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALhJbBji5u0oQxJB3Te-k4wKZ108pbn4an5kk%2BDDbjxB4%2BWJnQ%40mail.gmail.com).
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 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/CAPmjWd06gQg-RDGHaD4ScWzLRshUQ6NBybHfTnD1ufDUUH\_CMA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd06gQg-RDGHaD4ScWzLRshUQ6NBybHfTnD1ufDUUH_CMA%40mail.gmail.com).
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CALhJbBhGmNn2EQWuxPeoAq2Do%3DgoZSZdYKTDLy2FkW1y1YkZZg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALhJbBhGmNn2EQWuxPeoAq2Do%3DgoZSZdYKTDLy2FkW1y1YkZZg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Randall\_McRee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randall_mcree/32/47177_2.png) [@Randall\_McRee](https://discuss.elastic.co/u/Randall_McRee)
#### Post date: [February 8, 2014, 7:29pm UTC](https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658/5 "2014-02-08T19:29:56Z")

</div>

Use the explain feature! has all of that information.

On Thu, Feb 6, 2014 at 10:35 PM, [usha2626@gmail.com](mailto:usha2626@gmail.com) wrote:

> Hi,
> 
> How do we implement whymatch concept in elasticsearch by finding the total  
> number of fields in which the search term occurs and the frequency of that  
> search term??
> 
> --  
> 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/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/72b99acc-bd1e-4954-bc52-f09971397daf%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CAFjHw36Y-p-NdtF6MdX-kMUPGvZtvBy2\_V-pzyMJuJTxftntMA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAFjHw36Y-p-NdtF6MdX-kMUPGvZtvBy2_V-pzyMJuJTxftntMA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Navneet\_Mathpal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/navneet_mathpal/32/3677_2.png) [@Navneet\_Mathpal](https://discuss.elastic.co/u/Navneet_Mathpal)
#### Post date: [February 11, 2014, 5:08am UTC](https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658/6 "2014-02-11T05:08:02Z")

</div>

Hi Britta Weber ,

I wanted to extract the total number of times a search term occurs..in a  
particular field..  
Even if the search term is a phrase,the query should return the total  
number of times that phrase  
occurs and also the frequency of individual term of that phrase in each  
field.

Thanks  
Navneet Mathpal

On Friday, 7 February 2014 12:05:33 UTC+5:30, [usha...@gmail.com](mailto:usha...@gmail.com) wrote:

> Hi,
> 
> How do we implement whymatch concept in elasticsearch by finding the total  
> number of fields in which the search term occurs and the frequency of that  
> search term??

--  
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/14c0ce29-5e02-462b-802f-8d131b371b1f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/14c0ce29-5e02-462b-802f-8d131b371b1f%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:51am UTC](https://discuss.elastic.co/t/calculation-of-whymatch-in-elasticsearch/15658/7 "2017-07-06T01:51:15Z")

</div>


