Skip to content

Commit f95e70a

Browse files
Merge #1258
1258: Expose a set of Meilisearch search parameters to be overridden/configured r=brunoocasali a=flevi29 # Pull Request ## Related issue Fixes #1249 ## What does this PR do? - Adds ability to configure and override a selection of Meilisearch search parameters - Modifies some types, stronger type checking ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: F. Levi <55688616+flevi29@users.noreply.github.com>
2 parents 8042361 + 95d1f6e commit f95e70a

File tree

27 files changed

+521
-287
lines changed

27 files changed

+521
-287
lines changed

.changeset/metal-emus-pump.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
"@meilisearch/instant-meilisearch": major
3+
"@meilisearch/autocomplete-client": patch
4+
---
5+
6+
Added ability to override a selection of Meilisearch search parameters.
7+
8+
⚠️ The returned value of the core `instantMeiliSearch` function has changed!
9+
10+
This change was necessary for the aforementioned ability to be implemented and
11+
applied in a clean manner.
12+
The necessary migration should be of minimal impact.
13+
14+
### Migration
15+
16+
Change the following
17+
18+
```js
19+
// 1.
20+
const client = instantMeiliSearch(/*...*/)
21+
// 2.
22+
const searchClient = instantMeiliSearch(/*...*/)
23+
// 3.
24+
instantsearch({
25+
indexName: 'movies',
26+
searchClient: instantMeiliSearch(/*...*/),
27+
})
28+
```
29+
30+
to the following
31+
32+
```js
33+
// 1.
34+
const { searchClient: client } = instantMeiliSearch(/*...*/)
35+
// 2.
36+
const { searchClient } = instantMeiliSearch(/*...*/)
37+
// 3.
38+
instantsearch({
39+
indexName: 'movies',
40+
searchClient: instantMeiliSearch(/*...*/).searchClient,
41+
})
42+
```

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ landing_getting_started_1: |-
1313
searchClient: instantMeiliSearch(
1414
'http://localhost:7700',
1515
'searchKey'
16-
),
16+
).searchClient,
1717
})
1818
1919
search.addWidgets([
@@ -59,7 +59,7 @@ getting_started_front_end_integration_md: |-
5959
indexName: "movies",
6060
searchClient: instantMeiliSearch(
6161
"http://localhost:7700"
62-
)
62+
).searchClient
6363
});
6464
search.addWidgets([
6565
instantsearch.widgets.searchBox({

packages/autocomplete-client/src/client/createSearchClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function createSearchClient({ userAgent }: { userAgent: string }) {
1616
options = { clientAgents: [] },
1717
}: ClientConfig): SearchClient => {
1818
const clientAgents = options.clientAgents || []
19-
const searchClient = instantMeiliSearch(url, apiKey, {
19+
const { searchClient } = instantMeiliSearch(url, apiKey, {
2020
...options,
2121
clientAgents: concatUserAgents([userAgent, ...clientAgents]),
2222
})

packages/instant-meilisearch/README.md

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ To be able to create a search interface, you'll need to [install `instantsearch.
8080
```js
8181
import { instantMeiliSearch } from '@meilisearch/instant-meilisearch'
8282

83-
const searchClient = instantMeiliSearch(
83+
const { searchClient, setMeiliSearchParams } = instantMeiliSearch(
8484
'https://ms-adf78ae33284-106.lon.meilisearch.io', // Host
8585
'a63da4928426f12639e19d62886f621130f3fa9ff3c7534c5d179f0f51c4f303' // API key
8686
)
8787
```
8888

89+
where `searchClient` is to be passed to instantsearch.js or its many framework adaptations, and [`setMeiliSearchParams`](#modify-meilisearch-search-parameters) is a function used to set/modify certain Meilisearch search parameters to be overridden.
90+
8991
### Parameters
9092

9193
- `Host` - URL of Meilisearch instance
@@ -96,19 +98,19 @@ const searchClient = instantMeiliSearch(
9698
`instant-meilisearch` offers some options you can set to further fit your needs.
9799

98100
- [`placeholderSearch`](#placeholder-search): Enable or disable placeholder search (default: `true`).
99-
- [`finitePagination`](#finite-pagination): Enable finite pagination when using the the [`pagination`](#-pagination) widget (default: `false`) .
101+
- [`finitePagination`](#finite-pagination): Enable finite pagination when using the [`pagination`](#-pagination) widget (default: `false`) .
100102
- [`primaryKey`](#primary-key): Specify the primary key of your documents (default `undefined`).
101103
- [`keepZeroFacets`](#keep-zero-facets): Show the facets value even when they have 0 matches (default `false`).
102-
- [`matchingStrategy`](#matching-strategy): Determine the search strategy on words matching (default `last`).
103104
- [`requestConfig`](#request-config): Use custom request configurations.
104-
- ['httpClient'](#custom-http-client): Use a custom HTTP client.
105+
- [`httpClient`](#custom-http-client): Use a custom HTTP client.
106+
- [`meiliSearchParams`](#meilisearch-search-parameters): Override a selection of Meilisearch search parameters (default `undefined`).
105107

106108
The options are added as the third parameter of the `instantMeilisearch` function.
107109

108110
```js
109111
import { instantMeiliSearch } from '@meilisearch/instant-meilisearch'
110112

111-
const searchClient = instantMeiliSearch(
113+
const { searchClient } = instantMeiliSearch(
112114
'https://ms-adf78ae33284-106.lon.meilisearch.io',
113115
'a63da4928426f12639e19d62886f621130f3fa9ff3c7534c5d179f0f51c4f303',
114116
{
@@ -172,20 +174,6 @@ genres:
172174
{ keepZeroFacets : true } // default: false
173175
```
174176

175-
### Matching strategy
176-
177-
`matchingStrategy` gives you the possibility to choose how Meilisearch should handle the presence of multiple query words, see [documentation](https://www.meilisearch.com/docs/reference/api/search#matching-strategy).
178-
179-
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.
180-
The other strategy is `all`, where both `hello` and `world` **must** be present in a document for it to be returned.
181-
182-
183-
```js
184-
{
185-
matchingStrategy: 'all' // default last
186-
}
187-
```
188-
189177
### Request Config
190178

191179
You can provide a custom request configuration. Available field can be [found here](https://fetch.spec.whatwg.org/#requestinit).
@@ -221,6 +209,66 @@ You can use your own HTTP client, for example, with [`axios`](https://github.com
221209
}
222210
```
223211

212+
### Meilisearch search parameters
213+
214+
`meiliSearchParams` lets you override a set of search parameters that are sent off to Meilisearch.
215+
The following options can be overridden:
216+
[`attributesToRetrieve`](https://www.meilisearch.com/docs/reference/api/search#attributes-to-retrieve),
217+
[`attributesToCrop`](https://www.meilisearch.com/docs/reference/api/search#attributes-to-crop),
218+
[`cropLength`](https://www.meilisearch.com/docs/reference/api/search#crop-length),
219+
[`cropMarker`](https://www.meilisearch.com/docs/reference/api/search#crop-marker),
220+
[`attributesToHighlight`](https://www.meilisearch.com/docs/reference/api/search#attributes-to-highlight),
221+
[`highlightPreTag`, `highlightPostTag`](https://www.meilisearch.com/docs/reference/api/search#highlight-tags),
222+
[`showMatchesPosition`](https://www.meilisearch.com/docs/reference/api/search#show-matches-position),
223+
[`matchingStrategy`](https://www.meilisearch.com/docs/reference/api/search#matching-strategy),
224+
[`showRankingScore`](https://www.meilisearch.com/docs/reference/api/search#ranking-score),
225+
[`attributesToSearchOn`](https://www.meilisearch.com/docs/reference/api/search#customize-attributes-to-search-on-at-search-time).
226+
227+
```js
228+
instantMeiliSearch(
229+
// ...
230+
{
231+
meiliSearchParams: {
232+
attributesToHighlight: ['overview'],
233+
highlightPreTag: '<em>',
234+
highlightPostTag: '</em>',
235+
attributesToSearchOn: ['overview'],
236+
},
237+
}
238+
)
239+
```
240+
241+
### Modify Meilisearch search parameters
242+
243+
`instantMeiliSearch` returns an instance with two properties on it, one of them being `setMeiliSearchParams`.
244+
245+
```js
246+
const { searchClient, setMeiliSearchParams } = instantMeiliSearch(/*...*/)
247+
```
248+
249+
It modifies (or sets if not already set) the [overridden Meilisearch search parameters](#meilisearch-search-parameters).
250+
It only modifies parameters that are defined on the provided object, the following will not change `attributesToHighlight`.
251+
252+
```js
253+
const { setMeiliSearchParams } = instantMeiliSearch(
254+
// ...
255+
{
256+
meiliSearchParams: {
257+
attributesToHighlight: ['overview'],
258+
highlightPreTag: '<em>',
259+
highlightPostTag: '</em>',
260+
attributesToSearchOn: ['overview'],
261+
},
262+
}
263+
)
264+
265+
setMeiliSearchParams({
266+
highlightPreTag: '<mark>',
267+
highlightPostTag: '</mark>',
268+
attributesToSearchOn: ['overview', 'title'],
269+
})
270+
```
271+
224272
## 🪡 Example with InstantSearch
225273

226274
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.
@@ -252,12 +300,14 @@ In `index.html`:
252300
In `app.js`:
253301

254302
```js
303+
const { searchClient } = instantMeiliSearch(
304+
'https://ms-adf78ae33284-106.lon.meilisearch.io',
305+
'a63da4928426f12639e19d62886f621130f3fa9ff3c7534c5d179f0f51c4f303'
306+
)
307+
255308
const search = instantsearch({
256309
indexName: 'steam-video-games',
257-
searchClient: instantMeiliSearch(
258-
'https://ms-adf78ae33284-106.lon.meilisearch.io',
259-
'a63da4928426f12639e19d62886f621130f3fa9ff3c7534c5d179f0f51c4f303'
260-
),
310+
searchClient,
261311
})
262312

263313
search.addWidgets([
@@ -381,7 +431,7 @@ const search = instantsearch({
381431
{
382432
// ... InstantMeiliSearch options
383433
}
384-
),
434+
).searchClient,
385435
// ... InstantSearch options
386436
routing: true // for example
387437
})

packages/instant-meilisearch/__tests__/assets/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,11 @@ export type Movies = {
228228
_highlightResult?: Movies
229229
}
230230

231-
const searchClient = instantMeiliSearch('http://localhost:7700', 'masterKey')
232-
const wrongSearchClient = instantMeiliSearch(
231+
const { searchClient } = instantMeiliSearch(
232+
'http://localhost:7700',
233+
'masterKey'
234+
)
235+
const { searchClient: wrongSearchClient } = instantMeiliSearch(
233236
'http://localhost:7777',
234237
'masterKey'
235238
)

packages/instant-meilisearch/__tests__/custom-http-client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Custom HTTP client tests', () => {
1818
return await result.json()
1919
})
2020

21-
const searchClient = instantMeiliSearch(
21+
const { searchClient } = instantMeiliSearch(
2222
'http://localhost:7700',
2323
'masterKey',
2424
{

0 commit comments

Comments
 (0)