objectbox-dart icon indicating copy to clipboard operation
objectbox-dart copied to clipboard

Filter DB Entries based on list content

Open CaptainDario opened this issue 3 years ago • 6 comments

Describe the solution you'd like

I am building a dictionary application and there I have a class Entry that has a List<String> of meanings. If a user now searches query in the dictionary, I want to get every word that contains the query as a substring.

Example

/* Imagine those two entries in a objectbox DB*/
Entry dictEntry1 = Entry()
dictEntry1.meanings = ["test"];

Entry dictEntry2 = Entry()
dictEntry2.meanings = ["book"];

// This is the search term the user entered
String query = "tes";

Now I would like a way to query all Entrys in the DB that have "tes" as a substring of any of their meanings.

CaptainDario avatar Oct 06 '22 13:10 CaptainDario

Thanks for the suggestion!

For now you will have to write your own filter code for this or remodel your data to use existing query conditions.

Side note: the current contains condition will only match if an element is equal to the search term. E.g. "there" will match ["hello", "there"], but "the" will not.

greenrobot-team avatar Oct 10 '22 05:10 greenrobot-team

@greenrobot-team How can I write my own Filter Code? Here is state filters are only for java/kotlin.

CaptainDario avatar Oct 10 '22 07:10 CaptainDario

@CaptainDario Dart has built-in helpers to filter iterables, e.g. where().

greenrobot-team avatar Oct 11 '22 07:10 greenrobot-team

@greenrobot-team how likely is that this will be implemented any time soon? Is it very difficult to do so? Because just using .getAll() and then .where() on the result is very slow.

CaptainDario avatar Oct 14 '22 18:10 CaptainDario

No plans right now.

Maybe create a separate box with meanings that link back to an Entry, then your code can query that using a contains condition? Something like:

class Meaning {
  @Index()
  String meaning;

  @Backlink('meanings')
  final entries = ToMany<Entry>();
}

class Entry {
  final meanings = ToMany<Meaning>();
}

greenrobot-team avatar Oct 17 '22 06:10 greenrobot-team

Thank you for your help! Gonna give this a try.

CaptainDario avatar Oct 17 '22 08:10 CaptainDario