Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
feat(facets): when * is present, only send that parameter (#874)
Browse files Browse the repository at this point in the history
* feat(facets): add expandWildcardFacets parameter to process * in facets

When * is present in facets, we don't need to request the individual facet values of each facet that's declared, as * will already request it.

With a new parameter "expandWildcardFacets", we filter out all facets that aren't * itself or -something. The new parameter doesn't add "*" automatically, it still

To decide:
- is "-something" worth caring about?
- when * isn't present, should no facets be requested or the default ones?
- is this the right location, maybe the client cache should have similar logic instead?

see: https://github.com/algolia/algoliasearch-helper-js/issues/391

* remove reference to non-existing -

* don't inject *

* rephrase

* remove the parameter, by default!

* Update src/requestBuilder.js

* fix

* Update test/spec/requestBuilder.js

Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>

Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
  • Loading branch information
Haroenv and francoischalifour authored Oct 8, 2021
1 parent 3648463 commit fc183ec
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,16 @@ declare namespace algoliasearchHelper {
export class SearchParameters implements PlainSearchParameters {
managedParameters: [
'index',

'facets',
'disjunctiveFacets',
'facetsRefinements',
'hierarchicalFacets',
'facetsExcludes',

'disjunctiveFacetsRefinements',
'numericRefinements',
'tagRefinements',
'hierarchicalFacets',
'hierarchicalFacetsRefinements'
];

Expand Down
14 changes: 11 additions & 3 deletions src/SearchParameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1312,9 +1312,17 @@ SearchParameters.prototype = {

managedParameters: [
'index',
'facets', 'disjunctiveFacets', 'facetsRefinements',
'facetsExcludes', 'disjunctiveFacetsRefinements',
'numericRefinements', 'tagRefinements', 'hierarchicalFacets', 'hierarchicalFacetsRefinements'

'facets',
'disjunctiveFacets',
'facetsRefinements',
'hierarchicalFacets',
'facetsExcludes',

'disjunctiveFacetsRefinements',
'numericRefinements',
'tagRefinements',
'hierarchicalFacetsRefinements'
],
getQueryParams: function getQueryParams() {
var managedParameters = this.managedParameters;
Expand Down
2 changes: 1 addition & 1 deletion src/requestBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var requestBuilder = {
var numericFilters = requestBuilder._getNumericFilters(state);
var tagFilters = requestBuilder._getTagFilters(state);
var additionalParams = {
facets: facets,
facets: facets.indexOf('*') > -1 ? ['*'] : facets,
tagFilters: tagFilters
};

Expand Down
61 changes: 61 additions & 0 deletions test/spec/requestBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,64 @@ test('does only a single query if refinements are empty', function() {
var queries = getQueries(searchParams.index, searchParams);
expect(queries).toHaveLength(1);
});

describe('wildcard facets', function() {
test('keeps as-is if no * present', function() {
var searchParams = new SearchParameters({
facets: ['test'],
disjunctiveFacets: ['test_disjunctive', 'test_numeric'],
hierarchicalFacets: [{name: 'test_hierarchical', attributes: ['whatever']}]
});

var queries = getQueries(searchParams.index, searchParams);

expect(queries.length).toBe(1);
expect(queries[0].params.facets).toEqual([
'test',
'test_disjunctive',
'test_numeric',
'whatever'
]);
});

test('keeps only *', function() {
var searchParams = new SearchParameters({
facets: ['test', '*'],
disjunctiveFacets: ['test_disjunctive', 'test_numeric'],
hierarchicalFacets: [{name: 'test_hierarchical', attributes: ['whatever']}]
});

var queries = getQueries(searchParams.index, searchParams);

expect(queries.length).toBe(1);
expect(queries[0].params.facets).toEqual(['*']);
});

test('keeps only * when first value', function() {
var searchParams = new SearchParameters({
facets: ['*', 'test'],
disjunctiveFacets: ['test_disjunctive', 'test_numeric'],
hierarchicalFacets: [{name: 'test_hierarchical', attributes: ['whatever']}]
});

var queries = getQueries(searchParams.index, searchParams);

expect(queries.length).toBe(1);
expect(queries[0].params.facets).toEqual(['*']);
});

test('only applies to first query', function() {
var searchParams = new SearchParameters({
facets: ['test', '*'],
disjunctiveFacets: ['test_disjunctive', 'test_numeric'],
hierarchicalFacets: [{name: 'test_hierarchical', attributes: ['whatever']}],
disjunctiveFacetsRefinements: {test_disjunctive: ['one', 'two']}
});

var queries = getQueries(searchParams.index, searchParams);

expect(queries.length).toBe(2);
expect(queries[0].params.facets).toEqual(['*']);
expect(queries[1].params.facets).toEqual('test_disjunctive');
});
});

0 comments on commit fc183ec

Please sign in to comment.