With Spotlight it is easy to search for content on the whole computer. I just wanted to restrict it to my notes database.
NSMetadataQueryknows the method
setSearchScopes:for this. I set the scope to the directory just before starting the query, as I set the search predicate. Unfortunately I always ended up with at most one result, even when I knew that there would be more.
The solution to this was to directly call
setSearchScopes:after initializing the query:
_query = [[NSMetadataQuery alloc] init];
// Restrict the search to our working directory
NSString *path = [@"~/Library/QNotes" stringByExpandingTildeInPath];
[_query setSearchScopes:[NSArray arrayWithObjects:path,nil]];
Now I am correctly seeing all results.
Btw.: Ars technica as a nice article about spotlight, even if it ends up in Spotlight gui bashing at the end. But the first half is a good read.
Apple also has a good overview article.