# Getting different results while using bool query vs bool query with function score query

**URL:** <https://discuss.elastic.co/t/getting-different-results-while-using-bool-query-vs-bool-query-with-function-score-query/19480>\
**Category:** Elasticsearch\
**Created:** [August 26, 2014, 11:15pm UTC](https://discuss.elastic.co/t/getting-different-results-while-using-bool-query-vs-bool-query-with-function-score-query/19480 "2014-08-26T23:15:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![akshaysh](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@akshaysh](https://discuss.elastic.co/u/akshaysh)\
**Post date:** [August 26, 2014, 11:15pm UTC](https://discuss.elastic.co/t/getting-different-results-while-using-bool-query-vs-bool-query-with-function-score-query/19480/1 "2014-08-26T23:15:07Z")

</div>

I am trying to add a custom boost to the different should clauses in the  
bool query, but I am getting different number of results when I use the  
bool query with 2 should clauses containing 2 simple query string query vs  
a bool query with 2 should clauses with 2 function score query  
encapsulating the same simple query string queries.  
The following query returns me 2 results for my data set:  
{  
"query" : {  
"filtered" : {  
"query" : {  
"bool" : {  
"should" : [ {  
"simple\_query\_string" : {  
"query" : "128",  
"fields" : ["content.name\_enu.simple"]  
}  
}, {  
"simple\_query\_string" : {  
"query" : "128",  
"fields" : ["content.name\_enu.simple\_with\_numeric"]  
}  
} ]  
}  
},  
"filter" : {  
"bool" : {  
"must" : [ {  
"term" : {  
"securityInfo.securityType" : "open"  
}  
}, {  
"bool" : {  
"must" : [ {  
"term" : {  
"sourceId.sourceSystem" : "jmeter\_007971\_numeric"  
}  
}, {  
"term" : {  
"sourceId.type" : "file"  
}  
} ]  
}  
} ],  
"\_cache" : true  
}  
}  
}  
},  
"fields" : [ "elementId", "sourceId.id", "sourceId.type",  
"sourceId.sourceSystem", "sourceVersion", "content.name\_enu" ]  
}

Where as if I use the following query I get 5 results, same simple query  
strings but with function scores:  
{  
"query" : {  
"filtered" : {  
"query" : {  
"bool" : {  
"should" : [ {  
"function\_score" : {  
"query" : {  
"simple\_query\_string" : {  
"query" : "128",  
"fields" : ["content.name\_enu.simple"]  
}  
},  
"boost\_factor" : 1.5  
}  
}, {  
"function\_score" : {  
"query" : {  
"simple\_query\_string" : {  
"query" : "128",  
"fields" : ["content.name\_enu.simple\_with\_numeric"]  
}  
},  
"boost\_factor" : 2.5  
}  
} ]  
}  
},  
"filter" : {  
"bool" : {  
"must" : [ {  
"term" : {  
"securityInfo.securityType" : "open"  
}  
}, {  
"bool" : {  
"must" : [ {  
"term" : {  
"sourceId.sourceSystem" : "jmeter\_007971\_numeric"  
}  
}, {  
"term" : {  
"sourceId.type" : "file"  
}  
} ]  
}  
} ],  
"\_cache" : true  
}  
}  
}  
},  
"fields" : [ "elementId", "sourceId.id", "sourceId.type",  
"sourceId.sourceSystem", "sourceVersion", "content.name\_enu" ]  
}

From my understanding of how the should clause works I was expecting both  
the queries to return 5 results but I am not able to understand why the 1st  
query returns me 2 results for my data set. The "content.name\_enu.simple"  
uses a simple analyzer, whereas simple\_with\_numeric uses whitespace  
tokenizer and lowercase filter

--  
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/0e31e1c7-8b07-4220-abc9-c520d681495a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0e31e1c7-8b07-4220-abc9-c520d681495a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 27, 2014, 6:07pm UTC](https://discuss.elastic.co/t/getting-different-results-while-using-bool-query-vs-bool-query-with-function-score-query/19480/2 "2014-08-27T18:07:00Z")

</div>

The function score should not affect relevancy, only the scoring, so the  
number of results should not differ. Strange.

Perhaps you do not need to use a function score. With the simple query  
string, you can append the boost parameter to the field name:

"simple\_query\_string": {  
"query": "128",  
"fields": [  
"content.name\_enu.simple^1.5"  
]  
}

Since your example query is just a simple term and not a Lucene query, you  
should probably use a match query, which is a boostable query.

Cheers,

Ivan

On Tue, Aug 26, 2014 at 4:15 PM, Akshay Shukla [akshayshukla.as@gmail.com](mailto:akshayshukla.as@gmail.com)  
wrote:

> I am trying to add a custom boost to the different should clauses in the  
> bool query, but I am getting different number of results when I use the  
> bool query with 2 should clauses containing 2 simple query string query vs  
> a bool query with 2 should clauses with 2 function score query  
> encapsulating the same simple query string queries.  
> The following query returns me 2 results for my data set:  
> {  
> "query" : {  
> "filtered" : {  
> "query" : {  
> "bool" : {  
> "should" : [ {  
> "simple\_query\_string" : {  
> "query" : "128",  
> "fields" : ["content.name\_enu.simple"]  
> }  
> }, {  
> "simple\_query\_string" : {  
> "query" : "128",  
> "fields" : ["content.name\_enu.simple\_with\_numeric"]  
> }  
> } ]  
> }  
> },  
> "filter" : {  
> "bool" : {  
> "must" : [ {  
> "term" : {  
> "securityInfo.securityType" : "open"  
> }  
> }, {  
> "bool" : {  
> "must" : [ {  
> "term" : {  
> "sourceId.sourceSystem" : "jmeter\_007971\_numeric"  
> }  
> }, {  
> "term" : {  
> "sourceId.type" : "file"  
> }  
> } ]  
> }  
> } ],  
> "\_cache" : true  
> }  
> }  
> }  
> },  
> "fields" : [ "elementId", "sourceId.id", "sourceId.type",  
> "sourceId.sourceSystem", "sourceVersion", "content.name\_enu" ]  
> }
> 
> Where as if I use the following query I get 5 results, same simple query  
> strings but with function scores:  
> {  
> "query" : {  
> "filtered" : {  
> "query" : {  
> "bool" : {  
> "should" : [ {  
> "function\_score" : {  
> "query" : {  
> "simple\_query\_string" : {  
> "query" : "128",  
> "fields" : ["content.name\_enu.simple"]  
> }  
> },  
> "boost\_factor" : 1.5  
> }  
> }, {  
> "function\_score" : {  
> "query" : {  
> "simple\_query\_string" : {  
> "query" : "128",  
> "fields" : ["content.name\_enu.simple\_with\_numeric"]  
> }  
> },  
> "boost\_factor" : 2.5  
> }  
> } ]  
> }  
> },  
> "filter" : {  
> "bool" : {  
> "must" : [ {  
> "term" : {  
> "securityInfo.securityType" : "open"  
> }  
> }, {  
> "bool" : {  
> "must" : [ {  
> "term" : {  
> "sourceId.sourceSystem" : "jmeter\_007971\_numeric"  
> }  
> }, {  
> "term" : {  
> "sourceId.type" : "file"  
> }  
> } ]  
> }  
> } ],  
> "\_cache" : true  
> }  
> }  
> }  
> },  
> "fields" : [ "elementId", "sourceId.id", "sourceId.type",  
> "sourceId.sourceSystem", "sourceVersion", "content.name\_enu" ]  
> }
> 
> From my understanding of how the should clause works I was expecting both  
> the queries to return 5 results but I am not able to understand why the 1st  
> query returns me 2 results for my data set. The "content.name\_enu.simple"  
> uses a simple analyzer, whereas simple\_with\_numeric uses whitespace  
> tokenizer and lowercase filter
> 
> --  
> 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/0e31e1c7-8b07-4220-abc9-c520d681495a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0e31e1c7-8b07-4220-abc9-c520d681495a%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/0e31e1c7-8b07-4220-abc9-c520d681495a%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/0e31e1c7-8b07-4220-abc9-c520d681495a%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/CALY%3DcQAEJyMOonB38jqQiWQ\_17mU%3DGSdkUqz0ctQ6OR8yywoWg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAEJyMOonB38jqQiWQ_17mU%3DGSdkUqz0ctQ6OR8yywoWg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 27, 2014, 6:18pm UTC](https://discuss.elastic.co/t/getting-different-results-while-using-bool-query-vs-bool-query-with-function-score-query/19480/3 "2014-08-27T18:18:16Z")

</div>

Forgot to add that since your search term is the same, besides using a  
match query, you can also use a multi match query. Your queries would be  
easier to read.

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

Not sure why your original query is not working. If you post some example  
documents and mapping, but others might be able to figure it out.

--  
Ivan

On Wed, Aug 27, 2014 at 11:07 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> The function score should not affect relevancy, only the scoring, so the  
> number of results should not differ. Strange.
> 
> Perhaps you do not need to use a function score. With the simple query  
> string, you can append the boost parameter to the field name:
> 
> "simple\_query\_string": {  
> "query": "128",  
> "fields": [  
> "content.name\_enu.simple^1.5"  
> ]  
> }
> 
> Since your example query is just a simple term and not a Lucene query, you  
> should probably use a match query, which is a boostable query.
> 
> Cheers,
> 
> Ivan
> 
> On Tue, Aug 26, 2014 at 4:15 PM, Akshay Shukla [akshayshukla.as@gmail.com](mailto:akshayshukla.as@gmail.com)  
> wrote:
> 
> > I am trying to add a custom boost to the different should clauses in the  
> > bool query, but I am getting different number of results when I use the  
> > bool query with 2 should clauses containing 2 simple query string query vs  
> > a bool query with 2 should clauses with 2 function score query  
> > encapsulating the same simple query string queries.  
> > The following query returns me 2 results for my data set:  
> > {  
> > "query" : {  
> > "filtered" : {  
> > "query" : {  
> > "bool" : {  
> > "should" : [ {  
> > "simple\_query\_string" : {  
> > "query" : "128",  
> > "fields" : ["content.name\_enu.simple"]  
> > }  
> > }, {  
> > "simple\_query\_string" : {  
> > "query" : "128",  
> > "fields" : ["content.name\_enu.simple\_with\_numeric"]  
> > }  
> > } ]  
> > }  
> > },  
> > "filter" : {  
> > "bool" : {  
> > "must" : [ {  
> > "term" : {  
> > "securityInfo.securityType" : "open"  
> > }  
> > }, {  
> > "bool" : {  
> > "must" : [ {  
> > "term" : {  
> > "sourceId.sourceSystem" : "jmeter\_007971\_numeric"  
> > }  
> > }, {  
> > "term" : {  
> > "sourceId.type" : "file"  
> > }  
> > } ]  
> > }  
> > } ],  
> > "\_cache" : true  
> > }  
> > }  
> > }  
> > },  
> > "fields" : [ "elementId", "sourceId.id", "sourceId.type",  
> > "sourceId.sourceSystem", "sourceVersion", "content.name\_enu" ]  
> > }
> > 
> > Where as if I use the following query I get 5 results, same simple query  
> > strings but with function scores:  
> > {  
> > "query" : {  
> > "filtered" : {  
> > "query" : {  
> > "bool" : {  
> > "should" : [ {  
> > "function\_score" : {  
> > "query" : {  
> > "simple\_query\_string" : {  
> > "query" : "128",  
> > "fields" : ["content.name\_enu.simple"]  
> > }  
> > },  
> > "boost\_factor" : 1.5  
> > }  
> > }, {  
> > "function\_score" : {  
> > "query" : {  
> > "simple\_query\_string" : {  
> > "query" : "128",  
> > "fields" : ["content.name\_enu.simple\_with\_numeric"]  
> > }  
> > },  
> > "boost\_factor" : 2.5  
> > }  
> > } ]  
> > }  
> > },  
> > "filter" : {  
> > "bool" : {  
> > "must" : [ {  
> > "term" : {  
> > "securityInfo.securityType" : "open"  
> > }  
> > }, {  
> > "bool" : {  
> > "must" : [ {  
> > "term" : {  
> > "sourceId.sourceSystem" : "jmeter\_007971\_numeric"  
> > }  
> > }, {  
> > "term" : {  
> > "sourceId.type" : "file"  
> > }  
> > } ]  
> > }  
> > } ],  
> > "\_cache" : true  
> > }  
> > }  
> > }  
> > },  
> > "fields" : [ "elementId", "sourceId.id", "sourceId.type",  
> > "sourceId.sourceSystem", "sourceVersion", "content.name\_enu" ]  
> > }
> > 
> > From my understanding of how the should clause works I was expecting both  
> > the queries to return 5 results but I am not able to understand why the 1st  
> > query returns me 2 results for my data set. The "content.name\_enu.simple"  
> > uses a simple analyzer, whereas simple\_with\_numeric uses whitespace  
> > tokenizer and lowercase filter
> > 
> > --  
> > 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/0e31e1c7-8b07-4220-abc9-c520d681495a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0e31e1c7-8b07-4220-abc9-c520d681495a%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/0e31e1c7-8b07-4220-abc9-c520d681495a%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/0e31e1c7-8b07-4220-abc9-c520d681495a%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/CALY%3DcQBM2KityjY%2BS2FtqHEtVuJKfokm6Z15gy8\_VgXu4h2mLw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBM2KityjY%2BS2FtqHEtVuJKfokm6Z15gy8_VgXu4h2mLw%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:05am UTC](https://discuss.elastic.co/t/getting-different-results-while-using-bool-query-vs-bool-query-with-function-score-query/19480/4 "2017-07-06T01:05:57Z")

</div>


