Should Query with Filter

Hello everybody!

Given that types will be dropped in future releases I wanted to move them in a special field for each document (as stated here).

Now for the search query I'll have something like this:

{
    "query": {
        "bool" : {
            "should" : [
                { "match_phrase": { "field1": { "query": "test", "_name": "field1" }}},
                { "match": { "field2": { "query": "test", "_name": "field2"}}},
                { "match_phrase": { "field3": { "query": "test", "_name": "field3"}}},
                { "match": { "field4": { "query": "test", "_name": "field4"}}},
                { "match": { "field5": { "query": "test", "_name": "field5"}}}
            ],
            "filter": {
              "match": {
                "type_field": "important_type_name"
              }
            }
        }
    }
}

The only addition from what I used to have here is the filter for the exact match on the custom field type_field, which has keyword as type in its mapping.

The problem now is that this search will include all documents that have "type_field" = "important_type_name". As stated here in the should section, the should will only act in influencing the score.

How can I make a query that uses the should bool query but that has a strict filter on it? i.e. I want to use the capabilities of should (with fuzziness, boosting and match or match_phrase) on multiple fields but still be sure that it looks only through some already filtered documents, and all the final results will have matched_queries (that's why I used the named queries, to show that right now, some resulted documents don't have any matched queries but are still part of the final result).

Thanks very much in advance!

I think that setting minimum_should_match: 1 in the should array would help here.

See https://www.elastic.co/guide/en/elasticsearch/reference/6.0/query-dsl-bool-query.html

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