Skip to content

Commit 9dacb50

Browse files
authored
feat(queryBuilder): remove aux queries for unrefined facets (#152)
1 parent fd91173 commit 9dacb50

File tree

2 files changed

+73
-36
lines changed

2 files changed

+73
-36
lines changed

helper/lib/src/query_builder.dart

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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() =>

helper/test/query_builder_test.dart

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,33 @@ void main() {
1616
const queryBuilder = QueryBuilder(query);
1717
final queries = queryBuilder.build();
1818
final disjunctiveFacetingQueries = queries.skip(1);
19-
expect(disjunctiveFacetingQueries.length, 2);
20-
for (final query in disjunctiveFacetingQueries) {
21-
expect(query.facets?.length, 1);
22-
}
19+
expect(disjunctiveFacetingQueries.length, 0);
20+
});
21+
22+
test('test disjunctive faceting queries with some unrefined facets', () {
23+
final filterGroups = <FilterGroup>{
24+
FacetFilterGroup(FilterGroupID.or('g1'), {
25+
Filter.facet('brand', 'samsung'),
26+
Filter.facet('price', 100),
27+
}),
28+
FacetFilterGroup(FilterGroupID.or('g2'), {
29+
Filter.facet('color', 'red'),
30+
}),
31+
};
32+
final query = SearchState(
33+
indexName: 'index',
34+
query: 'phone',
35+
disjunctiveFacets: {'brand', 'category', 'color', 'price'},
36+
filterGroups: filterGroups,
37+
);
38+
final queryBuilder = QueryBuilder(query);
39+
final queries = queryBuilder.build();
40+
final disjunctiveFacetingQueries = queries.skip(1);
41+
expect(disjunctiveFacetingQueries.length, 3);
42+
43+
final hasCategory = disjunctiveFacetingQueries
44+
.any((query) => (query.facets ?? []).contains('category'));
45+
expect(hasCategory, false);
2346
});
2447

2548
test('test disjunctive faceting queries generation with filters', () {
@@ -236,6 +259,14 @@ void main() {
236259
query: 'phone',
237260
disjunctiveFacets: {'color', 'brand', 'size'},
238261
filterGroups: {
262+
FacetFilterGroup(
263+
FilterGroupID.or('g1'),
264+
{
265+
Filter.facet('color', 'blue'),
266+
Filter.facet('brand', 'samsung'),
267+
Filter.facet('size', 'l'),
268+
},
269+
),
239270
FilterGroup.hierarchical(
240271
name: 'category',
241272
filters: {Filter.facet('category.lvl2', 'a > b > c')},

0 commit comments

Comments
 (0)