Skip to content

Commit 3b1fe06

Browse files
Merge #620
620: Create a custom _meilisearch_id field r=bidoubiwa a=bidoubiwa #612 When indexing an entry to Meilisearch, the `id` field used to be transformed with a prepend of its collection name. For example in a `restaurant` collection: ```json { "id": 1, "name": "pizza mania" } ``` Would become in Meilisearch: ```json { "id": "restaurant-1", "name": "pizza mania" } ``` With this PR, instead of overwriting the `id` field an additional field is added `_meilisearch_id`. The entry in Meilisearch is thus transformed like this: ```json { "id": 1, "name": "pizza mania", "_meilisearch_id": "restaurant-1" } ``` Co-authored-by: Pablo Aldana <[email protected]>
2 parents 9026ea8 + 73bf38d commit 3b1fe06

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

resources/entries-query/populate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = {
1212

1313
// The following document structure is indexed in Meilisearch
1414
// {
15-
// id: 'restaurant-1',
15+
// id: '1',
16+
// _meilisearch_id: 'restaurant-1',
1617
// title: 'The slimmy snail',
1718
// // ... other restaurant fields
1819
// repeatableComponent: [

server/services/meilisearch/adapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = ({ strapi }) => {
3737
addCollectionNamePrefix: function ({ contentType, entries }) {
3838
return entries.map(entry => ({
3939
...entry,
40-
id: this.addCollectionNamePrefixToId({
40+
_meilisearch_id: this.addCollectionNamePrefixToId({
4141
entryId: entry.id,
4242
contentType,
4343
}),

server/services/meilisearch/connector.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ module.exports = ({ strapi, adapter, config }) => {
130130
})
131131
)
132132
} else {
133-
return client.index(indexUid).updateDocuments(sanitized)
133+
return client
134+
.index(indexUid)
135+
.updateDocuments(sanitized, { primaryKey: '_meilisearch_id' })
134136
}
135137
})
136138
},
@@ -251,7 +253,9 @@ module.exports = ({ strapi, adapter, config }) => {
251253
adapter,
252254
})
253255

254-
const task = await client.index(indexUid).addDocuments(documents)
256+
const task = await client
257+
.index(indexUid)
258+
.addDocuments(documents, { primaryKey: '_meilisearch_id' })
255259
await store.addIndexedContentType({ contentType })
256260

257261
return task
@@ -285,7 +289,9 @@ module.exports = ({ strapi, adapter, config }) => {
285289
})
286290

287291
// Add documents in Meilisearch
288-
const task = await client.index(indexUid).addDocuments(documents)
292+
const task = await client
293+
.index(indexUid)
294+
.addDocuments(documents, { primaryKey: '_meilisearch_id' })
289295

290296
return task.taskUid
291297
}

0 commit comments

Comments
 (0)