Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI-3982] Add filterMatchTypes to search #358

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions spec/src/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,28 @@ describe(`ConstructorIO - Search${bundledDescriptionSuffix}`, () => {
});
});

it('Should return a response with a valid filterName, filterValue and additional filters and filterMatchTypes', (done) => {
const filters = { keywords: ['battery-powered'] };
const filterMatchTypes = { keywords: 'any' };
const { search } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
});

search.getSearchResults('Jacket', { filters, filterMatchTypes }, {}).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);
expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res.request.filters).to.deep.equal(filters);
expect(requestedUrlParams).to.have.property('filters');
expect(requestedUrlParams.filters).to.have.property('keywords').to.equal(Object.values(filters)[0][0]);
expect(requestedUrlParams).to.have.property('filter_match_types');
expect(requestedUrlParams.filter_match_types).to.have.property('keywords').to.equal(filterMatchTypes.keywords);
done();
});
});

it('Should emit an event with response data', (done) => {
const { search } = new ConstructorIO({
apiKey: testApiKey,
Expand Down
10 changes: 9 additions & 1 deletion src/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function createSearchUrl(query, parameters, options, isVoiceSearch = false) {
}

if (parameters) {
const { offset, page, resultsPerPage, filters, sortBy, sortOrder, section, fmtOptions, hiddenFields, hiddenFacets, variationsMap, qsParam, preFilterExpression } = parameters;
const { offset, page, resultsPerPage, filters, sortBy, sortOrder, section, fmtOptions, hiddenFields, hiddenFacets, variationsMap, qsParam, preFilterExpression, filterMatchTypes } = parameters;

// Pull offset from parameters
if (!helpers.isNil(offset)) {
Expand All @@ -67,6 +67,10 @@ function createSearchUrl(query, parameters, options, isVoiceSearch = false) {
queryParams.filters = filters;
}

if (filterMatchTypes) {
queryParams.filter_match_types = filterMatchTypes;
}

// Pull sort by from parameters
if (sortBy) {
queryParams.sort_by = sortBy;
Expand Down Expand Up @@ -164,6 +168,7 @@ class Search {
* @param {string[]} [parameters.hiddenFacets] - Hidden facets to return
* @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.com/reference/shared-variations-mapping for details
* @param {object} [parameters.qsParam] - Parameters listed above can be serialized into a JSON object and parsed through this parameter. Please refer to https://docs.constructor.com/reference/v1-search-get-search-results
* @param {object} [parameters.filterMatchTypes] - An object specifying whether results must match `all`, `any` or `none` of a given filter. Please refer to https://docs.constructor.com/reference/v1-search-get-search-results
* @param {object} [networkParameters] - Parameters relevant to the network request
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
* @returns {Promise}
Expand All @@ -174,6 +179,9 @@ class Search {
* filters: {
* size: 'medium'
* },
* filterMatchTypes: {
* size: 'all'
* }
* });
*/
getSearchResults(query, parameters, networkParameters = {}) {
Expand Down
1 change: 1 addition & 0 deletions src/types/search.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface SearchParameters {
hiddenFacets?: string[];
variationsMap?: VariationsMap;
qsParam?: Record<string, any>;
filterMatchTypes: Record<string, 'all'| 'any' | 'none'>
}
declare class Search {
constructor(options: ConstructorClientOptions);
Expand Down