# Mixing bool and multi match/function score query

**URL:** <https://discuss.elastic.co/t/mixing-bool-and-multi-match-function-score-query/16771>\
**Category:** Elasticsearch\
**Created:** [April 2, 2014, 3:01pm UTC](https://discuss.elastic.co/t/mixing-bool-and-multi-match-function-score-query/16771 "2014-04-02T15:01:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gkwelding](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gkwelding/32/1658_2.png) [@gkwelding](https://discuss.elastic.co/u/gkwelding)\
**Post date:** [April 2, 2014, 3:01pm UTC](https://discuss.elastic.co/t/mixing-bool-and-multi-match-function-score-query/16771/1 "2014-04-02T15:01:18Z")

</div>

I'm currently doing a query that's a mix of multi match and function score.  
The important bit of the JSON looks like this:

```
    "function_score":{
        "query":{
            "query_string":{
                "query":"some query",

```

"fields":["id","name","strippedDescription","colourSearch","sizeSearch"]  
}  
}  
}

However, I also want to include results that don't necessarily match the  
query but have a particular numeric value that's greater than 0. I think a  
bool query would do this, but I don't know how to use a bool query with a  
function score query.

I understand that a multi match query is just shorthand for a bool query,  
and I could expand out the multi match query into its bool counter-part,  
however, I then don't know how I would do function score within that.

Any ideas? I'm on version 1.1.0 by the way.

--  
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/4279a874-eef8-47ec-9f49-f91efed1f851%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4279a874-eef8-47ec-9f49-f91efed1f851%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [April 3, 2014, 8:45am UTC](https://discuss.elastic.co/t/mixing-bool-and-multi-match-function-score-query/16771/2 "2014-04-03T08:45:06Z")

</div>

Hi,

I am unsure I got that right - the query in the function\_score can be  
any query, also a bool query. If you want documents that do not match  
the above query, but some other criterion (for example range query) to  
be scored with the function\_score functions you can just replace the  
query\_string with a  
"bool" : {"should": [{"query\_string:{...}"}, {"range": {...}}]}.  
Was that your question?  
If not, could you elaborate a little more on what you want to achieve?

On Wed, Apr 2, 2014 at 5:01 PM, Garry Welding [gkwelding@gmail.com](mailto:gkwelding@gmail.com) wrote:

> I'm currently doing a query that's a mix of multi match and function score.  
> The important bit of the JSON looks like this:
> 
> ```
> "function_score":{
> "query":{
> "query_string":{
> "query":"some query",
> 
> ```
> 
> "fields":["id","name","strippedDescription","colourSearch","sizeSearch"]  
> }  
> }  
> }
> 
> However, I also want to include results that don't necessarily match the  
> query but have a particular numeric value that's greater than 0. I think a  
> bool query would do this, but I don't know how to use a bool query with a  
> function score query.
> 
> I understand that a multi match query is just shorthand for a bool query,  
> and I could expand out the multi match query into its bool counter-part,  
> however, I then don't know how I would do function score within that.
> 
> Any ideas? I'm on version 1.1.0 by the way.
> 
> --  
> 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/4279a874-eef8-47ec-9f49-f91efed1f851%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4279a874-eef8-47ec-9f49-f91efed1f851%40googlegroups.com).  
> 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/CALhJbBguv2u8W3%2BDvosYY4Y7xv7fkqCP-9A4WBSVdg2\_1MQHJg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALhJbBguv2u8W3%2BDvosYY4Y7xv7fkqCP-9A4WBSVdg2_1MQHJg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![gkwelding](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gkwelding/32/1658_2.png) [@gkwelding](https://discuss.elastic.co/u/gkwelding)\
**Post date:** [April 3, 2014, 8:53am UTC](https://discuss.elastic.co/t/mixing-bool-and-multi-match-function-score-query/16771/3 "2014-04-03T08:53:53Z")

</div>

Hey, thanks for getting back to me, but with a little more thought I  
actually figured this out. What I needed was this...

```
{
    "query":{
        "function_score":{
            "query":{
                "bool":{
                    "should": [
                       {
                           "range": {
                              "allBoost": {
                                 "gt": 0
                              }
                           }
                       },{
                            "multi_match":{
                                "query":"some search query",
                                "fields":[
                                    "id",
                                    "name",
                                    "description",
                                    "category"
                                ]
                            }
                       }
                    ]
                }
            },
            "functions":[
                {
                    "filter":{
                        "range": {
                            "allBoost": {
                                "gt": 0
                            }
                        }
                    },
                    "script_score":{
                        "script":"doc['allBoost'].value"
                    }
                },
                {
                    "filter":{
                        "range": {
                            "allBoost": {
                                "lte": 0
                            }
                        }
                    },
                    "script_score":{
                        "script":"_score"
                    }
                }
            ],
            "boost_mode": "replace"
        }
    }
}

```

What was missing was the knowledge that you could nest multi field queries  
within bool queries...

On Thursday, April 3, 2014 9:45:06 AM UTC+1, Britta Weber wrote:

> Hi,
> 
> I am unsure I got that right - the query in the function\_score can be  
> any query, also a bool query. If you want documents that do not match  
> the above query, but some other criterion (for example range query) to  
> be scored with the function\_score functions you can just replace the  
> query\_string with a  
> "bool" : {"should": [{"query\_string:{...}"}, {"range": {...}}]}.  
> Was that your question?  
> If not, could you elaborate a little more on what you want to achieve?
> 
> On Wed, Apr 2, 2014 at 5:01 PM, Garry Welding \<[gkwe...@gmail.com](mailto:gkwe...@gmail.com)\<javascript:\>\>  
> wrote:
> 
> > I'm currently doing a query that's a mix of multi match and function  
> > score.  
> > The important bit of the JSON looks like this:
> > 
> > ```
> > "function_score":{ 
> > "query":{ 
> > "query_string":{ 
> > "query":"some query", 
> > 
> > ```
> > 
> > "fields":["id","name","strippedDescription","colourSearch","sizeSearch"]  
> > }  
> > }  
> > }
> > 
> > However, I also want to include results that don't necessarily match the  
> > query but have a particular numeric value that's greater than 0. I think  
> > a  
> > bool query would do this, but I don't know how to use a bool query with  
> > a  
> > function score query.
> > 
> > I understand that a multi match query is just shorthand for a bool  
> > query,  
> > and I could expand out the multi match query into its bool counter-part,  
> > however, I then don't know how I would do function score within that.
> > 
> > Any ideas? I'm on version 1.1.0 by the way.
> > 
> > --  
> > 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/4279a874-eef8-47ec-9f49-f91efed1f851%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4279a874-eef8-47ec-9f49-f91efed1f851%40googlegroups.com).
> 
> > 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/e1d8c456-d62a-498e-a805-ac40e40131d5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e1d8c456-d62a-498e-a805-ac40e40131d5%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, 1:38am UTC](https://discuss.elastic.co/t/mixing-bool-and-multi-match-function-score-query/16771/4 "2017-07-06T01:38:36Z")

</div>


