algolia / algoliasearch-helper-flutter

⚡️ Building block to create instant-search applications with Flutter
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/flutter/
Other
21 stars 14 forks source link

Simple Filtering Question #110

Closed andrewkimjoseph closed 7 months ago

andrewkimjoseph commented 8 months ago

I have a List<String> in my Dart code named connectionsUids, and I want HitsSearcher to return only the results whose objectID is in connectionsUids.

How do I go about it?

andrewkimjoseph commented 7 months ago

I figured how to do this.

Create a FilterGroupID object based on the particular field you want to filter and set the operator to FilterOperator.or:

final connectionId = const FilterGroupID("uid", FilterOperator.or)

Then, create a FilterState object:

final _filterState = FilterState()

Then, create a function that adds the filters and connects the FilterState object to the HitsSearcher object:

void addFiltersAndConnectFilterState() {
   if (connectionUids?.isEmpty == true) {
      _filterState.add(const FilterGroupID("none"), [Filter.facet("none", "none")]);
   } else {
      for (String uid in connectionUids!) {
         _filterState.add(connectionId, [Filter.facet('uid', uid)]);
      }
    }
     _connectionsSearcher.connectFilterState(_filterState);
}

You can call this method in initState(){};

Reply to this comment if you need any help.