Advanced Spotlight use
From Applepedia
Contents |
How do I do more with this infernal contraption?
So you've installed Tiger. Your onebutan's finally finished indexing your hard drive, and you're merrily searching away. You think to yourself, "Gee, wouldn't it be nice if I could use some boolean options here?" You open your little Spotlight window up, type in Macintosh OR Apple, and...get obviously wrong results back. What's going on here?
The answer, dear friends, is simple. Spotlight's GUI interface does in fact understand a limited boolean subset, but you have to know how to access it, and Apple doesn't seem to want to publicize it too much for fear that the mouth-breather user base would jump ship to Windows if they dared to mention the word "boolean".
Booleans
The Spotlight syntax for accessing booleans is relatively simple, if not entirely standard, or at least what you'd expect it to respond to if you expect it to react to Google-style AND/OR/NOT keywords. Spotlight understands spaces as a normal Boolean AND, which we all know. It also theoretically (see below) understands - as NOT, prefixed immediately to a word. Furthermore, it understands | (pipe) as OR, replacing a space. Don't put spaces around it! Spotlight won't understand it then. Most importantly, Spotlight does understand grouping. Use square brackets ( [ and ] ), again without spaces.
That being said, in an ideal world, Spotlight would actually work with all of these. In reality, it's not quite so clean. Searching for OpenLDAP Missig returns 5 conversations I have had with a friend by the last name of Missig on LDAP. You would expect OpenLDAP -Missig to return everything about OpenLDAP that didn't have the word Missig associated with it, but instead it returns 4 IM conversations, a subset of the previous query. What's going on here? I don't know. Grouping is similarly somewhat funky. The query [OpenLDAP Samba]|Directory should return the results of the queries OpenLDAP Samba and Directory concatenated together, but that's definitely not what it's doing. If you can figure out Spotlight's weird-ass boolean "logic", please update this page, because I personally think something is broken in Spotlight's little head.
Wait!
Don't run away yet! There are features of Spotlight that do work! The kind: and date: modifiers can be used to narrow down a search. kind: understands most Spotlight naming types like "PDF", "Image", "Document", and so forth. date: understands natural language dates like "this year" or "2003". You can't use both kind: and date: in a query, though, and make sure you put whichever one you're using at the end, because otherwise you'll get hit by Weird Results Syndrome.
Party in the Terminal
So yeah. Spotlight's GUI interface is kind of neutered. If you want more precise searching, you'll have to turn to the Terminal and use mdfind, or (ugh) use the Finder's "Other..." search. I prefer mdfind, because it doesn't mysteriously beachball for 30 seconds for no reason and then relaunch right in the middle of a major file move operation. Using mdfind, you can conduct both generic and precise queries based on metadata attributes:
- mdfind "what" will return everything that has "what" in it. (Big query.)
- mdfind "kMDItemPixelWidth=2560" will return every image (or any other file whose importer gives it a kMDItemPixelWidth attribute) that is 2560 pixels wide. <, <=, >, and >= also work, so you can say "find me every image equal to or wider than 2560 pixels, like those pictures I took of my enormously fat Aunt Margo at the last family reunion when she was eating an entire bucket of fried chicken from the Food Lion all by herself". (mdfind won't understand the last part, unfortunately.)
- mdfind "kMDItemKind="*Image*" && kMDItemPixelWidth="2560"" further refines the above query to return only specifically images, since QuickTime movies and the like also have kMDItemPixelWidth attributes. (If you have a computer that can play back a 2560x1600 QuickTime movie well, please give it to me.)
For the record, mdfind also understands || to mean OR and -- to mean NOT. (Why couldn't GUI Spotlight?)
"Whoa, whoa, whoa!", you say. How do I know what attributes I can search for? Easy! Perform mdls filename on any file and it will blast back all the metadata information it has on that file, giving you the attribute names you need. They all begin with kMDItem and then have some short string after them describing what they do, which is usually pretty self-evident.
mdfind's cohorts
The other three command-line Spotlight tools are mdcheckschema, mdimport, and mdutil. mdcheckschema is pretty specialized. mdimport is more useful, because you can use it to manually import directories or force reimports of everything claimed by a certain plugin. You can't import directories that are outside Spotlight's enforced boundaries, though, or expect it to pick up files that are missing the necessary triggers for import, so no, you can't index the developer documentation in Spotlight. Hurrrr. Read the man page for information on all the wonderful things mdimport can do. mdutil controls the general Spotlight system, and is mostly useful for the -E option, which lets you blow away the entire Spotlight store on any given volume, forcing it to reindex completely. Again, the man page has all the dirty details.
Common Command Queries
- "mdutil -E /" -- resets Spotlight database (-E is for 'erase' and replace "/" with the unix path you want to reimport) and reimports if appropriate.

