Skip to content

Commit b7c6b41

Browse files
Merge #538
538: Add entries query option in plugin configuration r=bidoubiwa a=bidoubiwa # Pull Request ## Related issue Fixes #536 Fixes #263 Fixes #85 ## What does this PR do? A lot of users were asking for more control over what entries are indexed in Meilisearch and how. For example: - the batch sizes which has a default value of 500. - the `locale` support to fetch all entries of every language This PR adds a new setting `entriesQuery` that lets you set exactly how Meilisearch should fetch documents from your database. These options are provided directly by the `findMany` API of strapi ([see doc](https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/entity-service/crud.html#findmany)). Further explanation on how it works isn provided in the README of this PR. ## ❌ Breaking changes The setting `populateEntryRule` in the plugin configuration is removed in favor of `populate` in `entriesQuery`. ## 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: Charlotte Vermandel <[email protected]>
2 parents 19b9d52 + 312c68d commit b7c6b41

File tree

15 files changed

+1001
-155
lines changed

15 files changed

+1001
-155
lines changed

README.md

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Settings:
202202
- [🪄 Transform entries](#-transform-entries)
203203
- [🤚 Filter entries](#-filter-entries)
204204
- [🏗 Add Meilisearch settings](#-add-meilisearch-settings)
205-
- [👥 Populate entry rule](#-populate-entry-rule)
205+
- [🔎 Entries query](#🔎-entries-query)
206206

207207
### 🏷 Custom index name
208208

@@ -373,59 +373,33 @@ module.exports = {
373373

374374
[See resources](./resources/meilisearch-settings) for more settings examples.
375375

376-
### 👥 Populate entry rule
376+
### 🔎 Entries query
377377

378-
Content-types in Strapi may have relationships with other content-types (ex: `restaurant` can have a many-to-many relation with `category`). To ensure that these links are fetched and added to an entry correctly from your Strapi database, the correct populate rule must be provided ([see documentation](https://docs-next.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/entity-service/populate.html#basic-populating)).
378+
When indexing a content type to Meilisearch, the plugin has to fetch the documents from your database. With `entriesQuery` it is possible to specify some options that should be applied during the fetching of the entries.
379+
The options you can set are described in the [`findMany` documentation](https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/entity-service/crud.html#findmany) of Strapi. However, we do not accept any changes on the `start` parameter.
379380

380-
To communicate the populate rule, use the `populateEntryRule` setting on the according content-type in the plugin's settings.
381+
If you are using the [🌍 Internationalization (i18n)](https://docs.strapi.io/developer-docs/latest/plugins/i18n.html) plugin, an additional field `locale` can also be added in `entriesQuery`.
381382

382383
**For example**
383384

384-
Imagine my `restaurant` content-type has a relation with a repeatable-component `repeatableComponent` that itself has a relationship with the content-type `categories`.
385-
386-
The following population will ensure that a `restaurant` entry contains even the most nested relation.
385+
For example, if you want your documents to be fetched in batches of `1000` you specify it in the `entriesQuery` option.
387386

388387
```js
389388
module.exports = {
390389
meilisearch: {
391390
config: {
392391
restaurant: {
393-
populateEntryRule: ['repeatableComponent.categories', 'categories'],
392+
entriesQuery: {
393+
limit: 1000
394+
}
394395
}
395396
}
396397
},
397398
}
398399
```
399400

400-
by providing this, the following is indexed in Meilisearch:
401-
402-
```json
403-
{
404-
"id": "restaurant-1",
405-
"title": "The slimmy snail",
406-
// ... other restaurant fields
407-
"repeatableComponent": [
408-
{
409-
"id": 1,
410-
"title": "my repeatable component 1"
411-
"categories": [
412-
{
413-
"id": 3,
414-
"name": "Asian",
415-
// ... other category fields
416-
},
417-
{
418-
"id": 2,
419-
"name": "Healthy",
420-
// ... other category fields
421-
}
422-
],
401+
[See resources](./resources/entries-query) for more entriesQuery examples.
423402

424-
}
425-
],
426-
427-
}
428-
```
429403

430404
### 🕵️‍♀️ Start Searching <!-- omit in toc -->
431405

resources/entries-query/locale.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// All entries in every language are indexed in Meilisearch.
2+
module.exports = {
3+
meilisearch: {
4+
config: {
5+
restaurant: {
6+
locale: 'all',
7+
},
8+
},
9+
},
10+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = {
2+
meilisearch: {
3+
config: {
4+
restaurant: {
5+
entriesQuery: {
6+
populate: ['repeatableComponent.categories', 'categories'],
7+
},
8+
},
9+
},
10+
},
11+
}
12+
13+
// The following document structure is indexed in Meilisearch
14+
// {
15+
// id: 'restaurant-1',
16+
// title: 'The slimmy snail',
17+
// // ... other restaurant fields
18+
// repeatableComponent: [
19+
// {
20+
// id: 1,
21+
// title: 'my repeatable component 1',
22+
// categories: [
23+
// {
24+
// id: 3,
25+
// name: 'Asian',
26+
// // ... other category fields
27+
// },
28+
// {
29+
// id: 2,
30+
// name: 'Healthy',
31+
// // ... other category fields
32+
// },
33+
// ],
34+
// },
35+
// ],
36+
// }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Both published and draft entries are added in Meilisearch
2+
module.exports = {
3+
meilisearch: {
4+
config: {
5+
restaurant: {
6+
publicationState: 'preview',
7+
},
8+
},
9+
},
10+
}

server/__mocks__/meilisearch.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const addDocumentsMock = jest.fn(() => 10)
2+
const updateDocumentsMock = jest.fn(() => 10)
3+
const updateSettingsMock = jest.fn(() => 10)
4+
const deleteDocuments = jest.fn(() => {
5+
return [{ taskUid: 1 }, { taskUid: 2 }]
6+
})
7+
const getIndexes = jest.fn(() => {
8+
return { results: [{ uid: 'my_restaurant' }, { uid: 'restaurant' }] }
9+
})
10+
11+
const getTasks = jest.fn(() => {
12+
return {
13+
results: [
14+
{ uid: 1, status: 'enqueued', indexUid: 'restaurant' },
15+
{ uid: 2, status: 'processed', indexUid: 'restaurant' },
16+
{ uid: 3, status: 'enqueued', indexUid: 'about' },
17+
],
18+
}
19+
})
20+
21+
const getStats = jest.fn(() => {
22+
return { numberOfDocuments: 1, isIndexing: false, fieldDistribution: {} }
23+
})
24+
25+
const mockIndex = jest.fn(() => ({
26+
addDocuments: addDocumentsMock,
27+
updateDocuments: updateDocumentsMock,
28+
updateSettings: updateSettingsMock,
29+
deleteDocuments,
30+
getStats,
31+
}))
32+
33+
// @ts-ignore
34+
const mock = jest.fn().mockImplementation(() => {
35+
return {
36+
getIndexes,
37+
index: mockIndex,
38+
getTasks,
39+
}
40+
})
41+
42+
module.exports = { MeiliSearch: mock }

0 commit comments

Comments
 (0)