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

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!

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.

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" }}
]
}
}
}

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.

Ah... yep. Just needed to use match instead of term. :slight_smile: