Sub topic 1
What Spotlight is and how it works
Spotlight is the search system built into macOS. It finds apps, files, folders, messages, contacts, calendar events, and settings. It also answers quick questions, such as arithmetic, unit conversions, and word definitions. This chapter explains what Spotlight is made of and how it produces results. The later chapters cover the details of using it.
Prerequisites
This chapter assumes you can do the following:
- Open apps and use Finder on a Mac.
- Use common keyboard shortcuts such as Command-Space.
- Say roughly where your documents and downloads are stored.
- Run a short command in Terminal. This is needed only for the optional commands near the end. Everything else works without Terminal.
What Spotlight does
Spotlight combines several kinds of search behind one box.
- Local search looks through your Mac: apps, documents, folders, mail, messages, contacts, calendar items, and system settings.
- Quick answers calculate expressions, convert units, and show dictionary definitions directly in the results.
- Suggestions can include results from the web and from other sources, depending on your settings.
You get one list of results grouped by category, so you rarely need to decide in advance which kind of thing you are looking for.
The three parts: index, metadata, and query
Spotlight does not read every file when you search. That would be far too slow. Instead it works in three parts.
- The index. A background process reads your files ahead of time and builds a database of what they contain. Searching means looking things up in this database, which is why results appear as you type.
- Metadata. For each file, the index stores facts about it in addition to its text. These facts are called metadata. Examples are the file name, the kind of file, the creation and modification dates, the author, and the page count of a PDF.
- The query. What you type is matched against both the file contents and the metadata. A query can therefore be as simple as a word or as specific as "PDF files modified this week".
The index is kept up to date by macOS. When you save, move, rename, or delete a file, the system notifies Spotlight, and the index entry changes shortly afterward.
What is indexed
By default Spotlight indexes the startup volume and any other volume you attach, with some exceptions.
| Item | Typically indexed? | Notes |
|---|---|---|
| Documents, PDFs, text files | Yes | Both names and contents are searched |
| Apps and system settings | Yes | These are what you launch from Spotlight |
| Mail, Messages, Contacts, Calendar | Yes | Depends on the result categories you enable |
| Folders you exclude in settings | No | You choose these in System Settings |
| System and temporary areas | Mostly no | macOS skips locations that are not useful to search |
| Files whose type Spotlight cannot read | Name and basic metadata only | Contents cannot be searched |
A file type is searchable by content only when macOS has an importer for it. An importer is a small piece of software that knows how to extract text and metadata from one file format. Common formats such as PDF, Word documents, plain text, and images with embedded text are covered. An unusual or proprietary format may match by name but not by what is inside.
Why the first results can be incomplete
Indexing happens in the background and takes time. After a macOS update, after connecting a new drive, or after a rebuild of the index, Spotlight may still be working through your files. During that time you may see missing or partial results. This is normal and usually fixes itself. Indexing can also make the Mac's fans spin or the battery drain faster for a while, because the process uses CPU and disk.
Checking the index from Terminal
You can look at Spotlight's state from Terminal. The command below reports whether indexing is enabled on the startup volume.
mdutil -s /A healthy result says that indexing is enabled. If it says indexing is disabled, Spotlight cannot find new files on that volume.
You can also see the metadata Spotlight stores for a single file. Replace the path with a file you have.
mdls ~/Documents/report.pdfThe output is a list of attributes such as kMDItemDisplayName, kMDItemContentType, and kMDItemContentModificationDate. These are the same attributes that Spotlight queries and Terminal searches use. Later chapters show how to query them directly.
Privacy and where processing happens
Local search runs on your Mac, using the local index. The index is stored on your disk, not sent elsewhere. Web and other suggestions are a separate feature. If you do not want them, you can turn them off in the Spotlight settings, which a later chapter covers. Excluding a folder from Spotlight also keeps its contents out of the index.
Common misunderstandings
- "Spotlight searches live." It searches the index, not the disk. A file that was just created may take a moment to appear.
- "If a file exists, Spotlight will find it." Excluded folders, unreadable formats, and an unfinished or damaged index all cause misses.
- "Spotlight and Finder search are different engines." Finder search uses the same index. The difference is the interface and the options around it, which a later chapter explains.
- "Rebuilding the index is a routine fix." It is a last resort. It costs time and CPU, and it is needed only when results stay wrong after indexing has had time to finish.
Summary of the working model
- Spotlight searches a prebuilt index, not the files themselves.
- The index holds both contents and metadata, so you can search by what a file says and by facts about it.
- macOS updates the index in the background after changes, so there can be a short delay.
- Some places and file types are not indexed, and you can add exclusions yourself.
- Finder search, the Spotlight window, and Terminal tools all read the same index.
Exercise
You save a new PDF and search for a phrase from inside it, but it does not appear right away. Give two possible reasons that follow from how Spotlight works, and one command that shows whether indexing is enabled.
Show answer
First, Spotlight searches the index, and the index updates in the background, so the new file may not be indexed yet. Second, the file may be in a folder excluded from Spotlight, or its format may not be readable for content, so only the name would match. The command is mdutil -s /, which reports the indexing status of the startup volume.
Exercise
What is metadata, and how can you see the metadata Spotlight has stored for one file from Terminal?
Show answer
Metadata is information about a file, such as its name, kind, dates, and author, as opposed to its contents. Run mdls followed by the file's path, for example mdls ~/Documents/report.pdf, to list the stored attributes.