Skip to content

Commit c702e85

Browse files
Merge #818
818: fix: getIndexes limited to 20 indexes r=bidoubiwa a=nicolasvienot # Pull Request ## Related issue Fixes #714 ## Context Selecting more than 20 collections in the Meilisearch plugin causes issues where collections can't be selected or are reset in the Meilisearch plugin. ## What does this PR do? The issue is that when retrieving the indexes through the `getIndexes` function, there is a limit set automatically by Meilisearch to `20` ([See reference](https://www.meilisearch.com/docs/reference/api/indexes#list-all-indexes)). As a result, only 20 indexes will be returned and the later checks between indexes in Meilisearch and collections in Strapi are wrong. This PR changes the call to Meilisearch from GET `/indexes` to GET `/stats` which does not have a `limit`. - Update `getIndexes` to `getIndexUids` and use the `getStats` function from `meilisearch-js` - Return an array containing all the `indexUids` - Update tests ## 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: nicolasvienot <[email protected]>
2 parents 9f56236 + a14313c commit c702e85

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

server/__mocks__/meilisearch.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@ const updateSettingsMock = jest.fn(() => 10)
44
const deleteDocuments = jest.fn(() => {
55
return [{ taskUid: 1 }, { taskUid: 2 }]
66
})
7-
const getIndexes = jest.fn(() => {
8-
return { results: [{ uid: 'my_restaurant' }, { uid: 'restaurant' }] }
7+
const getStats = jest.fn(() => {
8+
return {
9+
databaseSize: 447819776,
10+
lastUpdate: '2019-11-15T11:15:22.092896Z',
11+
indexes: {
12+
my_restaurant: {
13+
numberOfDocuments: 1,
14+
isIndexing: false,
15+
fieldDistribution: {},
16+
},
17+
restaurant: {
18+
numberOfDocuments: 1,
19+
isIndexing: false,
20+
fieldDistribution: {},
21+
},
22+
},
23+
}
924
})
1025

1126
const getTasks = jest.fn(() => {
@@ -18,7 +33,7 @@ const getTasks = jest.fn(() => {
1833
}
1934
})
2035

21-
const getStats = jest.fn(() => {
36+
const getIndexStats = jest.fn(() => {
2237
return { numberOfDocuments: 1, isIndexing: false, fieldDistribution: {} }
2338
})
2439

@@ -27,13 +42,13 @@ const mockIndex = jest.fn(() => ({
2742
updateDocuments: updateDocumentsMock,
2843
updateSettings: updateSettingsMock,
2944
deleteDocuments,
30-
getStats,
45+
getStats: getIndexStats,
3146
}))
3247

3348
// @ts-ignore
3449
const mock = jest.fn().mockImplementation(() => {
3550
return {
36-
getIndexes,
51+
getStats,
3752
index: mockIndex,
3853
getTasks,
3954
}

server/__tests__/meilisearch.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ describe('Tests content types', () => {
2323
strapi: customStrapi,
2424
})
2525

26-
const indexes = await meilisearchService.getIndexes()
26+
const indexes = await meilisearchService.getIndexUids()
2727

28-
expect(indexes).toEqual([{ uid: 'my_restaurant' }, { uid: 'restaurant' }])
28+
expect(indexes).toEqual(['my_restaurant', 'restaurant'])
2929
})
3030

3131
test('Test to delete entries from Meilisearch', async () => {

server/bootstrap.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ async function syncIndexedCollections({
3131
contentTypeService,
3232
meilisearch,
3333
}) {
34-
const indexes = await meilisearch.getIndexes()
35-
const indexUids = indexes.map(index => index.uid)
34+
const indexUids = await meilisearch.getIndexUids()
3635
// All indexed contentTypes
3736
const indexedContentTypes = await store.getIndexedContentTypes()
3837
const contentTypes = contentTypeService.getContentTypesUid()

server/services/meilisearch/connector.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ module.exports = ({ strapi, adapter, config }) => {
6565

6666
return {
6767
/**
68-
* Get indexes with a safe guard in case of error.
68+
* Get index uids with a safe guard in case of error.
6969
*
7070
* @returns { Promise<import("meilisearch").Index[]> }
7171
*/
72-
getIndexes: async function () {
72+
getIndexUids: async function () {
7373
try {
7474
const { apiKey, host } = await store.getCredentials()
7575
const client = Meilisearch({ apiKey, host })
76-
const { results: indexes } = await client.getIndexes()
77-
return indexes
76+
const { indexes } = await client.getStats()
77+
return Object.keys(indexes)
7878
} catch (e) {
7979
strapi.log.error(`meilisearch: ${e.message}`)
8080
return []
@@ -176,8 +176,7 @@ module.exports = ({ strapi, adapter, config }) => {
176176
* }>}>} - List of contentTypes reports.
177177
*/
178178
getContentTypesReport: async function () {
179-
const indexes = await this.getIndexes()
180-
const indexUids = indexes.map(index => index.uid)
179+
const indexUids = await this.getIndexUids()
181180

182181
// All listened contentTypes
183182
const listenedContentTypes = await store.getListenedContentTypes()

0 commit comments

Comments
 (0)