# Boosting certain conditions

**URL:** <https://discuss.elastic.co/t/boosting-certain-conditions/9580>\
**Category:** Elasticsearch\
**Created:** [November 6, 2012, 1:06pm UTC](https://discuss.elastic.co/t/boosting-certain-conditions/9580 "2012-11-06T13:06:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandon\_Hilkert](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_hilkert/32/2182_2.png) [@Brandon\_Hilkert](https://discuss.elastic.co/u/Brandon_Hilkert)\
**Post date:** [November 6, 2012, 1:06pm UTC](https://discuss.elastic.co/t/boosting-certain-conditions/9580/1 "2012-11-06T13:06:46Z")

</div>

I have the following filtered query right now:

{  
"query": {  
"filtered": {  
"query": {  
"query\_string": {  
"query": "MySQL Apple"  
}  
},  
"filter": {  
"and": [  
{  
"term": {  
"is\_active": true  
}  
},  
{  
"not": {  
"terms": {  
"id": [  
6433746,  
5308541  
]  
}  
}  
},  
{  
"not": {  
"terms": {  
"linkedin\_id": [  
"D-D5EAmm\_J",  
"SxQY\_1y5pA",  
"Nqt6b6We2-"  
]  
}  
}  
},  
{  
"not": {  
"terms": {  
"fb\_user\_id": [  
"120139",  
"503009",  
"610039"  
]  
}  
}  
}  
]  
}  
}  
},  
"size": 9,  
"fields": [  
"id"  
]  
}

As you can see I need the following:

1. Search the \_all field for query text
2. Filter out certain Facebook, Linkedin, and system users

As part of this, I would like to add the following:

Boost certain results when their field "fb\_connections" contains certain  
IDs that I will pass in. Likewise for the "linkedin\_connections" field.

Can I still use a filtered query? If so, what might that addition look  
like? If not, what is the more appropriate query type.

Thanks,  
Brandon

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 6, 2012, 10:53pm UTC](https://discuss.elastic.co/t/boosting-certain-conditions/9580/2 "2012-11-06T22:53:42Z")

</div>

Yes, you can still use filtered query. You just need to wrap your existing  
query into Custom Filters Score query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html)to boost results that match certain conditions. The conditions can be  
expressed as list of filters with corresponding boost factor:

{  
"query": {  
"filtered": {  
"query": {

- 

```
   "custom_filters_score" : {*

```

- 

```
     "query": {*

```

- 

```
       *"query_string": {
        "query": "MySQL Apple"
      },

```

- 

```
       "filters" : [*

```

- 

```
           {*

```

- 

```
             "filter" : { "term" : { "linkedin_connections" : 

```

"DEds324ts34"} },\*

- 

```
             "boost" : "3"*

```

- 

```
           },*

```

- 

```
           {*

```

- 

```
             "filter" : { "term" : { "fb_connections" : "123456"} },*

```

- 

```
             "boost" : "2"*

```

- 

```
           }*

```

- 

```
       ],*

```

- 

```
       "score_mode" : "first" *

```

- 

```
     }*
},
"filter": {
  "and": [
    {
      "term": {
        "is_active": true
      }
    },

```

........... the rest of your query .............

On Tuesday, November 6, 2012 8:06:46 AM UTC-5, Brandon Hilkert wrote:

> I have the following filtered query right now:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "query\_string": {  
> "query": "MySQL Apple"  
> }  
> },  
> "filter": {  
> "and": [  
> {  
> "term": {  
> "is\_active": true  
> }  
> },  
> {  
> "not": {  
> "terms": {  
> "id": [  
> 6433746,  
> 5308541  
> ]  
> }  
> }  
> },  
> {  
> "not": {  
> "terms": {  
> "linkedin\_id": [  
> "D-D5EAmm\_J",  
> "SxQY\_1y5pA",  
> "Nqt6b6We2-"  
> ]  
> }  
> }  
> },  
> {  
> "not": {  
> "terms": {  
> "fb\_user\_id": [  
> "120139",  
> "503009",  
> "610039"  
> ]  
> }  
> }  
> }  
> ]  
> }  
> }  
> },  
> "size": 9,  
> "fields": [  
> "id"  
> ]  
> }
> 
> As you can see I need the following:
> 
> 1. Search the \_all field for query text
> 2. Filter out certain Facebook, Linkedin, and system users
> 
> As part of this, I would like to add the following:
> 
> Boost certain results when their field "fb\_connections" contains certain  
> IDs that I will pass in. Likewise for the "linkedin\_connections" field.
> 
> Can I still use a filtered query? If so, what might that addition look  
> like? If not, what is the more appropriate query type.
> 
> Thanks,  
> Brandon

--

---

<div class="post-metadata">

**Author:** ![Brandon\_Hilkert](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_hilkert/32/2182_2.png) [@Brandon\_Hilkert](https://discuss.elastic.co/u/Brandon_Hilkert)\
**Post date:** [November 6, 2012, 11:07pm UTC](https://discuss.elastic.co/t/boosting-certain-conditions/9580/3 "2012-11-06T23:07:31Z")

</div>

Thanks Igor. Would I still get results that are outside the filter? I  
really just want that boosting to strengthen the results, but don't want it  
to limit it to just those.

On Tue, Nov 6, 2012 at 5:53 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> Yes, you can still use filtered query. You just need to wrap your existing  
> query into Custom Filters Score query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html)to boost results that match certain conditions. The conditions can be  
> expressed as list of filters with corresponding boost factor:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {
> 
> - 
> 
> ```
> "custom_filters_score" : {*
> 
> ```
> 
> - 
> 
> ```
> "query": {*
> 
> ```
> 
> - 
> 
> ```
> *"query_string": {
> "query": "MySQL Apple"
> },
> 
> ```
> 
> - 
> 
> ```
> "filters" : [*
> 
> ```
> 
> - 
> 
> ```
> {*
> 
> ```
> 
> - 
> 
> ```
> "filter" : { "term" : { "linkedin_connections" :
> 
> ```
> 
> "DEds324ts34"} },\*
> 
> - 
> 
> ```
> "boost" : "3"*
> 
> ```
> 
> - 
> 
> ```
> },*
> 
> ```
> 
> - 
> 
> ```
> {*
> 
> ```
> 
> - 
> 
> ```
> "filter" : { "term" : { "fb_connections" : "123456"} },
> 
> ```
> 
> - 
> - 
> 
> ```
> "boost" : "2"*
> 
> ```
> 
> - 
> 
> ```
> }*
> 
> ```
> 
> - 
> 
> ```
> ],*
> 
> ```
> 
> - 
> 
> ```
> "score_mode" : "first" *
> 
> ```
> 
> - 
> 
> ```
> }*
> },
> "filter": {
> "and": [
> {
> "term": {
> "is_active": true
> }
> },
> 
> ```
> 
> ........... the rest of your query .............
> 
> On Tuesday, November 6, 2012 8:06:46 AM UTC-5, Brandon Hilkert wrote:
> 
> > I have the following filtered query right now:
> > 
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "query\_string": {  
> > "query": "MySQL Apple"  
> > }  
> > },  
> > "filter": {  
> > "and": [  
> > {  
> > "term": {  
> > "is\_active": true  
> > }  
> > },  
> > {  
> > "not": {  
> > "terms": {  
> > "id": [  
> > 6433746,  
> > 5308541  
> > ]  
> > }  
> > }  
> > },  
> > {  
> > "not": {  
> > "terms": {  
> > "linkedin\_id": [  
> > "D-D5EAmm\_J",  
> > "SxQY\_1y5pA",  
> > "Nqt6b6We2-"  
> > ]  
> > }  
> > }  
> > },  
> > {  
> > "not": {  
> > "terms": {  
> > "fb\_user\_id": [  
> > "120139",  
> > "503009",  
> > "610039"  
> > ]  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > },  
> > "size": 9,  
> > "fields": [  
> > "id"  
> > ]  
> > }
> > 
> > As you can see I need the following:
> > 
> > 1. Search the \_all field for query text
> > 2. Filter out certain Facebook, Linkedin, and system users
> > 
> > As part of this, I would like to add the following:
> > 
> > Boost certain results when their field "fb\_connections" contains certain  
> > IDs that I will pass in. Likewise for the "linkedin\_connections" field.
> > 
> > Can I still use a filtered query? If so, what might that addition look  
> > like? If not, what is the more appropriate query type.
> > 
> > Thanks,  
> > Brandon
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 6, 2012, 11:16pm UTC](https://discuss.elastic.co/t/boosting-certain-conditions/9580/4 "2012-11-06T23:16:31Z")

</div>

Yes, this is what Custom Filters Score is doing. It uses these filters to  
boost results, not to limit them.

On Tuesday, November 6, 2012 6:07:54 PM UTC-5, Brandon Hilkert wrote:

> Thanks Igor. Would I still get results that are outside the filter? I  
> really just want that boosting to strengthen the results, but don't want it  
> to limit it to just those.
> 
> On Tue, Nov 6, 2012 at 5:53 PM, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > Yes, you can still use filtered query. You just need to wrap your  
> > existing query into Custom Filters Score query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html)to boost results that match certain conditions. The conditions can be  
> > expressed as list of filters with corresponding boost factor:
> > 
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {
> > 
> > - 
> > 
> > ```
> > "custom_filters_score" : {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "query": {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > *"query_string": {
> > "query": "MySQL Apple"
> > },
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "filters" : [*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "filter" : { "term" : { "linkedin_connections" : 
> > 
> > ```
> > 
> > "DEds324ts34"} },\*
> > 
> > - 
> > 
> > ```
> > "boost" : "3"*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > },*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "filter" : { "term" : { "fb_connections" : "123456"} 
> > 
> > ```
> > 
> > },\*
> > 
> > - 
> > 
> > ```
> > "boost" : "2"*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > }*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > ],*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "score_mode" : "first" *
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > }*
> > },
> > "filter": {
> > "and": [
> > {
> > "term": {
> > "is_active": true
> > }
> > },
> > 
> > ```
> > 
> > ........... the rest of your query .............
> > 
> > On Tuesday, November 6, 2012 8:06:46 AM UTC-5, Brandon Hilkert wrote:
> > 
> > > I have the following filtered query right now:
> > > 
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "query\_string": {  
> > > "query": "MySQL Apple"  
> > > }  
> > > },  
> > > "filter": {  
> > > "and": [  
> > > {  
> > > "term": {  
> > > "is\_active": true  
> > > }  
> > > },  
> > > {  
> > > "not": {  
> > > "terms": {  
> > > "id": [  
> > > 6433746,  
> > > 5308541  
> > > ]  
> > > }  
> > > }  
> > > },  
> > > {  
> > > "not": {  
> > > "terms": {  
> > > "linkedin\_id": [  
> > > "D-D5EAmm\_J",  
> > > "SxQY\_1y5pA",  
> > > "Nqt6b6We2-"  
> > > ]  
> > > }  
> > > }  
> > > },  
> > > {  
> > > "not": {  
> > > "terms": {  
> > > "fb\_user\_id": [  
> > > "120139",  
> > > "503009",  
> > > "610039"  
> > > ]  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > },  
> > > "size": 9,  
> > > "fields": [  
> > > "id"  
> > > ]  
> > > }
> > > 
> > > As you can see I need the following:
> > > 
> > > 1. Search the \_all field for query text
> > > 2. Filter out certain Facebook, Linkedin, and system users
> > > 
> > > As part of this, I would like to add the following:
> > > 
> > > Boost certain results when their field "fb\_connections" contains certain  
> > > IDs that I will pass in. Likewise for the "linkedin\_connections" field.
> > > 
> > > Can I still use a filtered query? If so, what might that addition look  
> > > like? If not, what is the more appropriate query type.
> > > 
> > > Thanks,  
> > > Brandon
> > 
> > --

--

---

<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:05am UTC](https://discuss.elastic.co/t/boosting-certain-conditions/9580/5 "2017-07-06T03:05:49Z")

</div>


