I have a collection of Bars, each of which have isar links to Foos:
@collection
class Bar{
Id? id;
final foos = IsarLinks<Foo>();
... // Bar stuff
};
Each Foo object has a field named text, which is a String:
@collection
class Foo{
Id? id;
late String text;
... // Foo stuff
};
Now, what I would like to do is search and find all the Foos linked to a particular Bar that contain all of the given key words in their text (preferably case insensitive). So, searching with the key words "canada" and "street" should match a Foo with text = "I don't know what street, Canada is on. - Al Capone
Isar has a guide on how to do a full text search. In it, they recommend adding an index of the words of the text searched on, like so:
@collection
class Foo{
Id? id;
late String text;
@Index(type: IndexType.value, caseSensitive: false) // for more flexibility; see https://isar.dev/recipes/full_text_search.html#i-want-more-control
List<String> get words => Isar.splitWords(text); // see https://isar.dev/recipes/full_text_search.html#splitting-text-the-right-way
};
However, there does not seem to be an easy way to filer based on an index with IsarLinks.
My questions:
- Is there a way to filter based on an index through all the items of an
IsarLinksobject? - If not, what would be the best way to perform a text search like I described? As a bonus question, why can't I do this with
IsarLinks?
Note: I know I can simply use IsarLinks.filter and build a query that selects all Foos with text containing all the key words, but I am wondering if there is a way to do a text search in the way Isar (with indexes) recommends on IsarLinks.
Isar does not provide a direct way to filter based on an index through all the items of an
IsarLinksobject. The indexing functionality is primarily designed for individual properties of an object rather than for complex relationships likeIsarLinks.To perform a text search across the linked
Fooobjects based on thetextfield, you can follow these steps:Fooobjects from theIsarLinksof a specificBar.Fooobjects based on the search keywords using thewheremethod and a custom condition.textfield.Here's an example implementation of the text search functionality:
In this example, the
searchTextInFoosfunction takes aBarobject and a list of searchkeywordsas input. It filters theFooobjects linked to thatBarbased on the keywords using thewheremethod.The
wheremethod applies a custom condition to eachFooobject, checking if all keywords are contained within thetextfield. The condition uses a case-insensitive comparison by converting both thetextandkeywordto lowercase.Finally, the search results are stored in the
searchResultslist, which you can utilize as needed.While it would be convenient to perform a full-text search directly on an
IsarLinksobject with the help of indexing, the current version of Isar does not provide this capability. However, the approach outlined above allows you to achieve the desired text search functionality for the linkedFooobjects.