Exclude specific fields from multimatch query

Hello everyone,

Is there a way to perform a multi match query in all fields but exclude only certain fields from being considered in the clause?
We have a structure with dynamic indexes and we don't know the fields at query time, but we have restrictions with respect to some fields (and that also is dynamic).

For example, we need to search the text "John Doe" that could be in any field but we have to exclude the field nickname from being searched

Right now our query looks like this

{
    "query": {
        "bool": {
             "must": [
                 {
                     "multi_match": {
                         "query" : "john doe",
                         "type" : "most_fields",
                         "fields" : [],
                         "fuzzyness" : "AUTO"
                     }
                 }
             ],
             "must_not": [
                 {
                     "multi_match": {
                         "query" : "john doe",
                         "type" : "most_fields",
                         "fields" : ["nickname"],
                         "fuzzyness" : "AUTO"
                     }
                 }
             ]
        }
    }
}

This approach satisfies our requirement not matching a document with these values

{
    "name": "peter something",
    "nickname": "john doe",
    ...
}

But the problem is that this document will not match either

{
    "name": "john doe",
    "nickname": "john doe",
    ...
}

and we need it to match as "john doe" is found in the name field, which falls into the "all the other fields with no restriction" category

Any help will be much appreciated :slight_smile:

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