Fetching entities whose many-to-many relationship contains all terms

77 Views Asked by At

I have a model with a Verse entity that has a many-to-many relationship to a Word entity.

I'd like to find verses that contain all words that a user is searching for.

I'm guessing that it could be done with a predicate like

    "ANY words.word == %@ AND ANY words.word == %@ AND ...", term1, term2, ...

Can this use some type of word->Verse index, to avoid having to compare every verse's words against term1?

If not, how should the (model or) predicate be changed to make this fetch more efficient?

2

There are 2 best solutions below

2
On BEST ANSWER

First, you can just do a contains in one predicate:

[NSPredicate predicateWithFormat:"ANY words.word CONTAINS %@", arrayOfWords];

But, that will be slow. String compares are slow. Case and diacritic insensitive are almost the slowest. You are better off having another property on your Word entity that is for searching where you strip out the case and the diacritic and then search against that field. Your search performance will be much higher.

0
On

I would create a NSPredicate for each term, and then use a andPredicateWithSubpredicates: NSCompoundPredicate to perform the search. If it is a live search, reload the search results on a space, or reload on every character and use one predicate with CONTAINS.