Skip to content

Commit 11d9aac

Browse files
Merge #1011
1011: Add support for indexing with wildcard locale r=Strift a=DanielRoeven-Intunio # Pull Request ## Related issue Fixes #988 ## What does this PR do? - Adds back support for indexing (all) localised content types in Strapi. - Updates README and examples The [i18n plugin](https://docs-v4.strapi.io/dev-docs/plugins/i18n) in Strapi 4 to localise content used the key-value pair `locale: 'all'` in the `entriesQuery` to query entries in all locales. [This is reflected in the `strapi-plugin-meilisearch` README](https://github.com/meilisearch/strapi-plugin-meilisearch?tab=readme-ov-file#-entries-query). [In Strapi 5, i18n is now part of the Strapi core](https://docs.strapi.io/dev-docs/migration/v4-to-v5/breaking-changes/i18n-content-manager-locale). With this change, the key-value pair changed to `locale: '*'`. (Or perhaps this change happened together with the move from the [Entity Service API](https://docs.strapi.io/dev-docs/api/entity-service/crud) to the [Document Service API](https://docs.strapi.io/dev-docs/api/document-service)). Reading the code, it appears that this plugin already uses the Document Service, but the [link in the `strapi-plugin-meilisearch` README still points to the Entity Service](https://github.com/meilisearch/strapi-plugin-meilisearch?tab=readme-ov-file#-entries-query). Regardless of whether the root cause is the Strapi 4 + i18n / Strapi 5 change, or the Entity Service / Document Service change, I've updated the README to point to the correct docs.) This PR only _adds_ support for the wildcard `*` locale, and left support in for the `all` locale, used previously, maintaining backwards compatibility. I've updated the README to reflect usage first and foremost with Strapi 5, with a small note for usage with Strapi 4. ## 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? Thanks for the work on the plugin, happy to hear your thoughts and make adjustments to the PR as needed! Co-authored-by: Daniel Roeven <[email protected]>
2 parents 2add7d0 + 3e12351 commit 11d9aac

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,24 +427,26 @@ The options you can set are described in the [`findMany` documentation](https://
427427

428428
**Common use cases**
429429

430-
If you are using the [🌍 Internationalization (i18n)](https://docs.strapi.io/developer-docs/latest/plugins/i18n.html) plugin, an additional field `locale` should also be added in `entriesQuery`.
430+
If you are localizing your Strapi content, an additional field `locale` should also be added in `entriesQuery`.
431431

432-
⚠️ Warning: if you do not specify `locale: "all"` in `entriesQuery`, you may not index all available entries, potentially leading to missing products in your search results. To ensure all entries in every language are indexed in Meilisearch, include the `locale` field with the value 'all'.
432+
⚠️ Warning: if you do not specify `locale: "*"` in `entriesQuery`, you may not index all available entries, potentially leading to missing products in your search results. To ensure all entries in every language are indexed in Meilisearch, include the `locale` field with the value 'all'.
433433

434434
```js
435435
module.exports = {
436436
meilisearch: {
437437
config: {
438438
restaurant: {
439439
entriesQuery: {
440-
locale: 'all',
440+
locale: '*',
441441
},
442442
},
443443
},
444444
},
445445
}
446446
```
447447

448+
If you are using Strapi 4 with the [🌍 Internationalization (i18n)](https://docs.strapi.io/developer-docs/latest/plugins/i18n.html) plugin, the `locale` field should be set to `all`.
449+
448450
If you want to add a collection with a relation to the collection being included, you have to configure the `populate` parameter in `entriesQuery`. See [the docs](https://docs.strapi.io/dev-docs/api/entity-service/populate) on how it works, and [an example](./resources/entries-query/populate.js) in our resources.
449451

450452
**Example**

resources/entries-query/locale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
config: {
55
restaurant: {
66
entriesQuery: {
7-
locale: 'all',
7+
locale: '*',
88
},
99
},
1010
},

server/src/__tests__/configuration.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,34 @@ describe('Test Meilisearch plugin configurations', () => {
393393
expect(entries).toEqual([{ id: 1, locale: 'fr' }])
394394
})
395395

396+
test('Test should not remove any entries with wildcard locale', async () => {
397+
const customStrapi = createStrapiMock({
398+
restaurantConfig: {
399+
entriesQuery: {
400+
locale: '*',
401+
},
402+
},
403+
})
404+
405+
const contentType = 'restaurant'
406+
const meilisearchService = createMeilisearchService({
407+
strapi: customStrapi,
408+
})
409+
410+
const entries = meilisearchService.removeLocaleEntries({
411+
contentType,
412+
entries: [
413+
{ id: 1, locale: 'fr' },
414+
{ id: 2, locale: 'en' },
415+
],
416+
})
417+
418+
expect(entries).toEqual([
419+
{ id: 1, locale: 'fr' },
420+
{ id: 2, locale: 'en' },
421+
])
422+
})
423+
396424
test('Test should keep unpublished entries when status is set to draft', async () => {
397425
const customStrapi = createStrapiMock({
398426
restaurantConfig: {

server/src/services/meilisearch/config.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,10 @@ export default ({ strapi }) => {
243243

244244
/**
245245
* Remove language entries.
246-
* In the plugin entriesQuery, if `locale` is set and not equal to `all`
247-
* all entries that do not have the specified language are removed.
246+
* In the plugin entriesQuery, if `locale` is set and not equal to
247+
* `all` (used with the i18n plugin for Strapi) or `*` (used with Strapi 5
248+
* native localization), all entries that do not have the specified
249+
* language are removed.
248250
*
249251
* @param {object} options
250252
* @param {Array<Object>} options.entries - The entries to filter.
@@ -258,7 +260,11 @@ export default ({ strapi }) => {
258260

259261
const entriesQuery = contentTypeConfig.entriesQuery || {}
260262

261-
if (!entriesQuery.locale || entriesQuery.locale === 'all') {
263+
if (
264+
!entriesQuery.locale ||
265+
entriesQuery.locale === 'all' ||
266+
entriesQuery.locale === '*'
267+
) {
262268
return entries
263269
} else {
264270
return entries.filter(entry => entry.locale === entriesQuery.locale)

0 commit comments

Comments
 (0)