@@ -17,10 +17,16 @@ final class QueryBuilder {
1717 /// Number of search result queries
1818 int get _resultQueriesCount => 1 ;
1919
20- /// Number of generated disjunctive queries for given hierarchical
21- /// filters list
22- int get _disjunctiveQueriesCount =>
23- _searchState.disjunctiveFacets? .length ?? 0 ;
20+ /// Number of generated disjunctive queries for given filters list
21+ int get _disjunctiveQueriesCount => _getRefinedDisjunctiveFacets ().length;
22+
23+ /// Get refined disjunctive facets from the search state's filter groups.
24+ Iterable <String > _getRefinedDisjunctiveFacets () {
25+ final currentFilterGroups = _searchState.filterGroups ?? {};
26+ return (_searchState.disjunctiveFacets ?? {}).where ((facetName) =>
27+ currentFilterGroups.any (
28+ (group) => group.any ((facet) => facet.attribute == facetName)));
29+ }
2430
2531 /// Number of generated hierarchical queries for given hierarchical
2632 /// filters list
@@ -92,35 +98,35 @@ final class QueryBuilder {
9298
9399 /// Build additional queries to fetch correct facets count values
94100 /// for disjunctive facets
95- Iterable <SearchState > _buildDisjunctiveFacetingQueries (SearchState query) =>
96- query.disjunctiveFacets? .map ((facet) {
97- final filterGroupsCopy = < FilterGroup > {};
98- final currentFilterGroups = _searchState.filterGroups ?? {};
99- for (final filterGroup in currentFilterGroups) {
100- final Set <Filter > updatedFilters;
101- if (filterGroup.groupID.operator == FilterOperator .or) {
102- updatedFilters = filterGroup
103- .where (
104- (element) =>
105- ! (element is FilterFacet && element.attribute == facet),
106- )
107- .toSet ();
108- } else {
109- updatedFilters = filterGroup;
110- }
111- filterGroupsCopy.add (filterGroup.copyWith (filters: updatedFilters));
101+ Iterable <SearchState > _buildDisjunctiveFacetingQueries (SearchState query) {
102+ final currentFilterGroups = _searchState.filterGroups ?? {};
103+ return _getRefinedDisjunctiveFacets ().map ((facet) {
104+ final filterGroupsCopy = < FilterGroup > {};
105+ for (final filterGroup in currentFilterGroups) {
106+ final Set <Filter > updatedFilters;
107+ if (filterGroup.groupID.operator == FilterOperator .or) {
108+ updatedFilters = filterGroup
109+ .where (
110+ (element) =>
111+ ! (element is FilterFacet && element.attribute == facet),
112+ )
113+ .toSet ();
114+ } else {
115+ updatedFilters = filterGroup;
112116 }
113- return query.copyWith (
114- facets: [facet],
115- filterGroups: filterGroupsCopy,
116- attributesToRetrieve: ['objectID' ],
117- // TODO: should be [], workaround to avoid the client exception
118- attributesToHighlight: ['objectID' ],
119- hitsPerPage: 0 ,
120- analytics: false ,
121- );
122- }) ??
123- [];
117+ filterGroupsCopy.add (filterGroup.copyWith (filters: updatedFilters));
118+ }
119+ return query.copyWith (
120+ facets: [facet],
121+ filterGroups: filterGroupsCopy,
122+ attributesToRetrieve: ['objectID' ],
123+ // TODO: should be [], workaround to avoid the client exception
124+ attributesToHighlight: ['objectID' ],
125+ hitsPerPage: 0 ,
126+ analytics: false ,
127+ );
128+ });
129+ }
124130
125131 /// Create modifiable copy of filter groups.
126132 Set <FilterGroup > _copyFilterGroups () =>
0 commit comments