Sub topic 12
The index and file metadata
This chapter assumes you can open Spotlight, search for files, and use Terminal at a basic level. Here you will learn what Spotlight stores when it indexes a volume, why searches return instantly, and why some files never appear.
How the index works
Spotlight does not scan your disk when you type. It keeps a database called the index and searches that. The index holds two kinds of data for each file:
- Content: the words inside documents such as PDFs, Pages files, emails, and plain text.
- Metadata: facts about the file, such as its name, kind, size, dates, and author.
macOS builds the index in the background after setup or after you add a new disk. It then updates the index as files change. A background process called mds watches for changes, and helper processes called importers read each file and extract its text and metadata. Each file type has its own importer, so Spotlight can read a PDF's text but not the inside of an unknown file format.
Metadata attributes
Every indexed file carries named attributes. Some common ones are:
| Attribute | Meaning |
|---|---|
| `kMDItemDisplayName` | The name shown in Finder |
| `kMDItemContentType` | The file's type, such as `com.adobe.pdf` |
| `kMDItemFSSize` | Size in bytes |
| `kMDItemContentModificationDate` | When the content last changed |
| `kMDItemAuthors` | Authors recorded inside the document |
These attributes are what the search keywords such as kind: and name: rely on. They are also what Terminal queries use, which a later chapter covers.
Inspecting a file's metadata
The mdls command prints the attributes Spotlight has stored for one file. Replace the path with a file of your own.
mdls ~/Documents/report.pdfThe output is a list of name = value lines. Look for kMDItemContentType and kMDItemTextContent. Not every file has every attribute. A plain text file has no author, for example, so that attribute shows (null).
Checking index status
To see whether indexing is on for the startup volume, run:
mdutil -s /The output is usually Indexing enabled. If it says indexing is disabled, Spotlight cannot find anything on that volume. You can check other volumes by replacing / with a path such as /Volumes/Backup.
What is not indexed
Some files are left out, and this explains many "missing result" cases:
- Folders you added to the exclusion list in Spotlight settings.
- Volumes where indexing is turned off.
- Files with a format no importer understands, which are still found by name but not by content.
- System files and some library folders, which macOS skips by default.
Common errors
- Searching immediately after a big copy. The index updates in the background, so new files may take a few minutes to appear. Wait, then search again.
- Assuming Spotlight reads every file type. Unusual formats are matched on metadata only.
- Reading
(null)as a problem. It means the attribute is not set for that file.
Exercise
You run mdutil -s / and it reports that indexing is enabled. A search for a word inside a recent .pdf finds nothing, but searching for the PDF's file name works. What does this suggest, and which command would you use to check what Spotlight stored for that file?
Show answer
Spotlight indexed the file's name and metadata, but its content was not extracted or not yet indexed. This could be a timing delay or a PDF with no readable text, such as a scanned image. Run mdls on the file and check whether kMDItemTextContent has a value.