The second patch in particular could be cleaned up; there is definitely some duplication between the two code paths, so opening the database and creating a query could/should be refactored. It seems somewhat reasonable to have notmuch_query_search_tags in the API. I would say just document the limitation of ignoring excludes when querying for all tags, otherwise that optimization (which is a huge speed improvement) will mostly be useless.