Skip to content

Commit d389628

Browse files
authoredDec 3, 2024··
[CI-3982] Add filterMatchTypes to search (#358)
1 parent 80c09a1 commit d389628

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed
 

‎spec/src/modules/search.js

+22
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,28 @@ describe(`ConstructorIO - Search${bundledDescriptionSuffix}`, () => {
531531
});
532532
});
533533

534+
it('Should return a response with a valid filterName, filterValue and additional filters and filterMatchTypes', (done) => {
535+
const filters = { keywords: ['battery-powered'] };
536+
const filterMatchTypes = { keywords: 'any' };
537+
const { search } = new ConstructorIO({
538+
apiKey: testApiKey,
539+
fetch: fetchSpy,
540+
});
541+
542+
search.getSearchResults('Jacket', { filters, filterMatchTypes }, {}).then((res) => {
543+
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);
544+
expect(res).to.have.property('request').to.be.an('object');
545+
expect(res).to.have.property('response').to.be.an('object');
546+
expect(res).to.have.property('result_id').to.be.an('string');
547+
expect(res.request.filters).to.deep.equal(filters);
548+
expect(requestedUrlParams).to.have.property('filters');
549+
expect(requestedUrlParams.filters).to.have.property('keywords').to.equal(Object.values(filters)[0][0]);
550+
expect(requestedUrlParams).to.have.property('filter_match_types');
551+
expect(requestedUrlParams.filter_match_types).to.have.property('keywords').to.equal(filterMatchTypes.keywords);
552+
done();
553+
});
554+
});
555+
534556
it('Should emit an event with response data', (done) => {
535557
const { search } = new ConstructorIO({
536558
apiKey: testApiKey,

‎src/modules/search.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function createSearchUrl(query, parameters, options, isVoiceSearch = false) {
4545
}
4646

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

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

70+
if (filterMatchTypes) {
71+
queryParams.filter_match_types = filterMatchTypes;
72+
}
73+
7074
// Pull sort by from parameters
7175
if (sortBy) {
7276
queryParams.sort_by = sortBy;
@@ -164,6 +168,7 @@ class Search {
164168
* @param {string[]} [parameters.hiddenFacets] - Hidden facets to return
165169
* @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.com/reference/shared-variations-mapping for details
166170
* @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
171+
* @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
167172
* @param {object} [networkParameters] - Parameters relevant to the network request
168173
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
169174
* @returns {Promise}
@@ -174,6 +179,9 @@ class Search {
174179
* filters: {
175180
* size: 'medium'
176181
* },
182+
* filterMatchTypes: {
183+
* size: 'all'
184+
* }
177185
* });
178186
*/
179187
getSearchResults(query, parameters, networkParameters = {}) {

‎src/types/search.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface SearchParameters {
3030
hiddenFacets?: string[];
3131
variationsMap?: VariationsMap;
3232
qsParam?: Record<string, any>;
33+
filterMatchTypes: Record<string, 'all'| 'any' | 'none'>
3334
}
3435
declare class Search {
3536
constructor(options: ConstructorClientOptions);

0 commit comments

Comments
 (0)
Please sign in to comment.