The following 3 patches implement this feature. Internally the message object learns about flags. Only a single flag is defined to denote that a message matched the search expression.
That flag is then rendered on the "message{" line in the output of
notmuch show like this:
message{ id:... depth:4 match:0 filename:...
message{ id:... depth:4 match:1 filename:...
This can now be used by UI interfaces to hide or collapse less
interesting messages.
-Bart
