How to search successfully in Spotlight: GUI tools
The most common problem reported in Spotlight search is failure to find target file(s). In this series of articles, I’ll examine what can go wrong, and how you can make local searches more successful using features in macOS. I’m well aware that there are other utilities for searching, some relying on Spotlight, others working independently, but here I’ll confine myself as much as possible to what’s provided in macOS.
Successful searches using Spotlight have four essential requirements:
- The target file(s) must have had their metadata added to Spotlight’s volume indexes.
- Those metadata must be accessible to the tool used to perform the search.
- The search queries used must be capable of finding the target(s).
- Spotlight’s search must work correctly.
Check indexes and access
The first of those can be checked using the mdimport
command tool in Terminal, using the commandmdimport -t -d3 [filepath]
where [filepath] is the path of the file. You can vary the digit used in the -d3
option from 1-3, with larger numbers delivering more detail. For -d3
you’ll first be given the file type and mdimporter used, following which all the data extracted is given according to its Spotlight attributes:Imported '/Users/hoakley/Documents/SpotTestA.rtf' of type 'public.rtf' with plugIn /System/Library/Spotlight/RichText.mdimporter.
37 attributes returned
followed by a long list.
If the file hasn’t been indexed, this article works through the steps you can take to rectify that. Note that recent experience is that using mdutil -E /
to erase and force rebuild of indexes on the Data volume may not work as expected, and you should either perform this in System Settings, or using the command mdutil -E /System/Volumes/Data
Which tool?
Global Spotlight is accessed through the magnifying glass icon in the right of the menu bar, or using the default key combination of Command-space. This includes website content, and isn’t ideal when you’re searching for local files. If you want to use this as an easy gateway to local search, enter the text you want to search for and scroll down to use the command Search in Finder, which opens a Finder Find window for the results of that search query. Alternatively, you can click on Show More in the Documents section of the search results.
Local Spotlight can also be opened by the Find command at the foot of the Finder’s File menu, and takes you straight to its search box, at the right of the toolbar in that Finder window.
This window offers a choice of two search scopes at the upper left. The first covers all the accessible contents of that Mac, and the second is the folder open in that window when it was converted to a Find window. To set the scope for a local Spotlight search, start from a normal Finder window with the target folder open, then use the Find command on that.
Searching
Typing text into the search box at the right of the toolbar then performs live or incremental search for both filenames and content at the same time, or you can select one of them in the menu.
Text entered into the search box can simply be the start of a word in the target, or can be a basic search query such as name:"target"*cdw
. I will explain those in a later article about search queries.
Instead of, or in addition to, entering text in the search box, you can set further search criteria in search bars below that.
In this case, the file Name is required to contain the string entered in the text box. Add more of these search bars for additional criteria to narrow your search.
Search attributes
In search bars, the first popup sets the metadata attribute to use in the query. For example, both Name and Filename refer to the name of the file, although Name is given in its localised form. Many more options are available by selecting the Other… item at the foot of the attribute menu. You can either set those as a one-off, or add them to the menu of attributes if you’re likely to use them again. These roughly correspond to the metadata attributes as in formal Spotlight search queries used elsewhere, although their names are different.
The second popup sets the operator to be used. While they may appear self-explanatory, two merit fuller explanation as they may work differently from how you might expect:
- matches finds the start of words found by breaking the whole string at word boundaries (see below). Thus it will find danger and danger-es in one-danger-est, but won’t find anger there.
- contains is applied without reference to word boundaries, and simply finds the given string, regardless of what characters occur before or after that. Thus it will find anger in dangerous. It has one unusual behaviour, though: it ignores hyphens, so dangeres will be found in the string danger-est.
Word boundaries
These are crucial in search queries run from the search box in a Find window, and the matches operator used in a search bar below. Although the search box claims to use the contains operator, it actually behaves as the matches operator does in a search bar.
In many languages word roots and meaning appear at the start of words, with declensions and conjugations at the end. If you want to find words related to harvest, like harvester, harvesting and harvested, then you’re going to enter a search query using harvest rather than vest. Like other search engines designed for live or incremental search, Spotlight is fastest when searching for the start of words. It therefore divides compound words often used for filenames into component words. It does so using rules for word boundaries laid down in the International Components for Unicode.
In practice, word boundaries include a space, the underscore _, hyphen – and changes of case used in CamelCase. Spotlight treats each of the following examples as three words:one target two
one_target_two
one-target-two
OneTargetTwo
Languages other than English may allow other word boundaries, but those are the most common.
The rules recognise that hyphens are difficult, and Spotlight makes them even trickier as it can ignore them altogether when searching for an arbitrary string without word boundaries, and will then happily find netargett in one-target-two! Spotlight also struggles with multiple hyphens mixed with underscores. For example, it may not be able to find danger in the file name a_a-b-c-e-danger_z.txt when using matches, but should work as expected when using contains instead.
Other tools in macOS
In-app search or Core Spotlight relies on search features provided by the app, for example that in Mail. Although these use Spotlight’s indexes and its query language, their function is different from Global or Local Spotlight, and implemented through a distinct Core Spotlight API.
The primary command tool for performing Spotlight search is mdfind
, which uses formal query strings and predicates. I’ll tackle those in a future article.
Recommendations for successful search
- To perform local search of files indexed by Spotlight, use a Find window in the Finder.
- Limit search scope by folder when possible, by opening the folder in a Finder window then converting it into a Find window using the menu command.
- For the quickest results, type the start of a distinctive word in the file name or content into the search box.
- For the most inclusive results, leave the search box empty and construct a series of search bars to limit the list of hits.
- Limit the type of files when possible, using the Kind metadata attribute in the first popup menu.
- For file names with a limited number of hyphens and underscores as word boundaries, use Name matches with the start of a word included in the localised file name.
- For file names with more word boundaries, use Name contains with a distinctive target string.
I’m very grateful to Aldous and to Thomas Tempelmann for their unstinting help in understanding word boundaries and their importance, and for solving the mystery of Cleta.