Skip to content

Commit 9fa4780

Browse files
committed
override filters even when instantsearch filters are empty
1 parent 78769d4 commit 9fa4780

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

packages/instant-meilisearch/src/adapter/search-request-adapter/__tests__/search-params.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,39 @@ describe('Parameters adapter', () => {
240240

241241
expect(searchParams.distinct).toBe(indexDistinctSearchConfig)
242242
})
243+
244+
test('filter can be set via global override when IS filters are empty', () => {
245+
const globalFilter = 'category = "books"'
246+
247+
const searchParams = adaptSearchParams({
248+
...DEFAULT_CONTEXT,
249+
// No facetFilters, filters, or numericFilters - empty IS filters
250+
meiliSearchParams: {
251+
filter: globalFilter,
252+
},
253+
})
254+
255+
expect(searchParams.filter).toBe(globalFilter)
256+
})
257+
258+
test('filter can be set via per-index override when IS filters are empty', () => {
259+
const globalFilter = 'category = "books"'
260+
const indexFilter = 'status = "active"'
261+
262+
const searchParams = adaptSearchParams({
263+
...DEFAULT_CONTEXT,
264+
// No facetFilters, filters, or numericFilters - empty IS filters
265+
meiliSearchParams: {
266+
filter: globalFilter,
267+
indexesOverrides: {
268+
test: { filter: indexFilter },
269+
},
270+
},
271+
})
272+
273+
// Per-index override should take precedence over global override
274+
expect(searchParams.filter).toBe(indexFilter)
275+
})
243276
})
244277

245278
describe('Geo filter adapter', () => {

packages/instant-meilisearch/src/adapter/search-request-adapter/search-params-adapter.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,14 @@ export function MeiliParamsCreator(searchContext: SearchContext) {
133133
}
134134
},
135135
addFilters() {
136-
if (meilisearchFilters.length) {
137-
meiliSearchParams.filter =
138-
overrideParams?.indexesOverrides?.[indexUid]?.filter ??
139-
overrideParams?.filter ??
140-
meilisearchFilters
136+
const overrideFilter =
137+
overrideParams?.indexesOverrides?.[indexUid]?.filter ??
138+
overrideParams?.filter
139+
140+
if (overrideFilter !== undefined) {
141+
meiliSearchParams.filter = overrideFilter
142+
} else if (meilisearchFilters.length) {
143+
meiliSearchParams.filter = meilisearchFilters
141144
}
142145
},
143146
addAttributesToRetrieve() {

0 commit comments

Comments
 (0)