# Combining match query with "\_all" and boosting at query time?

**URL:** <https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407>\
**Category:** Elasticsearch\
**Created:** [October 18, 2012, 12:19pm UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407 "2012-10-18T12:19:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Pepper](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_pepper/32/996_2.png) [@Alexander\_Pepper](https://discuss.elastic.co/u/Alexander_Pepper)\
**Post date:** [October 18, 2012, 12:19pm UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/1 "2012-10-18T12:19:39Z")

</div>

Hello there,

is it possible to use a match query which searches in "\_all" and combine it  
with boosting at query time?

For example: I have a field title and body. Now I'm searching for { "match"  
: { "\_all" : "foo" } }, but I want to boost hits in title over body at  
query time (not at index time).

Greetings from Berlin,  
Alex

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [October 18, 2012, 1:27pm UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/2 "2012-10-18T13:27:01Z")

</div>

Hi Alexander,

Why not use the multi\_match query? This allows you to use multiple  
fields at same time with different boosts.  
Example:  
{  
"multi\_match" : {  
"query" : "foo",  
"fields" : ["title^1.5", "body^0.5"]  
}  
}

Since you don't use index time boosts and you want have different  
weight per field this seems a better approach to me. You can then also  
disable the \_all field in your mapping, which will save you disk space  
too!

Martijn

On 18 October 2012 14:19, Alexander Pepper [alexander.pepper@infopark.de](mailto:alexander.pepper@infopark.de) wrote:

> Hello there,
> 
> is it possible to use a match query which searches in "\_all" and combine it  
> with boosting at query time?
> 
> For example: I have a field title and body. Now I'm searching for { "match"  
> : { "\_all" : "foo" } }, but I want to boost hits in title over body at query  
> time (not at index time).
> 
> Greetings from Berlin,  
> Alex
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Alexander\_Pepper](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_pepper/32/996_2.png) [@Alexander\_Pepper](https://discuss.elastic.co/u/Alexander_Pepper)\
**Post date:** [October 18, 2012, 1:34pm UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/3 "2012-10-18T13:34:13Z")

</div>

Hello Martijn,

On Thursday, October 18, 2012 3:27:25 PM UTC+2, Martijn v Groningen wrote:

> Why not use the multi\_match query? This allows you to use multiple  
> fields at same time with different boosts.  
> Example:  
> {  
> "multi\_match" : {  
> "query" : "foo",  
> "fields" : ["title^1.5", "body^0.5"]  
> }  
> }

Thank you for your reply. I also thought about using a specific list of  
fields. But this was only a simplified example. In reality i have several  
fields which are not necessary known to me during query time. Thats why I  
want to use the "\_all" field, but still boost specific fields. E.g. search  
for "foo" in "\_all" fields, but boost hits, where it is found in the  
attribute "title".

Any ideas?

Greetings from Berlin,  
Alex

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [October 18, 2012, 2:47pm UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/4 "2012-10-18T14:47:34Z")

</div>

So you don't know what fields might by used during query time, but do  
you know what fields you possibly want to boost?  
If that is the case then I think you still use the match\_all query.  
Just include the \_all field.  
{  
"multi\_match" : {  
"query" : "foo",  
"fields" : ["\_all", "title^2"]  
}  
}

If a document has a match on either \_all field or title field it will  
be returned (like a OR). Documents with a hit in title will get an  
extra boost of 2.

Martijn

On 18 October 2012 15:34, Alexander Pepper [alexander.pepper@infopark.de](mailto:alexander.pepper@infopark.de) wrote:

> Hello Martijn,
> 
> On Thursday, October 18, 2012 3:27:25 PM UTC+2, Martijn v Groningen wrote:
> 
> > Why not use the multi\_match query? This allows you to use multiple  
> > fields at same time with different boosts.  
> > Example:  
> > {  
> > "multi\_match" : {  
> > "query" : "foo",  
> > "fields" : ["title^1.5", "body^0.5"]  
> > }  
> > }
> 
> Thank you for your reply. I also thought about using a specific list of  
> fields. But this was only a simplified example. In reality i have several  
> fields which are not necessary known to me during query time. Thats why I  
> want to use the "\_all" field, but still boost specific fields. E.g. search  
> for "foo" in "\_all" fields, but boost hits, where it is found in the  
> attribute "title".
> 
> Any ideas?
> 
> Greetings from Berlin,  
> Alex

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Alexander\_Pepper](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_pepper/32/996_2.png) [@Alexander\_Pepper](https://discuss.elastic.co/u/Alexander_Pepper)\
**Post date:** [October 18, 2012, 3:51pm UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/5 "2012-10-18T15:51:34Z")

</div>

On Thursday, October 18, 2012 4:47:58 PM UTC+2, Martijn v Groningen wrote:

> So you don't know what fields might by used during query time, but do  
> you know what fields you possibly want to boost?  
> If that is the case then I think you still use the match\_all query.  
> Just include the \_all field.  
> {  
> "multi\_match" : {  
> "query" : "foo",  
> "fields" : ["\_all", "title^2"]  
> }  
> }
> 
> If a document has a match on either \_all field or title field it will  
> be returned (like a OR). Documents with a hit in title will get an  
> extra boost of 2.
> 
> Martijn

Great. This is what I was looking for. Especially the the link to dis\_max  
in the documentation about multi\_match gave me a good clue.

One further question regarding negative boosting: Let's say I want to down  
boost hits, where it matches in the title. As far as I can see both with  
bool and with dis\_max this is not possible, because it either will take the  
sum (bool) or the maximum (dis\_max).

Bool Query:  
{  
"bool": {  
"should": [  
{ "match" : { "\_all" : "foo" } },  
{ "custom\_boost\_factor": {  
"query": { "match" : { "title" : "foo" } },  
"boost\_factor": 0.01  
}  
]  
}  
}

Dis-Max Query:  
{  
"dis\_max": {  
"queries": [  
{ "match" : { "\_all" : "foo" } },  
{ "custom\_boost\_factor": {  
"query": { "match" : { "title" : "foo" } },  
"boost\_factor": 0.01  
}  
]  
}  
}

So my follow up question is: is it possible to lower the score of hits in  
the field title while still using the "\_all" field?

Greetings from Berlin,  
Alex

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [October 18, 2012, 6:39pm UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/6 "2012-10-18T18:39:02Z")

</div>

Yes, but the query will then be more complex. What might be a good  
idea to try is to let the custom\_filters\_score query wrap your current  
query:

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

Just add a query filter for all fields you want to give a lower boost.

Martijn

On 18 October 2012 17:51, Alexander Pepper [alexander.pepper@infopark.de](mailto:alexander.pepper@infopark.de) wrote:

> On Thursday, October 18, 2012 4:47:58 PM UTC+2, Martijn v Groningen wrote:
> 
> > So you don't know what fields might by used during query time, but do  
> > you know what fields you possibly want to boost?  
> > If that is the case then I think you still use the match\_all query.  
> > Just include the \_all field.  
> > {  
> > "multi\_match" : {  
> > "query" : "foo",  
> > "fields" : ["\_all", "title^2"]  
> > }  
> > }
> > 
> > If a document has a match on either \_all field or title field it will  
> > be returned (like a OR). Documents with a hit in title will get an  
> > extra boost of 2.
> > 
> > Martijn
> 
> Great. This is what I was looking for. Especially the the link to dis\_max in  
> the documentation about multi\_match gave me a good clue.
> 
> One further question regarding negative boosting: Let's say I want to down  
> boost hits, where it matches in the title. As far as I can see both with  
> bool and with dis\_max this is not possible, because it either will take the  
> sum (bool) or the maximum (dis\_max).
> 
> Bool Query:  
> {  
> "bool": {  
> "should": [  
> { "match" : { "\_all" : "foo" } },  
> { "custom\_boost\_factor": {  
> "query": { "match" : { "title" : "foo" } },  
> "boost\_factor": 0.01  
> }  
> ]  
> }  
> }
> 
> Dis-Max Query:  
> {  
> "dis\_max": {  
> "queries": [  
> { "match" : { "\_all" : "foo" } },  
> { "custom\_boost\_factor": {  
> "query": { "match" : { "title" : "foo" } },  
> "boost\_factor": 0.01  
> }  
> ]  
> }  
> }
> 
> So my follow up question is: is it possible to lower the score of hits in  
> the field title while still using the "\_all" field?
> 
> Greetings from Berlin,  
> Alex
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 19, 2012, 10:39am UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/7 "2012-10-19T10:39:50Z")

</div>

On Thu, 2012-10-18 at 20:39 +0200, Martijn v Groningen wrote:

> Yes, but the query will then be more complex. What might be a good  
> idea to try is to let the custom\_filters\_score query wrap your current  
> query:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html)
> 
> Just add a query filter for all fields you want to give a lower boost.

There's also the "boosting" query that allows you to reduce the score  
for docs matching a query

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

clint

--

---

<div class="post-metadata">

**Author:** ![Alexander\_Pepper](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_pepper/32/996_2.png) [@Alexander\_Pepper](https://discuss.elastic.co/u/Alexander_Pepper)\
**Post date:** [October 19, 2012, 12:07pm UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/8 "2012-10-19T12:07:54Z")

</div>

Hi Clint,

> There's also the "boosting" query that allows you to reduce the score  
> for docs matching a query
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/boosting-query.html)
> 
> clint

As far as I can see, the boosting-query only allows to boost up or down, but not different boosts for different fields. E.G. having field x, y, z, where x should be boosted with 2.0, y with boost 3.0 and z with boost 4.0. Am I right?

Greetings from Berlin,  
Alex

--

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 20, 2012, 10:10am UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/9 "2012-10-20T10:10:40Z")

</div>

On Fri, 2012-10-19 at 14:07 +0200, Alexander Pepper wrote:

> Hi Clint,
> 
> > There's also the "boosting" query that allows you to reduce the  
> > score  
> > for docs matching a query
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/boosting-query.html)
> 
> > clint
> 
> As far as I can see, the boosting-query only allows to boost up or  
> down, but not different boosts for different fields. E.G. having field  
> x, y, z, where x should be boosted with 2.0, y with boost 3.0 and z  
> with boost 4.0. Am I right?

Correct. Sorry I must have misread the question

clint

--

---

<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, 3:07am UTC](https://discuss.elastic.co/t/combining-match-query-with--all-and-boosting-at-query-time/9407/10 "2017-07-06T03:07:51Z")

</div>


