Skip to content

Commit cb77bb1

Browse files
authored
Update search parameters for v0.29.0 (#832)
* Add matchingStrategy in readme * Add matching strategy as an option * Simplify context creation
1 parent ba03f46 commit cb77bb1

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const searchClient = instantMeiliSearch(
8383
- [`finitePagination`](#finite-pagination): Used to work with the [`pagination`](#-pagination) widget (default: `false`) .
8484
- [`primaryKey`](#primary-key): Specify the primary key of your documents (default `undefined`).
8585
- [`keepZeroFacets`](#keep-zero-facets): Show the facets value even when they have 0 matches (default `false`).
86+
- [`matchingStrategy`](#matching-strategy): Determine the search strategy on words matching (default `last`).
8687

8788
The options are added as the third parameter of the `instantMeilisearch` function.
8889

@@ -170,6 +171,21 @@ genres:
170171
{ keepZeroFacets : true } // default: false
171172
```
172173

174+
### Matching strategy
175+
176+
`matchingStrategy` gives you the possibility to chose how Meilisearch should handle the presence of multiple query words.
177+
178+
For example, if your query is `Hello world` by default Meilisearch returns documents containing either both `Hello` and `world` or documents that only contain `hello`. This is the `last` strategy, where words are stripped from the right.
179+
The other strategy is `all`, where both `hello` and `worlds` **must** be present in a document for it to be returned.
180+
181+
// TODO: add documentation link
182+
183+
```js
184+
{
185+
matchingStrategy: 'all' // default last
186+
}
187+
```
188+
173189
## 🪡 Example with InstantSearch
174190

175191
The open-source [InstantSearch](https://www.algolia.com/doc/api-reference/widgets/js/) library powered by Algolia provides all the front-end tools you need to highly customize your search bar environment.

src/adapter/search-request-adapter/__tests__/search-params.tests.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { adaptSearchParams } from '../search-params-adapter'
2+
import { MatchingStrategies } from '../../../types'
23

34
const DEFAULT_CONTEXT = {
45
indexUid: 'test',
56
pagination: { paginationTotalHits: 20, page: 0, hitsPerPage: 6 },
67
defaultFacetDistribution: {},
8+
finitePagination: false,
79
}
810

911
describe('Parameters adapter', () => {
1012
test('adapting a basic searchContext ', () => {
1113
const searchParams = adaptSearchParams({
1214
...DEFAULT_CONTEXT,
13-
finitePagination: false,
1415
})
1516

1617
expect(searchParams.attributesToHighlight).toContain('*')
@@ -22,7 +23,6 @@ describe('Parameters adapter', () => {
2223
...DEFAULT_CONTEXT,
2324
facetFilters: [['genres:Drama', 'genres:Thriller'], ['title:Ariel']],
2425
sort: 'id < 1',
25-
finitePagination: false,
2626
})
2727

2828
expect(searchParams.filter).toStrictEqual([
@@ -33,6 +33,15 @@ describe('Parameters adapter', () => {
3333
expect(searchParams.attributesToHighlight).toContain('*')
3434
expect(searchParams.attributesToHighlight?.length).toBe(1)
3535
})
36+
37+
test('adapting a searchContext with matching strategy', () => {
38+
const searchParams = adaptSearchParams({
39+
...DEFAULT_CONTEXT,
40+
matchingStrategy: MatchingStrategies.ALL,
41+
})
42+
43+
expect(searchParams.matchingStrategy).toEqual('all')
44+
})
3645
})
3746

3847
describe('Geo rules adapter', () => {
@@ -42,7 +51,6 @@ describe('Geo rules adapter', () => {
4251
facetFilters: [['genres:Drama', 'genres:Thriller'], ['title:Ariel']],
4352
insideBoundingBox: '0,0,0,0',
4453
sort: 'id < 1',
45-
finitePagination: false,
4654
})
4755

4856
expect(searchParams.filter).toStrictEqual([
@@ -60,7 +68,6 @@ describe('Geo rules adapter', () => {
6068
...DEFAULT_CONTEXT,
6169
facetFilters: [['genres:Drama', 'genres:Thriller'], ['title:Ariel']],
6270
insideBoundingBox: '0,0,0,0',
63-
finitePagination: false,
6471
})
6572

6673
expect(searchParams.filter).toEqual([
@@ -77,7 +84,6 @@ describe('Geo rules adapter', () => {
7784
...DEFAULT_CONTEXT,
7885
insideBoundingBox: '0,0,0,0',
7986
sort: 'id < 1',
80-
finitePagination: false,
8187
})
8288

8389
expect(searchParams.filter).toEqual(['_geoRadius(0.00000, 0.00000, 0)'])
@@ -90,7 +96,6 @@ describe('Geo rules adapter', () => {
9096
const searchParams = adaptSearchParams({
9197
...DEFAULT_CONTEXT,
9298
insideBoundingBox: '0,0,0,0',
93-
finitePagination: false,
9499
})
95100

96101
expect(searchParams.filter).toEqual(['_geoRadius(0.00000, 0.00000, 0)'])
@@ -132,7 +137,6 @@ describe('Pagination adapter', () => {
132137
test('adapting a searchContext with no finite pagination', () => {
133138
const searchParams = adaptSearchParams({
134139
...DEFAULT_CONTEXT,
135-
finitePagination: false,
136140
})
137141

138142
expect(searchParams.limit).toBe(7)
@@ -142,7 +146,6 @@ describe('Pagination adapter', () => {
142146
const searchParams = adaptSearchParams({
143147
...DEFAULT_CONTEXT,
144148
pagination: { paginationTotalHits: 20, page: 1, hitsPerPage: 6 },
145-
finitePagination: false,
146149
})
147150

148151
expect(searchParams.limit).toBe(13)
@@ -152,7 +155,6 @@ describe('Pagination adapter', () => {
152155
const searchParams = adaptSearchParams({
153156
...DEFAULT_CONTEXT,
154157
pagination: { paginationTotalHits: 20, page: 40, hitsPerPage: 6 },
155-
finitePagination: false,
156158
})
157159

158160
expect(searchParams.limit).toBe(20)
@@ -162,7 +164,6 @@ describe('Pagination adapter', () => {
162164
const searchParams = adaptSearchParams({
163165
...DEFAULT_CONTEXT,
164166
pagination: { paginationTotalHits: 4, page: 0, hitsPerPage: 6 },
165-
finitePagination: false,
166167
})
167168

168169
expect(searchParams.limit).toBe(4)
@@ -173,7 +174,6 @@ describe('Pagination adapter', () => {
173174
...DEFAULT_CONTEXT,
174175
query: '',
175176
pagination: { paginationTotalHits: 4, page: 0, hitsPerPage: 6 },
176-
finitePagination: false,
177177
placeholderSearch: false,
178178
})
179179

@@ -185,7 +185,6 @@ describe('Pagination adapter', () => {
185185
...DEFAULT_CONTEXT,
186186
query: '',
187187
pagination: { paginationTotalHits: 200, page: 0, hitsPerPage: 6 },
188-
finitePagination: false,
189188
placeholderSearch: true,
190189
})
191190

src/adapter/search-request-adapter/search-params-adapter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function MeiliParamsCreator(searchContext: SearchContext) {
3232
finitePagination,
3333
sort,
3434
pagination,
35+
matchingStrategy,
3536
} = searchContext
3637

3738
return {
@@ -119,6 +120,11 @@ export function MeiliParamsCreator(searchContext: SearchContext) {
119120
}
120121
}
121122
},
123+
addMatchingStrategy() {
124+
if (matchingStrategy) {
125+
meiliSearchParams.matchingStrategy = matchingStrategy
126+
}
127+
},
122128
}
123129
}
124130

@@ -144,6 +150,7 @@ export function adaptSearchParams(
144150
meilisearchParams.addFilters()
145151
meilisearchParams.addSort()
146152
meilisearchParams.addGeoSearchRules()
153+
meilisearchParams.addMatchingStrategy()
147154

148155
return meilisearchParams.getParams()
149156
}

src/contexts/search-context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
AlgoliaMultipleQueriesQuery,
44
SearchContext,
55
FacetDistribution,
6+
MatchingStrategies,
67
} from '../types'
78

89
import { createPaginationContext } from './pagination-context'

src/types/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ export type ParsedFilter = {
2727
value: string
2828
}
2929

30+
export const enum MatchingStrategies {
31+
ALL = 'all',
32+
LAST = 'last',
33+
}
34+
3035
export type InstantMeiliSearchOptions = {
3136
paginationTotalHits?: number
3237
placeholderSearch?: boolean
3338
primaryKey?: string
3439
keepZeroFacets?: boolean
3540
finitePagination?: boolean
3641
clientAgents?: string[]
42+
matchingStrategy?: MatchingStrategies
3743
}
3844

3945
export type SearchCacheInterface = {
@@ -79,6 +85,7 @@ export type SearchContext = Omit<InstantSearchParams, 'insideBoundingBox'> &
7985
sort?: string
8086
placeholderSearch?: boolean
8187
primaryKey?: string
88+
matchingStrategy?: MatchingStrategies
8289
}
8390

8491
export type InstantMeiliSearchInstance = SearchClient & {

0 commit comments

Comments
 (0)