Skip to content

Commit caae598

Browse files
authored
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.
1 parent 2f21d46 commit caae598

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

packages/plugin-search/src/utilities/generateReindexHandler.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,23 @@ export const generateReindexHandler =
124124
for (let i = 0; i < totalBatches; i++) {
125125
const { docs } = await payload.find({
126126
collection,
127+
depth: 0,
127128
limit: batchSize,
128129
locale: localeToSync,
129130
page: i + 1,
130131
...defaultLocalApiProps,
131132
})
132133

133-
const promises = docs.map((doc) =>
134-
syncDocAsSearchIndex({
134+
for (const doc of docs) {
135+
await syncDocAsSearchIndex({
135136
collection,
136137
doc,
137138
locale: localeToSync,
138139
onSyncError: () => operation === 'create' && aggregateErrors++,
139140
operation,
140141
pluginConfig,
141142
req,
142-
}),
143-
)
144-
145-
// Sequentially await promises to avoid transaction issues
146-
for (const promise of promises) {
147-
await promise
143+
})
148144
}
149145
}
150146
}

packages/plugin-search/src/utilities/syncDocAsSearchIndex.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,17 @@ export const syncDocAsSearchIndex = async ({
6464
const doSync = syncDrafts || (!syncDrafts && status !== 'draft')
6565

6666
try {
67-
if (operation === 'create') {
68-
if (doSync) {
69-
await payload.create({
70-
collection: searchSlug,
71-
data: {
72-
...dataToSave,
73-
priority: defaultPriority,
74-
},
75-
locale: syncLocale,
76-
req,
77-
})
78-
}
67+
if (operation === 'create' && doSync) {
68+
await payload.create({
69+
collection: searchSlug,
70+
data: {
71+
...dataToSave,
72+
priority: defaultPriority,
73+
},
74+
depth: 0,
75+
locale: syncLocale,
76+
req,
77+
})
7978
}
8079

8180
if (operation === 'update') {
@@ -110,6 +109,7 @@ export const syncDocAsSearchIndex = async ({
110109
const duplicativeDocIDs = duplicativeDocs.map(({ id }) => id)
111110
await payload.delete({
112111
collection: searchSlug,
112+
depth: 0,
113113
req,
114114
where: { id: { in: duplicativeDocIDs } },
115115
})
@@ -134,6 +134,7 @@ export const syncDocAsSearchIndex = async ({
134134
...dataToSave,
135135
priority: foundDoc.priority || defaultPriority,
136136
},
137+
depth: 0,
137138
locale: syncLocale,
138139
req,
139140
})
@@ -148,6 +149,7 @@ export const syncDocAsSearchIndex = async ({
148149
docs: [docWithPublish],
149150
} = await payload.find({
150151
collection,
152+
depth: 0,
151153
draft: false,
152154
limit: 1,
153155
locale: syncLocale,
@@ -175,6 +177,7 @@ export const syncDocAsSearchIndex = async ({
175177
await payload.delete({
176178
id: searchDocID,
177179
collection: searchSlug,
180+
depth: 0,
178181
req,
179182
})
180183
} catch (err: unknown) {
@@ -190,6 +193,7 @@ export const syncDocAsSearchIndex = async ({
190193
...dataToSave,
191194
priority: defaultPriority,
192195
},
196+
depth: 0,
193197
locale: syncLocale,
194198
req,
195199
})

0 commit comments

Comments
 (0)