Sub topic 1
What Spotlight Is and How It Works
Prerequisites
This chapter assumes you can open apps, use Finder, and press keyboard shortcuts. You should also know roughly where your documents and downloads are stored. The short Terminal commands near the end are optional. You only need basic Terminal use to run them.
What Spotlight is
Spotlight is the search built into macOS. One search box finds several kinds of things:
- Apps and System Settings panes
- Files and folders, by name and by what is inside them
- Messages, mail, contacts, calendar events, and notes, depending on the apps you use
- Quick answers, such as calculations, unit conversions, and word definitions
Spotlight does not scan your disk each time you type. It searches a prebuilt database called the index, which is why results appear as you type.
How it works: the index
The index is a database that macOS keeps for each volume. A volume is a storage area such as your startup disk. It stores two things about each item.
- Content. For documents, PDFs, mail, and notes, the index records the words inside them. This is why searching for a phrase can find a file whose name does not contain it.
- Metadata. Metadata is information about a file rather than its text. Examples are the name, the kind, the creation date, the author, and the dimensions of an image.
Background processes keep the index current. When you save, move, or delete a file, macOS updates the matching index entries shortly afterward. A new file may take a moment to appear in results. A freshly installed system, or a rebuilt index, can take much longer, because everything must be read again.
Calculations, unit conversions, and definitions do not come from your files. Spotlight works them out or looks them up when you type them, then shows them alongside the indexed results.
Seeing the index from Terminal
You can look at the same data Spotlight uses. Two commands show the idea. Later chapters cover them in more depth.
mdutil -s /
mdls ~/Documents/report.pdfThe first command reports whether indexing is enabled for the startup volume. The second prints the metadata attributes stored for one file, so replace the path with a file you have. The attribute names it shows, such as kMDItemContentType, are the same ones used in advanced searches.
What is not indexed
Spotlight skips some locations by design. Examples are system folders that hold no user content and any folder you have excluded in Spotlight settings. Some file types also have no readable text, so Spotlight can match them only by metadata such as the name. When a file you expect is missing from the results, the cause is usually one of these:
- The folder is excluded in settings.
- The index is still being built.
- The index is stale or damaged and needs a rebuild.
Later chapters cover each of these in turn.
Decisions that follow from this model
- Search by content or by name. Content searches work only for formats Spotlight can read, so use a file name when searching for items such as images.
- Privacy. Excluding a folder keeps its items out of Spotlight results. This is a setting you control, and later chapters show where it is.
- Cost of rebuilding. Rebuilding the index is a fix of last resort. It uses extra CPU and takes time.
Exercise
A colleague says "Spotlight is slow to find a file I saved a minute ago, so it must be scanning my whole disk every time." What is wrong with this explanation, and what is the likely real cause?
Show answer
Spotlight does not scan the disk per search. It queries a prebuilt index of file content and metadata. A file saved a minute ago may not be in the index yet, because background indexing updates entries shortly after a change. The delay comes from indexing and not from searching.