Skip to content

perf(plugin-search): reduce query depth in hooks #12225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions packages/plugin-search/src/utilities/generateReindexHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,23 @@ export const generateReindexHandler =
for (let i = 0; i < totalBatches; i++) {
const { docs } = await payload.find({
collection,
depth: 0,
limit: batchSize,
locale: localeToSync,
page: i + 1,
...defaultLocalApiProps,
})

const promises = docs.map((doc) =>
syncDocAsSearchIndex({
for (const doc of docs) {
await syncDocAsSearchIndex({
collection,
doc,
locale: localeToSync,
onSyncError: () => operation === 'create' && aggregateErrors++,
operation,
pluginConfig,
req,
}),
)

// Sequentially await promises to avoid transaction issues
for (const promise of promises) {
await promise
})
}
}
}
Expand Down
28 changes: 16 additions & 12 deletions packages/plugin-search/src/utilities/syncDocAsSearchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,17 @@ export const syncDocAsSearchIndex = async ({
const doSync = syncDrafts || (!syncDrafts && status !== 'draft')

try {
if (operation === 'create') {
if (doSync) {
await payload.create({
collection: searchSlug,
data: {
...dataToSave,
priority: defaultPriority,
},
locale: syncLocale,
req,
})
}
if (operation === 'create' && doSync) {
await payload.create({
collection: searchSlug,
data: {
...dataToSave,
priority: defaultPriority,
},
depth: 0,
locale: syncLocale,
req,
})
}

if (operation === 'update') {
Expand Down Expand Up @@ -110,6 +109,7 @@ export const syncDocAsSearchIndex = async ({
const duplicativeDocIDs = duplicativeDocs.map(({ id }) => id)
await payload.delete({
collection: searchSlug,
depth: 0,
req,
where: { id: { in: duplicativeDocIDs } },
})
Expand All @@ -134,6 +134,7 @@ export const syncDocAsSearchIndex = async ({
...dataToSave,
priority: foundDoc.priority || defaultPriority,
},
depth: 0,
locale: syncLocale,
req,
})
Expand All @@ -148,6 +149,7 @@ export const syncDocAsSearchIndex = async ({
docs: [docWithPublish],
} = await payload.find({
collection,
depth: 0,
draft: false,
limit: 1,
locale: syncLocale,
Expand Down Expand Up @@ -175,6 +177,7 @@ export const syncDocAsSearchIndex = async ({
await payload.delete({
id: searchDocID,
collection: searchSlug,
depth: 0,
req,
})
} catch (err: unknown) {
Expand All @@ -190,6 +193,7 @@ export const syncDocAsSearchIndex = async ({
...dataToSave,
priority: defaultPriority,
},
depth: 0,
locale: syncLocale,
req,
})
Expand Down
Loading