Sub topic 15
Troubleshooting missing or unexpected results
This chapter assumes you can apply tags, build a smart folder, and run basic Terminal commands as covered earlier. It gives you a way to work out why a tag search or smart folder shows too few, too many, or the wrong items.
The working model
Tag searches and smart folders do not look inside folders. They ask the Spotlight index, a database of file metadata, for items that match. If an item is missing from the results, the cause is almost always one of three things:
- The item is not in the index, or its index entry is out of date.
- The item is in the index, but the criteria or search scope do not match it.
- The item matches, but Finder is hiding it.
Step 1: Check the metadata on the file
Start with the item itself. This command prints the tags Spotlight has stored for it.
mdls -name kMDItemUserTags ~/Documents/report.pdfIf the output is (null) but Finder shows a tag, the index entry is stale. If the output lists the tag, the index is fine and the problem is in the search.
Step 2: Run the query outside Finder
Test the same condition in Terminal. This removes the smart folder's scope and display settings from the picture.
mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red"'The tag name is case-sensitive in this form, and it must match the name exactly. A renamed tag, such as "Red" changed to "Urgent", no longer matches the old name. Check the spelling against the tag as it appears in Finder Settings.
For date-based smart folders, test the date criterion the same way.
mdfind -onlyin ~/Documents 'kMDItemFSContentChangeDate >= $time.today(-7)'Common causes
| Symptom | Likely cause | Fix |
|---|---|---|
| A tagged file never appears | Its folder is excluded from Spotlight | Open System Settings, go to Spotlight, then Search Privacy, and remove the folder from the list |
| Results are missing on an external drive | The drive is not indexed | Check that indexing is on for the volume, and give it time to finish |
| A smart folder shows nothing | Scope is set to a folder that does not contain the files | Change the scope from the current folder to This Mac |
| Too many results | Criteria are joined as "any" instead of "all" | Edit the smart folder and change the match setting |
| Files you expect are absent | They are hidden or inside a package | Add a criterion for the Other attribute, or search by file name |
| An old tag name still shows | The sidebar tag list is out of date | Reopen the Finder window, or relaunch Finder |
Rebuilding the index
When many items are wrong, the index may be damaged. You can rebuild it for one volume. Reindexing can take a long time and slows the Mac while it runs, so do it when you can leave the machine alone.
sudo mdutil -E /The -E option erases the index and starts a new one. Use mdutil -s / first to see whether indexing is enabled.
Check the saved file
If a smart folder behaves oddly, read its stored search. Edits in Finder are saved into this file, so it shows what the folder really queries.
plutil -p ~/Library/Saved\ Searches/Recent\ Documents.savedSearchLook at the query string and the search scope. A scope that points to a folder you have since moved is a common cause of empty results.
Exercise
A smart folder for the tag "Project" is empty. mdls shows the tag on your file, but mdfind with the same tag returns nothing for that file. What do you check next?
Show answer
Check the query. Look for a spelling or case mismatch in the tag name, and confirm that the -onlyin folder actually contains the file. If the query is correct, check whether the folder is in the Spotlight Search Privacy list.