perf(plugin-search): reduce query depth in hooks (#12225)
Perf improvements and reliability of document reindexing and synchronization of plugin-search functions. ## What Reindex Handler (generateReindexHandler.ts): - Replaced `Promise.all` with sequential `await` to prevent transaction issues. - Added `depth: 0` to payload.find for lighter queries. Sync Operations (syncDocAsSearchIndex.ts): - Standardized depth: 0 across create, delete, update, and find API calls. - Streamlined conditionals for create operations. ## Why Improved performance with reduced query overhead. Enhanced transaction safety by avoiding parallel database operations.
This commit is contained in:
@@ -124,14 +124,15 @@ export const generateReindexHandler =
|
|||||||
for (let i = 0; i < totalBatches; i++) {
|
for (let i = 0; i < totalBatches; i++) {
|
||||||
const { docs } = await payload.find({
|
const { docs } = await payload.find({
|
||||||
collection,
|
collection,
|
||||||
|
depth: 0,
|
||||||
limit: batchSize,
|
limit: batchSize,
|
||||||
locale: localeToSync,
|
locale: localeToSync,
|
||||||
page: i + 1,
|
page: i + 1,
|
||||||
...defaultLocalApiProps,
|
...defaultLocalApiProps,
|
||||||
})
|
})
|
||||||
|
|
||||||
const promises = docs.map((doc) =>
|
for (const doc of docs) {
|
||||||
syncDocAsSearchIndex({
|
await syncDocAsSearchIndex({
|
||||||
collection,
|
collection,
|
||||||
doc,
|
doc,
|
||||||
locale: localeToSync,
|
locale: localeToSync,
|
||||||
@@ -139,12 +140,7 @@ export const generateReindexHandler =
|
|||||||
operation,
|
operation,
|
||||||
pluginConfig,
|
pluginConfig,
|
||||||
req,
|
req,
|
||||||
}),
|
})
|
||||||
)
|
|
||||||
|
|
||||||
// Sequentially await promises to avoid transaction issues
|
|
||||||
for (const promise of promises) {
|
|
||||||
await promise
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,18 +64,17 @@ export const syncDocAsSearchIndex = async ({
|
|||||||
const doSync = syncDrafts || (!syncDrafts && status !== 'draft')
|
const doSync = syncDrafts || (!syncDrafts && status !== 'draft')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (operation === 'create') {
|
if (operation === 'create' && doSync) {
|
||||||
if (doSync) {
|
await payload.create({
|
||||||
await payload.create({
|
collection: searchSlug,
|
||||||
collection: searchSlug,
|
data: {
|
||||||
data: {
|
...dataToSave,
|
||||||
...dataToSave,
|
priority: defaultPriority,
|
||||||
priority: defaultPriority,
|
},
|
||||||
},
|
depth: 0,
|
||||||
locale: syncLocale,
|
locale: syncLocale,
|
||||||
req,
|
req,
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operation === 'update') {
|
if (operation === 'update') {
|
||||||
@@ -110,6 +109,7 @@ export const syncDocAsSearchIndex = async ({
|
|||||||
const duplicativeDocIDs = duplicativeDocs.map(({ id }) => id)
|
const duplicativeDocIDs = duplicativeDocs.map(({ id }) => id)
|
||||||
await payload.delete({
|
await payload.delete({
|
||||||
collection: searchSlug,
|
collection: searchSlug,
|
||||||
|
depth: 0,
|
||||||
req,
|
req,
|
||||||
where: { id: { in: duplicativeDocIDs } },
|
where: { id: { in: duplicativeDocIDs } },
|
||||||
})
|
})
|
||||||
@@ -134,6 +134,7 @@ export const syncDocAsSearchIndex = async ({
|
|||||||
...dataToSave,
|
...dataToSave,
|
||||||
priority: foundDoc.priority || defaultPriority,
|
priority: foundDoc.priority || defaultPriority,
|
||||||
},
|
},
|
||||||
|
depth: 0,
|
||||||
locale: syncLocale,
|
locale: syncLocale,
|
||||||
req,
|
req,
|
||||||
})
|
})
|
||||||
@@ -148,6 +149,7 @@ export const syncDocAsSearchIndex = async ({
|
|||||||
docs: [docWithPublish],
|
docs: [docWithPublish],
|
||||||
} = await payload.find({
|
} = await payload.find({
|
||||||
collection,
|
collection,
|
||||||
|
depth: 0,
|
||||||
draft: false,
|
draft: false,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
locale: syncLocale,
|
locale: syncLocale,
|
||||||
@@ -175,6 +177,7 @@ export const syncDocAsSearchIndex = async ({
|
|||||||
await payload.delete({
|
await payload.delete({
|
||||||
id: searchDocID,
|
id: searchDocID,
|
||||||
collection: searchSlug,
|
collection: searchSlug,
|
||||||
|
depth: 0,
|
||||||
req,
|
req,
|
||||||
})
|
})
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
@@ -190,6 +193,7 @@ export const syncDocAsSearchIndex = async ({
|
|||||||
...dataToSave,
|
...dataToSave,
|
||||||
priority: defaultPriority,
|
priority: defaultPriority,
|
||||||
},
|
},
|
||||||
|
depth: 0,
|
||||||
locale: syncLocale,
|
locale: syncLocale,
|
||||||
req,
|
req,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user