Skip to content

Commit 7c558c2

Browse files
bors[bot]bidoubiwa
andauthored
Merge #322
322: Add objectID when primaryKey is provided r=bidoubiwa a=bidoubiwa ## Problem With the new version of `instantSearch`, `objectID` becomes mandatory in the hits information to avoid errors and warnings in the console. For example, in `React` when using instant-meilisearch the following error is raised: ``` react_devtools_backend.js:2430 Warning: Failed prop type: The prop `hits[0].objectID` is marked as required in `Hits`, but its value is `undefined`. react_devtools_backend.js:2430 Warning: Each child in a list should have a unique "key" prop. ``` Because instantSearch adds the `objectID` as the key of every hit component, we both receive an error for its absence and an error because our components are missing a key prop as [react expects a key on every component](https://reactjs.org/docs/lists-and-keys.html#keys). ## Solution First I tried to fetch the primaryKey from MeiliSearch but the public key does not give permission to fetch the primaryKey. So the solution was to add the `primaryKey` option to instant-meilisearch options. If it is not present, the `objectID` field will not be added in the returned hits. If a wrong primaryKey is given its value will be undefined and the error above will be raised the same way. ### Usage ```javascript const searchClient = instantMeiliSearch( 'https://demos.meilisearch.com', 'dc3fedaf922de8937fdea01f0a7d59557f1fd31832cb8440ce94231cfdde7f25', { paginationTotalHits: 60, primaryKey: 'id', } ) ``` Not breaking! Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents f3338ae + 6aab0c0 commit 7c558c2

File tree

4 files changed

+7
-0
lines changed

4 files changed

+7
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const searchClient = instantMeiliSearch(
7171
{
7272
paginationTotalHits: 30, // default: 200.
7373
placeholderSearch: false // default: true.
74+
primaryKey: 'id' // default: undefined
7475
}
7576
);
7677
```
@@ -82,6 +83,7 @@ Which means that, with a `paginationTotalHits` default value of 200, and `hitsPe
8283
The default value of `hitsPerPage` is set to `20` but it can be changed with [`InsantSearch.configure`](https://www.algolia.com/doc/api-reference/widgets/configure/js/#examples).<br>
8384
⚠️ MeiliSearch is not designed for pagination and this can lead to performances issues, so the usage of the pagination widget is not encouraged. However, the `paginationTotalHits` parameter lets you implement this pagination with less performance issue as possible: depending on your dataset (the size of each document and the number of documents) you might decrease the value of `paginationTotalHits`.<br>
8485
More information about MeiliSearch and the pagination [here](https://github.com/meilisearch/documentation/issues/561).
86+
- `primaryKey` (`undefined` by default): Specify the field in your documents containing the [unique identifier](https://docs.meilisearch.com/learn/core_concepts/documents.html#primary-field). By adding this option, we avoid instantSearch errors that are thrown in the browser console. In `React` particulary, this option removes the `Each child in a list should have a unique "key" prop` error.
8587

8688
## Example with InstantSearch
8789

playgrounds/react/src/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const searchClient = instantMeiliSearch(
1818
'dc3fedaf922de8937fdea01f0a7d59557f1fd31832cb8440ce94231cfdde7f25',
1919
{
2020
paginationTotalHits: 60,
21+
primaryKey: 'id',
2122
}
2223
)
2324

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function instantMeiliSearch(
1515
client: new MeiliSearch({ host: hostUrl, apiKey: apiKey }),
1616
attributesToHighlight: ['*'],
1717
paginationTotalHits: options.paginationTotalHits || 200,
18+
primaryKey: options.primaryKey || undefined,
1819
placeholderSearch: options.placeholderSearch !== false, // true by default
1920
hitsPerPage: 20,
2021
/*
@@ -99,6 +100,7 @@ export function instantMeiliSearch(
99100
formattedHit,
100101
...instantSearchParams,
101102
}),
103+
...(this.primaryKey && { objectID: hit[this.primaryKey] }),
102104
}
103105
return modifiedHit
104106
})

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type ISSearchParams = IStypes.SearchRequestParameters &
99
export type InstantMeiliSearchOptions = {
1010
paginationTotalHits?: number
1111
placeholderSearch?: boolean
12+
primaryKey?: string
1213
}
1314

1415
export type ISHits<T = Record<string, any>> = T & {
@@ -35,6 +36,7 @@ export type InstantMeiliSearchInstance = {
3536
pagination?: boolean
3637
paginationTotalHits: number
3738
hitsPerPage: number
39+
primaryKey: string | undefined
3840
client: MStypes.MeiliSearch
3941
attributesToHighlight: string[]
4042
placeholderSearch: boolean

0 commit comments

Comments
 (0)