Sub topic 4
How Spotlight metadata makes searching possible
This chapter assumes you can navigate Finder, know the difference between files and folders, and are comfortable using menus. You do not need Terminal experience, but a few optional commands appear so you can see the data directly.
The index behind every search
Tags and smart folders do not scan your disk when you use them. They ask Spotlight, the macOS search service, which keeps an index. An index is a database that records facts about each file so macOS can answer a question without opening every file.
Those facts are called metadata, which means data about a file rather than its contents. Each fact is stored under an attribute name. Spotlight attribute names start with kMDItem.
Common attributes include:
| Attribute | What it records |
|---|---|
| `kMDItemDisplayName` | The file name shown in Finder |
| `kMDItemKind` | The file type, such as PDF Document |
| `kMDItemFSContentChangeDate` | When the file contents last changed |
| `kMDItemUserTags` | The tags you applied to the file |
Because tags are stored as an attribute, a tag behaves like any other searchable property. This is why you can organise files without moving them. The file stays in its folder, and the label travels with it.
Two sources of metadata
Spotlight gathers metadata in two ways:
- From the file system. Name, size, and dates come from the disk. Tags are stored with the file and read into the index.
- From the contents. Importer plug-ins read files such as PDFs, documents, and photos. They record text, authors, and camera details.
The index is updated in the background as files change. A new or edited file can take a moment to appear in results.
The diagram shows why the three features work together. They all read from the same index, so they agree with each other.
Looking at metadata yourself
You can read the stored metadata from Terminal. The mdls command lists the attributes of one file. This command prints only the tags:
mdls -name kMDItemUserTags ~/Documents/report.pdfIf the file has the tag Red, the output looks like this:
kMDItemUserTags = (
Red
)A file with no tags prints kMDItemUserTags = (null). Finder reads this same data when you search by tag.
The mdfind command goes the other way. You give it a condition, and it returns the matching files. Later chapters use it in more detail.
Common errors at this stage
- A new tag does not show up in a search. The index may not have caught up yet. Wait a short time and search again.
- A file shows
(null)for a tag you applied. Check that you ranmdlson the right file path, and that the tag was applied to that file and not to a copy. - Results are missing from one folder. That folder may be excluded from Spotlight. Check the privacy list.
Finding the cause of missing results is covered in the troubleshooting chapter. For now, remember the model: files carry metadata, Spotlight indexes it, and Finder features query the index.
Exercise
Why can a tag search find a file in any folder without scanning your disk?
Show answer
Tags are stored as the metadata attribute kMDItemUserTags, and Spotlight keeps that attribute in its index. A tag search queries the index instead of opening files, so the file's folder does not matter.
Exercise
Which command shows the tags on ~/Documents/report.pdf, and what does it print if the file has no tags?
Show answer
Run mdls -name kMDItemUserTags ~/Documents/report.pdf. For a file with no tags it prints kMDItemUserTags = (null).