Use case or problem
Searching for tags when using nesting usually works fine. But if you want to search for:
- a specific tag, and not include child/nested tags;
- a “path element”/specific sub-tag in a nested tag; or
- specific substrings in a nested tag,
you must ignore the tag: search operator entirely, because its sole method of operation is to search for a specific (potentially nested) tag, and any nested/sub-tags under that one. This can be tedious if your vault is huge, as you no longer gain the speed benefit of the tag cache. You must instead use either regex or plain text search, both of which have their problems w/r/t this sort of use-case, and both are much slower than tag:.
For example as to why this is bad: while I find this unlikely, it’s possible your tags are encoded weirdly in your frontmatter, given you can use any valid YAML syntax. Take a look at this:
Again, not saying this is likely, but it is technically possible to do this—the tag in the note is both missing its hashtag (it’s added automatically by Obsidian when parsing the tag item) and contains funky extra characters that are not represented in the tag as it is shown in Obsidian. Given that your note can have different representations of the same tag, depending on how you added it to the file, that makes plain text and regex searches possibly unreliable.
I wrote in-depth about this topic here.
Proposed solutions
My solutions are 100% backwards compatible, as the current tag: search operator explicitly denies any syntax that does not exactly match a tag, and none of the below do. Some of these are inspired by what people have tried in other forum posts and expected to work, only to find the operator does not work like any of the other operators.
1. Exact match
Wrap the value provided to the search operator in double quotes, to do an exact match. This is the same as plain text searches that use double quotes for the same purpose.
So, if you want to search for #books but not any nested tags under it, you would search for:
tag: “#books”
And just to be clear, tag: #books does not satisfy this use-case, as it will return nested tags, such as #books/fiction.
2. Return nested tags only
Let’s say you want to find only the nested tags under #books, but not anything tagged with that specific tag. You could use:
tag: #books/
3. Substring match
Given that the tag: operator requires the hashtag at the beginning, it would be trivial to consider any searches made without it to be substring searches for tags.
So a search for:
tag: book
would return #books, as well as #tutorials/book-writing.
Current workaround (optional)
Regular expressions or plain text search, but both have huge issues that make dealing with them a pain, depending on the makeup of your vault. You definitely can work around the issues by massaging your note content, but if your vault is huge, it will be slow and there’s nothing you can do about it.
Related feature requests (optional)
There are many over the years that have asked for similar features, but not exactly as I propose. Most of them are closed.
