# Trying to boost a result in a filtered query

**URL:** <https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893>\
**Category:** Elasticsearch\
**Created:** [October 9, 2013, 8:16pm UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893 "2013-10-09T20:16:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravi3100](https://avatars.discourse-cdn.com/v4/letter/r/90ced4/32.png) [@ravi3100](https://discuss.elastic.co/u/ravi3100)\
**Post date:** [October 9, 2013, 8:16pm UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893/1 "2013-10-09T20:16:29Z")

</div>

I am trying to run following query. I want to boost the record if its name  
contains a word pizza. Is there anything wrong in my query?. All the  
records (53) returned by filter contain the word "Houston" in field  
addrtext. Now I want to boost those records which contain the word pizza in  
field name. I have been trying for few days with different queries but no  
luck yet.  
What am I missing?

{  
"size" : 100,  
"query" : {  
"filtered" : {  
"query" : {  
"query\_string" : {  
"query" : "pizza",  
"default\_field" : "name^10"  
}  
}  
},  
"filter" : {  
"query" : {  
"query\_string" : {  
"query" : "Houston",  
"default\_field" : "addrtext"  
}  
}  
}  
}  
}

Thanks for help  
Ravi

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [October 10, 2013, 12:33pm UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893/2 "2013-10-10T12:33:45Z")

</div>

Hi,  
what you are currently doing with your filtered query is 1) filtering out  
all the documents that don't match the addrtext:Houston query 2) executing  
the query on top of those documents.

The fact that you give a boost to the name field doesn't make any  
difference since that's the only field you are searching on. The fields  
weight makes sense only when searching on multiple fields, or eventually if  
you have multiple queries in OR (e.g. using a bool query with should  
clauses).

Cheers  
Luca

On Wednesday, October 9, 2013 10:16:29 PM UTC+2, [ravi...@gmail.com](mailto:ravi...@gmail.com) wrote:

> I am trying to run following query. I want to boost the record if its name  
> contains a word pizza. Is there anything wrong in my query?. All the  
> records (53) returned by filter contain the word "Houston" in field  
> addrtext. Now I want to boost those records which contain the word pizza in  
> field name. I have been trying for few days with different queries but no  
> luck yet.  
> What am I missing?
> 
> {  
> "size" : 100,  
> "query" : {  
> "filtered" : {  
> "query" : {  
> "query\_string" : {  
> "query" : "pizza",  
> "default\_field" : "name^10"  
> }  
> }  
> },  
> "filter" : {  
> "query" : {  
> "query\_string" : {  
> "query" : "Houston",  
> "default\_field" : "addrtext"  
> }  
> }  
> }  
> }  
> }
> 
> Thanks for help  
> Ravi

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![ravi3100](https://avatars.discourse-cdn.com/v4/letter/r/90ced4/32.png) [@ravi3100](https://discuss.elastic.co/u/ravi3100)\
**Post date:** [October 11, 2013, 1:22am UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893/3 "2013-10-11T01:22:45Z")

</div>

That is exactly what is happening. The filter is behaving correctly. It is  
returning results containing "Houston".  
The query has no effect at all.

So how do I fix this?  
Basically what I want is this.  
I have a set of AND filters that return me say 53 results.  
Now let us say 3 of these records contain a word pizza in another field.  
I want to modify the query or filter so that I get the same 53 results with  
3 records containing pizza on top.  
How can I do this?

Ravi

On Thursday, October 10, 2013 8:33:45 AM UTC-4, Luca Cavanna wrote:

> Hi,  
> what you are currently doing with your filtered query is 1) filtering out  
> all the documents that don't match the addrtext:Houston query 2) executing  
> the query on top of those documents.
> 
> The fact that you give a boost to the name field doesn't make any  
> difference since that's the only field you are searching on. The fields  
> weight makes sense only when searching on multiple fields, or eventually if  
> you have multiple queries in OR (e.g. using a bool query with should  
> clauses).
> 
> Cheers  
> Luca
> 
> On Wednesday, October 9, 2013 10:16:29 PM UTC+2, [ravi...@gmail.com](mailto:ravi...@gmail.com) wrote:
> 
> > I am trying to run following query. I want to boost the record if its  
> > name contains a word pizza. Is there anything wrong in my query?. All the  
> > records (53) returned by filter contain the word "Houston" in field  
> > addrtext. Now I want to boost those records which contain the word pizza in  
> > field name. I have been trying for few days with different queries but no  
> > luck yet.  
> > What am I missing?
> > 
> > {  
> > "size" : 100,  
> > "query" : {  
> > "filtered" : {  
> > "query" : {  
> > "query\_string" : {  
> > "query" : "pizza",  
> > "default\_field" : "name^10"  
> > }  
> > }  
> > },  
> > "filter" : {  
> > "query" : {  
> > "query\_string" : {  
> > "query" : "Houston",  
> > "default\_field" : "addrtext"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > Thanks for help  
> > Ravi

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [October 11, 2013, 8:47am UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893/4 "2013-10-11T08:47:27Z")

</div>

Hi,  
all documents you get back match both the filter and the query. You might  
want to add the `explain=true` parameter to your request to know more about  
what is going on under the hood.

I think I got what you want to achieve though. I would go with a bool query  
that has a match\_all as must clause and your query as should clause, so  
that it's not mandatory but if it does match thoase documents will be  
boosted. I would also replace the query\_string with the match query.

{  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"must": [  
{"match\_all": {}}  
],  
"should": [  
{  
"match": {  
"name": "pizza"  
}  
}  
]  
}  
},  
"filter": {  
}  
}  
}  
}

On Fri, Oct 11, 2013 at 3:22 AM, [ravi3100@gmail.com](mailto:ravi3100@gmail.com) wrote:

> That is exactly what is happening. The filter is behaving correctly. It is  
> returning results containing "Houston".  
> The query has no effect at all.
> 
> So how do I fix this?  
> Basically what I want is this.  
> I have a set of AND filters that return me say 53 results.  
> Now let us say 3 of these records contain a word pizza in another field.  
> I want to modify the query or filter so that I get the same 53 results  
> with 3 records containing pizza on top.  
> How can I do this?
> 
> Ravi
> 
> On Thursday, October 10, 2013 8:33:45 AM UTC-4, Luca Cavanna wrote:
> 
> > Hi,  
> > what you are currently doing with your filtered query is 1) filtering out  
> > all the documents that don't match the addrtext:Houston query 2) executing  
> > the query on top of those documents.
> > 
> > The fact that you give a boost to the name field doesn't make any  
> > difference since that's the only field you are searching on. The fields  
> > weight makes sense only when searching on multiple fields, or eventually if  
> > you have multiple queries in OR (e.g. using a bool query with should  
> > clauses).
> > 
> > Cheers  
> > Luca
> > 
> > On Wednesday, October 9, 2013 10:16:29 PM UTC+2, [ravi...@gmail.com](mailto:ravi...@gmail.com) wrote:
> > 
> > > I am trying to run following query. I want to boost the record if its  
> > > name contains a word pizza. Is there anything wrong in my query?. All the  
> > > records (53) returned by filter contain the word "Houston" in field  
> > > addrtext. Now I want to boost those records which contain the word pizza in  
> > > field name. I have been trying for few days with different queries but no  
> > > luck yet.  
> > > What am I missing?
> > > 
> > > {  
> > > "size" : 100,  
> > > "query" : {  
> > > "filtered" : {  
> > > "query" : {  
> > > "query\_string" : {  
> > > "query" : "pizza",  
> > > "default\_field" : "name^10"  
> > > }  
> > > }  
> > > },  
> > > "filter" : {  
> > > "query" : {  
> > > "query\_string" : {  
> > > "query" : "Houston",  
> > > "default\_field" : "addrtext"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > Thanks for help  
> > > Ravi
> > > 
> > > --  
> > > 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/Lm3PjY8Y1nk/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Lm3PjY8Y1nk/unsubscribe).  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![ravi3100](https://avatars.discourse-cdn.com/v4/letter/r/90ced4/32.png) [@ravi3100](https://discuss.elastic.co/u/ravi3100)\
**Post date:** [October 15, 2013, 9:32pm UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893/5 "2013-10-15T21:32:07Z")

</div>

Thanks so much. It worked like a charm!

Ravi

On Friday, October 11, 2013 4:47:27 AM UTC-4, Luca Cavanna wrote:

> Hi,  
> all documents you get back match both the filter and the query. You might  
> want to add the `explain=true` parameter to your request to know more about  
> what is going on under the hood.
> 
> I think I got what you want to achieve though. I would go with a bool  
> query that has a match\_all as must clause and your query as should clause,  
> so that it's not mandatory but if it does match thoase documents will be  
> boosted. I would also replace the query\_string with the match query.
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool": {  
> "must": [  
> {"match\_all": {}}  
> ],  
> "should": [  
> {  
> "match": {  
> "name": "pizza"  
> }  
> }  
> ]  
> }  
> },  
> "filter": {  
> }  
> }  
> }  
> }
> 
> On Fri, Oct 11, 2013 at 3:22 AM, \<[ravi...@gmail.com](mailto:ravi...@gmail.com) \<javascript:\>\> wrote:
> 
> > That is exactly what is happening. The filter is behaving correctly. It  
> > is returning results containing "Houston".  
> > The query has no effect at all.
> > 
> > So how do I fix this?  
> > Basically what I want is this.  
> > I have a set of AND filters that return me say 53 results.  
> > Now let us say 3 of these records contain a word pizza in another field.  
> > I want to modify the query or filter so that I get the same 53 results  
> > with 3 records containing pizza on top.  
> > How can I do this?
> > 
> > Ravi
> > 
> > On Thursday, October 10, 2013 8:33:45 AM UTC-4, Luca Cavanna wrote:
> > 
> > > Hi,  
> > > what you are currently doing with your filtered query is 1) filtering  
> > > out all the documents that don't match the addrtext:Houston query 2)  
> > > executing the query on top of those documents.
> > > 
> > > The fact that you give a boost to the name field doesn't make any  
> > > difference since that's the only field you are searching on. The fields  
> > > weight makes sense only when searching on multiple fields, or eventually if  
> > > you have multiple queries in OR (e.g. using a bool query with should  
> > > clauses).
> > > 
> > > Cheers  
> > > Luca
> > > 
> > > On Wednesday, October 9, 2013 10:16:29 PM UTC+2, ravi...@gmail.comwrote:
> > > 
> > > > I am trying to run following query. I want to boost the record if its  
> > > > name contains a word pizza. Is there anything wrong in my query?. All the  
> > > > records (53) returned by filter contain the word "Houston" in field  
> > > > addrtext. Now I want to boost those records which contain the word pizza in  
> > > > field name. I have been trying for few days with different queries but no  
> > > > luck yet.  
> > > > What am I missing?
> > > > 
> > > > {  
> > > > "size" : 100,  
> > > > "query" : {  
> > > > "filtered" : {  
> > > > "query" : {  
> > > > "query\_string" : {  
> > > > "query" : "pizza",  
> > > > "default\_field" : "name^10"  
> > > > }  
> > > > }  
> > > > },  
> > > > "filter" : {  
> > > > "query" : {  
> > > > "query\_string" : {  
> > > > "query" : "Houston",  
> > > > "default\_field" : "addrtext"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > Thanks for help  
> > > > Ravi
> > > > 
> > > > --  
> > > > 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/Lm3PjY8Y1nk/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Lm3PjY8Y1nk/unsubscribe).  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > > 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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![ravi3100](https://avatars.discourse-cdn.com/v4/letter/r/90ced4/32.png) [@ravi3100](https://discuss.elastic.co/u/ravi3100)\
**Post date:** [October 23, 2013, 1:39pm UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893/6 "2013-10-23T13:39:50Z")

</div>

Declared victory too soon. Your suggestion works but there is a  
complication.

I have a sort section in the end. It is sorting by distance. The distance  
sorting is working correct but it is messing up with results of the query.  
Ideally I would like the order of all sorted results to be maintained and  
only those results matching the "should" query should get bumped up in  
final list. At present it is behaving as if the query section is not  
present.  
Is there any way to do this?  
I tried sorting by distance as well as the field but did not have the  
effect.  
Any suggestions?

{  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"must": [  
{"match\_all": {}}  
],  
"should": [  
{  
"match": {  
"name": "pizza"  
}  
}  
]  
}  
},  
"filter": {  
}  
}  
},  
"sort" : [  
]  
}

On Friday, October 11, 2013 4:47:27 AM UTC-4, Luca Cavanna wrote:

> Hi,  
> all documents you get back match both the filter and the query. You might  
> want to add the `explain=true` parameter to your request to know more about  
> what is going on under the hood.
> 
> I think I got what you want to achieve though. I would go with a bool  
> query that has a match\_all as must clause and your query as should clause,  
> so that it's not mandatory but if it does match thoase documents will be  
> boosted. I would also replace the query\_string with the match query.
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool": {  
> "must": [  
> {"match\_all": {}}  
> ],  
> "should": [  
> {  
> "match": {  
> "name": "pizza"  
> }  
> }  
> ]  
> }  
> },  
> "filter": {  
> }  
> }  
> }  
> }
> 
> On Fri, Oct 11, 2013 at 3:22 AM, \<[ravi...@gmail.com](mailto:ravi...@gmail.com) \<javascript:\>\> wrote:
> 
> > That is exactly what is happening. The filter is behaving correctly. It  
> > is returning results containing "Houston".  
> > The query has no effect at all.
> > 
> > So how do I fix this?  
> > Basically what I want is this.  
> > I have a set of AND filters that return me say 53 results.  
> > Now let us say 3 of these records contain a word pizza in another field.  
> > I want to modify the query or filter so that I get the same 53 results  
> > with 3 records containing pizza on top.  
> > How can I do this?
> > 
> > Ravi
> > 
> > On Thursday, October 10, 2013 8:33:45 AM UTC-4, Luca Cavanna wrote:
> > 
> > > Hi,  
> > > what you are currently doing with your filtered query is 1) filtering  
> > > out all the documents that don't match the addrtext:Houston query 2)  
> > > executing the query on top of those documents.
> > > 
> > > The fact that you give a boost to the name field doesn't make any  
> > > difference since that's the only field you are searching on. The fields  
> > > weight makes sense only when searching on multiple fields, or eventually if  
> > > you have multiple queries in OR (e.g. using a bool query with should  
> > > clauses).
> > > 
> > > Cheers  
> > > Luca
> > > 
> > > On Wednesday, October 9, 2013 10:16:29 PM UTC+2, ravi...@gmail.comwrote:
> > > 
> > > > I am trying to run following query. I want to boost the record if its  
> > > > name contains a word pizza. Is there anything wrong in my query?. All the  
> > > > records (53) returned by filter contain the word "Houston" in field  
> > > > addrtext. Now I want to boost those records which contain the word pizza in  
> > > > field name. I have been trying for few days with different queries but no  
> > > > luck yet.  
> > > > What am I missing?
> > > > 
> > > > {  
> > > > "size" : 100,  
> > > > "query" : {  
> > > > "filtered" : {  
> > > > "query" : {  
> > > > "query\_string" : {  
> > > > "query" : "pizza",  
> > > > "default\_field" : "name^10"  
> > > > }  
> > > > }  
> > > > },  
> > > > "filter" : {  
> > > > "query" : {  
> > > > "query\_string" : {  
> > > > "query" : "Houston",  
> > > > "default\_field" : "addrtext"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > Thanks for help  
> > > > Ravi
> > > > 
> > > > --  
> > > > 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/Lm3PjY8Y1nk/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Lm3PjY8Y1nk/unsubscribe).  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > > 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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![ravi3100](https://avatars.discourse-cdn.com/v4/letter/r/90ced4/32.png) [@ravi3100](https://discuss.elastic.co/u/ravi3100)\
**Post date:** [October 24, 2013, 2:23am UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893/7 "2013-10-24T02:23:35Z")

</div>

Found the answer. I had to use two step sorting. First sort by \_score and  
then by distance.

Ravi

--  
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).  
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, 2:10am UTC](https://discuss.elastic.co/t/trying-to-boost-a-result-in-a-filtered-query/13893/8 "2017-07-06T02:10:56Z")

</div>


