Skip to content

Commit 60d81bc

Browse files
committed
feat(meilisearch-connector): update addContentTypeInMeiliSearch
to handle multiple index name
1 parent ce71eea commit 60d81bc

File tree

2 files changed

+116
-18
lines changed

2 files changed

+116
-18
lines changed

server/__tests__/meilisearch.test.js

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ describe('Tests content types', () => {
113113
expect(tasks).toEqual(10)
114114
})
115115

116-
117116
test('Test to add entries linked to multiple indexes in Meilisearch', async () => {
118117
const pluginMock = jest.fn(() => ({
119118
// This rewrites only the needed methods to reach the system under test (removeSensitiveFields)
@@ -192,10 +191,10 @@ describe('Tests content types', () => {
192191

193192
expect(strapi.log.info).toHaveBeenCalledTimes(2)
194193
expect(strapi.log.info).toHaveBeenCalledWith(
195-
'The task to add 2 documents to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).'
194+
'The task to add 2 documents to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).',
196195
)
197196
expect(strapi.log.info).toHaveBeenCalledWith(
198-
'The task to add 2 documents to the Meilisearch index "anotherIndex" has been enqueued (Task uid: undefined).'
197+
'The task to add 2 documents to the Meilisearch index "anotherIndex" has been enqueued (Task uid: undefined).',
199198
)
200199
expect(client.index('').addDocuments).toHaveBeenCalledTimes(2)
201200
expect(client.index).toHaveBeenCalledWith('customIndex')
@@ -256,10 +255,10 @@ describe('Tests content types', () => {
256255

257256
expect(customStrapi.log.info).toHaveBeenCalledTimes(2)
258257
expect(customStrapi.log.info).toHaveBeenCalledWith(
259-
'A task to delete 2 documents of the index "customIndex" in Meilisearch has been enqueued (Task uid: undefined).'
258+
'A task to delete 2 documents of the index "customIndex" in Meilisearch has been enqueued (Task uid: undefined).',
260259
)
261260
expect(customStrapi.log.info).toHaveBeenCalledWith(
262-
'A task to delete 2 documents of the index "anotherIndex" in Meilisearch has been enqueued (Task uid: undefined).'
261+
'A task to delete 2 documents of the index "anotherIndex" in Meilisearch has been enqueued (Task uid: undefined).',
263262
)
264263
expect(client.index('').deleteDocuments).toHaveBeenCalledTimes(2)
265264
expect(client.index('').deleteDocuments).toHaveBeenCalledWith([
@@ -335,6 +334,95 @@ describe('Tests content types', () => {
335334
)
336335
})
337336

337+
test('Test to update the content of a collection in Meilisearch with a custom index name', async () => {
338+
const customStrapi = createStrapiMock({
339+
restaurantConfig: {
340+
indexName: ['customIndex'],
341+
entriesQuery: {
342+
limit: 1,
343+
fields: ['id'],
344+
filters: {},
345+
sort: {},
346+
populate: [],
347+
publicationState: 'preview',
348+
},
349+
},
350+
})
351+
352+
const meilisearchService = createMeilisearchService({
353+
strapi: customStrapi,
354+
})
355+
356+
await meilisearchService.addContentTypeInMeiliSearch({
357+
contentType: 'restaurant',
358+
})
359+
360+
expect(
361+
customStrapi.plugin().service().actionInBatches
362+
).toHaveBeenCalledWith({
363+
contentType: 'restaurant',
364+
callback: expect.anything(),
365+
entriesQuery: {
366+
limit: 1,
367+
fields: ['id'],
368+
filters: {},
369+
sort: {},
370+
populate: [],
371+
publicationState: 'preview',
372+
},
373+
})
374+
expect(customStrapi.log.info).toHaveBeenCalledTimes(1)
375+
expect(customStrapi.log.info).toHaveBeenCalledWith(
376+
'A task to update the settings to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).'
377+
)
378+
})
379+
380+
test('Test to update the content of a collection in Meilisearch with a multiple index names', async () => {
381+
const customStrapi = createStrapiMock({
382+
restaurantConfig: {
383+
indexName: ['customIndex', 'anotherIndex'],
384+
entriesQuery: {
385+
limit: 1,
386+
fields: ['id'],
387+
filters: {},
388+
sort: {},
389+
populate: [],
390+
publicationState: 'preview',
391+
},
392+
},
393+
})
394+
395+
const meilisearchService = createMeilisearchService({
396+
strapi: customStrapi,
397+
})
398+
399+
await meilisearchService.addContentTypeInMeiliSearch({
400+
contentType: 'restaurant',
401+
})
402+
403+
expect(
404+
customStrapi.plugin().service().actionInBatches
405+
).toHaveBeenCalledWith({
406+
contentType: 'restaurant',
407+
callback: expect.anything(),
408+
entriesQuery: {
409+
limit: 1,
410+
fields: ['id'],
411+
filters: {},
412+
sort: {},
413+
populate: [],
414+
publicationState: 'preview',
415+
},
416+
})
417+
expect(customStrapi.log.info).toHaveBeenCalledTimes(2)
418+
expect(customStrapi.log.info).toHaveBeenCalledWith(
419+
'A task to update the settings to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).'
420+
)
421+
expect(customStrapi.log.info).toHaveBeenCalledWith(
422+
'A task to update the settings to the Meilisearch index "anotherIndex" has been enqueued (Task uid: undefined).'
423+
)
424+
})
425+
338426
test('Test to update the content of a collection in Meilisearch with a custom index name', async () => {
339427
const customStrapi = createStrapiMock({
340428
restaurantConfig: {

server/services/meilisearch/connector.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ module.exports = ({ strapi, adapter, config }) => {
291291
},
292292

293293
/**
294-
* Add all entries from a contentType to its index in Meilisearch.
294+
* Add all entries from a contentType to all its indexes in Meilisearch.
295295
*
296296
* @param {object} options
297297
* @param {string} options.contentType - ContentType name.
@@ -301,14 +301,18 @@ module.exports = ({ strapi, adapter, config }) => {
301301
addContentTypeInMeiliSearch: async function ({ contentType }) {
302302
const { apiKey, host } = await store.getCredentials()
303303
const client = Meilisearch({ apiKey, host })
304-
const indexUid = config.getIndexNameOfContentType({ contentType })
304+
const indexUids = config.getIndexNamesOfContentType({ contentType })
305305

306306
// Get Meilisearch Index settings from model
307307
const settings = config.getSettings({ contentType })
308-
const task = await client.index(indexUid).updateSettings(settings)
309-
310-
strapi.log.info(
311-
`A task to update the settings to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${task.taskUid}).`,
308+
await Promise.all(
309+
indexUids.map(async indexUid => {
310+
const task = await client.index(indexUid).updateSettings(settings)
311+
strapi.log.info(
312+
`A task to update the settings to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${task.taskUid}).`,
313+
)
314+
return task
315+
}),
312316
)
313317

314318
// Callback function for batching action
@@ -322,15 +326,21 @@ module.exports = ({ strapi, adapter, config }) => {
322326
})
323327

324328
// Add documents in Meilisearch
325-
const { taskUid } = await client
326-
.index(indexUid)
327-
.addDocuments(documents, { primaryKey: '_meilisearch_id' })
328-
329-
strapi.log.info(
330-
`A task to add ${documents.length} documents to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${taskUid}).`,
329+
const taskUids = await Promise.all(
330+
indexUids.map(async indexUid => {
331+
const { taskUid } = await client
332+
.index(indexUid)
333+
.addDocuments(documents, { primaryKey: '_meilisearch_id' })
334+
335+
strapi.log.info(
336+
`A task to add ${documents.length} documents to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${taskUid}).`,
337+
)
338+
339+
return taskUid
340+
}),
331341
)
332342

333-
return taskUid
343+
return taskUids.flat()
334344
}
335345

336346
const tasksUids = await contentTypeService.actionInBatches({

0 commit comments

Comments
 (0)