I came up with this code earlier:
NSArray *searchTerms = [cleanedQuery componentsSeparatedByString:@" "];
NSPredicate *basePredicate = [NSPredicate
predicateWithFormat:@"SUBQUERY(keywords, $keyword, $keyword.name BEGINSWITH
$QUERY).@count != 0"];
NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *searchTerm in searchTerms) {
NSDictionary *sub = [NSDictionary
dictionaryWithObjectsAndKeys:searchTerm, @"QUERY", nil];
NSPredicate *termPredicate = [basePredicate
predicateWithSubstitutionVariables:sub];
[subpredicates addObject:termPredicate];
}
NSPredicate *combinedPredicate = [NSCompoundPredicate
andPredicateWithSubpredicates:subpredicates];
This doesn't use "ANY" as you mentioned (not sure exactly how I would use that)
but it works. However, I'm sure there's a better way to simplify this instead
of using a giant compound AND predicate to match each one of the search terms.
Is there any way to simplify this?
Thanks
On 2011-07-07, at 11:23 AM, The Karl Adam wrote:
> You want to be using SUBQUERY() to match ANY keywords. Check the docs
> for the predicate reference details for this.
>
> _Karl
>
> On Wed, Jul 6, 2011 at 8:23 PM, Indragie Karunaratne
> <[email protected]> wrote:
>> Hi all,
>>
>> I have a Core Data object model that I'm trying to write a fetch predicate
>> for (to use for search). Quick explanation of the model:
>>
>> We'll call the main entity "Book". There's also a "Keyword" entity. The
>> Book entity has a to-many relationship with the Keyword entity called
>> "keywords". In turn, the Keyword entity has an inverse relationship with the
>> Book entity called "book". The Keyword entity has a single attribute called
>> "name". So basically, each Book has Keywords that describe it.
>>
>> For my search, I have an array of search terms. I need a predicate that I
>> can use on fetch requests for the Book entity that will evaluate to TRUE if
>> ALL of the search terms have a corresponding Keyword in that the "name"
>> property begins with the search term.
>>
>> For example:
>>
>> There are three books:
>> Book1 - keywords: {"fiction", "scifi"}
>> Book2 - keywords: {"nonfiction"}
>>
>> If the search terms were {"fic", "nonfic", "sci"} the resulting fetched
>> array would contain NOTHING because none of the books have keywords that
>> begin with all 3 of those search terms..
>>
>> However, if the search terms were {"fic", "sci"}, the resulting fetched
>> array would contain Book1 since its keywords "fiction" and "scifi" begin
>> with the two search terms "fic" and "sci". The key part here is that ALL of
>> the search terms have to have a corresponding keyword as demonstrated above
>> for the predicate to evaluate to true.
>>
>> I hope I've explained this problem well enough, it's hard to put this stuff
>> into words ;-)
>>
>> Any help is appreciated,
>> Indragie_______________________________________________
>>
>> Cocoa-dev mailing list ([email protected])
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>> http://lists.apple.com/mailman/options/cocoa-dev/karl.adam%40gmail.com
>>
>> This email sent to [email protected]
>>
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]