# Combine a filter while scoring documents that both match and DONT match

**URL:** https://discuss.elastic.co/t/combine-a-filter-while-scoring-documents-that-both-match-and-dont-match/57625
**Category:** Elasticsearch
**Created:** [August 9, 2016, 7:53pm UTC](https://discuss.elastic.co/t/combine-a-filter-while-scoring-documents-that-both-match-and-dont-match/57625 "2016-08-09T19:53:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MatthewHartz](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MatthewHartz](https://discuss.elastic.co/u/MatthewHartz)
#### Post date: [August 9, 2016, 7:53pm UTC](https://discuss.elastic.co/t/combine-a-filter-while-scoring-documents-that-both-match-and-dont-match/57625/1 "2016-08-09T19:53:33Z")

</div>

I have a query that returns all documents that contain a coordinate within their geospacial location.

GET /location/operatinglocation/\_search  
{  
"query": {  
"bool": {  
"must": {  
"match\_all": {}  
},  
"filter": {  
"geo\_shape": {  
"location": {  
"shape": {  
"type": "point",  
"coordinates":  
[-117.7901088,33.6913751]  
},  
"relation": "contains"  
}  
}  
}  
}  
}  
}

Now I also what to be able to score these documents based on a relevant field such as "businessname", but at the same time, don't filter out the documents that don't contain the name, just give them a lower score.

Any suggestions?

Thanks!

---

<div class="post-metadata">

### Author: ![mikemccand](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mikemccand](https://discuss.elastic.co/u/mikemccand)
#### Post date: [August 9, 2016, 8:23pm UTC](https://discuss.elastic.co/t/combine-a-filter-while-scoring-documents-that-both-match-and-dont-match/57625/2 "2016-08-09T20:23:54Z")

</div>

Can't you just add your "businessname" sub-query as a `should` clause into your `bool` query?

I think you can remove the `must` `match_all` clause too.

---

<div class="post-metadata">

### Author: ![MatthewHartz](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MatthewHartz](https://discuss.elastic.co/u/MatthewHartz)
#### Post date: [August 9, 2016, 8:52pm UTC](https://discuss.elastic.co/t/combine-a-filter-while-scoring-documents-that-both-match-and-dont-match/57625/3 "2016-08-09T20:52:10Z")

</div>

I thought so, but the two results for example that do come back have the same score. If I leave out the match all clause, they both have a score of 0. If I include it, they have a score of .25. One being "Oracle" another being "McDonalds". The value i'm searching with is Oracle.

GET /location/operatinglocation/\_search  
{  
"query": {  
"bool": {  
"must": {  
"match\_all": {}  
},  
"filter": {  
"geo\_shape": {  
"location": {  
"shape": {  
"type": "point",  
"coordinates":  
[-117.7901088,33.6913751]  
},  
"relation": "contains"  
}  
}  
},  
"should": [  
{"term" : { "businessname": "Oracle" }}  
]  
}  
}  
}

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [August 9, 2016, 8:55pm UTC](https://discuss.elastic.co/t/combine-a-filter-while-scoring-documents-that-both-match-and-dont-match/57625/4 "2016-08-09T20:55:24Z")

</div>

I agree with Mike you should remove the match\_all which only adds a fixed constant to the score of all docs. Also if your `businessname` field is analyzed, you should use a `match` query rather than a `term` query.

---

<div class="post-metadata">

### Author: ![MatthewHartz](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MatthewHartz](https://discuss.elastic.co/u/MatthewHartz)
#### Post date: [August 9, 2016, 9:00pm UTC](https://discuss.elastic.co/t/combine-a-filter-while-scoring-documents-that-both-match-and-dont-match/57625/5 "2016-08-09T21:00:51Z")

</div>

Ah... yep. Just needed to use match instead of term. 🙂

---

<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 5, 2017, 10:29pm UTC](https://discuss.elastic.co/t/combine-a-filter-while-scoring-documents-that-both-match-and-dont-match/57625/6 "2017-07-05T22:29:02Z")

</div>


