How do I combine multiple filters with OR without affecting the score?

I want to filter my results with two different clauses aggregated with OR e.g. return documents with PropertyA=true OR PropertyB=true.

I've been trying to do this using a bool query. My base query is just a text search in must . If I put both clauses in the filter occurrence type, it aggregates them with an AND. If I put both clauses in the should occurrence type with minimum_should_match set to 1, then I get the right results. But then, documents matching both conditions get a higher score because "should" runs in a query context.

How do I filter to only documents matching either of two conditions, without increasing the score of documents matching both conditions?

Thanks in advance

Hi @chm Welcome to the community.

I just showed how to do this here

Look at the 3 posts following .. the first solution I put the OR in the query context , the 2nd I put the OR in the filter context... Which is what you are asking about.

There is a complete whole solution there.

I short an OR in a filter looks like this

GET discuss-test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "term": {
                  "type": "canis"
                }
              },
              {
                "term": {
                  "name": "mary"
                }
              }
            ]
          }
        }
      ]
    }
  }
}
1 Like

Thank you this looks perfect! Any ideas how to do that in NEST? It looks like the Filter property on BoolQuery only takes an IEnumerable<QueryContainer>.

Edit Nevermind, got it right after I posted:

            QueryContainer inner = new BoolQuery
			{
				Should = new[] {qc1, qc2},
				MinimumShouldMatch = new MinimumShouldMatch(1)
			};

			var outer = new BoolQuery
			{
				MustNot = new[] {qc3, qc4},
				Must = new[] {qc5},
				Filter = new[] {inner}
			};
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.