Search highlights a non-matched field if query contains a nested query

Hello, I am having trouble with the highlight-feature highlighting fields that should not be. This problem only occurs if the search contains a nested query.

I have set require_field_match to true in my search and my ES cluster is using version 9.4.4. I believe this is a bug, but I want to confirm that I'm not doing anything wrong. :sweat_smile:

Here is an example index:

PUT test
{
  "mappings": {
    "properties": {
      "num": { "type": "long" },
      "str1": { "type": "text" },
      "str2": { "type": "text" },
      "lst": {
        "type": "nested",
        "properties": {
          "val": { "type": "long" }
        }
      }
    }
  }
}

Data:

POST test/_create/1
{
  "num": 1,
  "str1": "foo",
  "str2": "bar",
  "lst": [
    { "val": 1 }
  ]
}

Search:

GET test/_search
{
  "highlight": {
    "require_field_match": true,
    "fields": {
      "str1": {},
      "str2": {}
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "filter": [
                    {
                      "term": {
                        "num": 0 // num = 0 does not exist, "foo" should not be highlighted.
                      }
                    },
                    {
                      "match": {
                        "str1": "foo"
                      }
                    }
                  ]
                }
              },
              {
                "match": {
                  "str2": "bar" // "bar" exists.
                }
              }
            ]
          }
        },
        {
          // Removing this nested query will correctly only highlight "bar".
          "nested": {
            "path": "lst", 
            "query": {
              "match": {
                "lst.val": 1
              }
            }
          }
        }
      ]
    }
  }
}

Result:

{
    // ...
    "hits": [
        // ...
        "highlight": {
          "str1": [
            "<em>foo</em>" // "foo" should not be highlighted.
          ],
          "str2": [
            "<em>bar</em>"
          ]
        }
      }
    ]
  }
}

Now that I am re-reading the docs, it does say:

Highlighters don’t reflect the boolean logic of a query when extracting terms to highlight. Thus, for some complex boolean queries (e.g nested boolean queries, queries using minimum_should_match etc.), parts of documents may be highlighted that don’t correspond to query matches.

So, is this a known limitation that cannot be fixed?