Skip to content

Commit 0dc782c

Browse files
authored
fix(plugin-search): exclude skipped drafts in reindex handler (#14224)
<!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # --> ### What? This PR aims to provide better insight into how many documents were reindexed. Currently, the reindex handler will ignore document status and claim all documents were reindexed even when none were due to being drafts (as per plugin-search default behavior). This PR takes into consideration `syncDrafts` enabled in the plugin config and whether the collection of the document being reindexed has `drafts` enabled. In this way, we can "skip" attempting to reindex documents that are drafts when syncDrafts is not true _and_ provide a useful count for the end-user of how many draft docs were skipped during reindexing. ### Why? To provide more insight into how many documents/drafts were reindexed, as well as provide a minor perf improvement when reindexing collections with `syncDrafts: false`. ### How? - By tracking document count in collections with drafts enabled via `totalDocsWithDrafts`. - Filtering the find in `reindexCollection` based on if `drafts` & `syncDrafts` are enabled Before: [Search-Results---Payload--before.webm](https://github.com/user-attachments/assets/bfd7696d-1124-4df5-8f99-d86c282f485c) After: [Search-Results---Payload.webm](https://github.com/user-attachments/assets/7b0d5c84-328e-43e9-b0ae-5bcd0a0e9f07)
1 parent bcb4d8e commit 0dc782c

File tree

46 files changed

+122
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+122
-52
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PayloadHandler } from 'payload'
1+
import type { PayloadHandler, Where } from 'payload'
22

33
import {
44
addLocalesToRequestFromData,
@@ -82,21 +82,28 @@ export const generateReindexHandler =
8282
}
8383

8484
const payload = req.payload
85-
const batchSize = pluginConfig.reindexBatchSize
85+
const { reindexBatchSize: batchSize, syncDrafts } = pluginConfig
8686

8787
const defaultLocalApiProps = {
8888
overrideAccess: false,
8989
req,
9090
user: req.user,
9191
}
92+
const whereStatusPublished: Where = {
93+
_status: {
94+
equals: 'published',
95+
},
96+
}
97+
let aggregateDocsWithDrafts = 0
9298
let aggregateErrors = 0
9399
let aggregateDocs = 0
94100

95-
const countDocuments = async (collection: string): Promise<number> => {
101+
const countDocuments = async (collection: string, drafts?: boolean): Promise<number> => {
96102
const { totalDocs } = await payload.count({
97103
collection,
98104
...defaultLocalApiProps,
99105
req: undefined,
106+
where: drafts ? undefined : whereStatusPublished,
100107
})
101108
return totalDocs
102109
}
@@ -112,8 +119,16 @@ export const generateReindexHandler =
112119
}
113120

114121
const reindexCollection = async (collection: string) => {
115-
const totalDocs = await countDocuments(collection)
122+
const draftsEnabled = Boolean(payload.collections[collection]?.config.versions?.drafts)
123+
124+
const totalDocsWithDrafts = await countDocuments(collection, true)
125+
const totalDocs =
126+
syncDrafts || !draftsEnabled
127+
? totalDocsWithDrafts
128+
: await countDocuments(collection, !draftsEnabled)
116129
const totalBatches = Math.ceil(totalDocs / batchSize)
130+
131+
aggregateDocsWithDrafts += totalDocsWithDrafts
117132
aggregateDocs += totalDocs
118133

119134
for (let j = 0; j < reindexLocales.length; j++) {
@@ -128,6 +143,7 @@ export const generateReindexHandler =
128143
limit: batchSize,
129144
locale: localeToSync,
130145
page: i + 1,
146+
where: syncDrafts || !draftsEnabled ? undefined : whereStatusPublished,
131147
...defaultLocalApiProps,
132148
})
133149

@@ -171,7 +187,8 @@ export const generateReindexHandler =
171187
const message = t('general:successfullyReindexed', {
172188
collections: collections.join(', '),
173189
count: aggregateDocs - aggregateErrors,
174-
total: aggregateDocs,
190+
skips: syncDrafts ? 0 : aggregateDocsWithDrafts - aggregateDocs,
191+
total: aggregateDocsWithDrafts,
175192
})
176193

177194
if (shouldCommit) {

packages/translations/src/languages/ar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ export const arTranslations: DefaultTranslationsObject = {
406406
successfullyCreated: '{{label}} تم إنشاؤها بنجاح.',
407407
successfullyDuplicated: '{{label}} تم استنساخها بنجاح.',
408408
successfullyReindexed:
409-
'تم إعادة فهرسة {{count}} من أصل {{total}} مستندات من {{collections}} مجموعات بنجاح.',
409+
'تمت إعادة فهرسة {{count}} من أصل {{total}} مستند من {{collections}} وتخطي {{skips}} مسودة.',
410410
takeOver: 'تولي',
411411
thisLanguage: 'العربية',
412412
time: 'الوقت',

packages/translations/src/languages/az.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ export const azTranslations: DefaultTranslationsObject = {
418418
successfullyCreated: '{{label}} uğurla yaradıldı.',
419419
successfullyDuplicated: '{{label}} uğurla dublikatlandı.',
420420
successfullyReindexed:
421-
'{{collections}} kolleksiyalarından {{total}} sənəddən {{count}} sənəd uğurla yenidən indeksləndi.',
421+
'"{{collections}}" kolleksiyalarından {{total}} sənəddən {{count}} sənəd uğurla yenidən indeksləndi və {{skips}} qaralama keçildi.',
422422
takeOver: 'Əvvəl',
423423
thisLanguage: 'Azərbaycan dili',
424424
time: 'Vaxt',

packages/translations/src/languages/bg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export const bgTranslations: DefaultTranslationsObject = {
415415
successfullyCreated: '{{label}} успешно създаден.',
416416
successfullyDuplicated: '{{label}} успешно дупликиран.',
417417
successfullyReindexed:
418-
'Успешно преиндексирани {{count}} от {{total}} документа от {{collections}} колекции.',
418+
'Успешно преиндексирани {{count}} от общо {{total}} документа от {{collections}} и пропуснати {{skips}} чернови.',
419419
takeOver: 'Поемане',
420420
thisLanguage: 'Български',
421421
time: 'Време',

packages/translations/src/languages/bnBd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ export const bnBdTranslations: DefaultTranslationsObject = {
421421
successfullyCreated: '{{label}} সফলভাবে তৈরি করা হয়েছে।',
422422
successfullyDuplicated: '{{label}} সফলভাবে ডুপ্লিকেট করা হয়েছে।',
423423
successfullyReindexed:
424-
'{{collections}} থেকে {{total}} ডকুমেন্টের মধ্যে {{count}} টি সফলভাবে পুনরায় সূচিবদ্ধ করা হয়েছে',
424+
'{{collections}} থেকে মোট {{total}}টি ডকুমেন্টের মধ্যে {{count}}টি সফলভাবে পুনরায় ইনডেক্স করা হয়েছে এবং {{skips}}টি খসড়া বাদ দেওয়া হয়েছে।',
425425
takeOver: 'দায়িত্ব নিন',
426426
thisLanguage: 'বাংলা (বাংলাদেশ)',
427427
time: 'সময়',

packages/translations/src/languages/bnIn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export const bnInTranslations: DefaultTranslationsObject = {
420420
successfullyCreated: '{{label}} সফলভাবে তৈরি করা হয়েছে।',
421421
successfullyDuplicated: '{{label}} সফলভাবে ডুপ্লিকেট করা হয়েছে।',
422422
successfullyReindexed:
423-
'{{collections}} থেকে {{total}} ডকুমেন্টের মধ্যে {{count}} টি সফলভাবে পুনরায় সূচিবদ্ধ করা হয়েছে',
423+
'{{collections}} থেকে মোট {{total}}টি নথির মধ্যে {{count}}টি সফলভাবে পুনরায় সূচীকৃত হয়েছে এবং {{skips}}টি খসড়া বাদ দেওয়া হয়েছে',
424424
takeOver: 'দায়িত্ব নিন',
425425
thisLanguage: 'বাংলা (বাংলাদেশ)',
426426
time: 'সময়',

packages/translations/src/languages/ca.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ export const caTranslations: DefaultTranslationsObject = {
418418
successfullyCreated: '{{label}} creada correctament.',
419419
successfullyDuplicated: '{{label}} duplicada correctament.',
420420
successfullyReindexed:
421-
'Reindexació correcta de {{count}} de {{total}} documents de {{collections}}',
421+
"S'han reindexat correctament {{count}} de {{total}} documents de {{collections}} i s'han omès {{skips}} esborranys.",
422422
takeOver: 'Prendre el control',
423423
thisLanguage: 'Catala',
424424
time: 'Temps',

packages/translations/src/languages/cs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export const csTranslations: DefaultTranslationsObject = {
414414
successfullyCreated: '{{label}} úspěšně vytvořeno.',
415415
successfullyDuplicated: '{{label}} úspěšně duplikováno.',
416416
successfullyReindexed:
417-
'Úspěšně přeindexováno {{count}} z {{total}} dokumentů z {{collections}} kolekcí.',
417+
'Úspěšně reindexováno {{count}} z {{total}} dokumentů z {{collections}} a přeskočeno {{skips}} konceptů.',
418418
takeOver: 'Převzít',
419419
thisLanguage: 'Čeština',
420420
time: 'Čas',

packages/translations/src/languages/da.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export const daTranslations: DefaultTranslationsObject = {
415415
successfullyCreated: '{{label}} oprettet.',
416416
successfullyDuplicated: '{{label}} duplikeret.',
417417
successfullyReindexed:
418-
'{{count}} ud af {{total}} dokumenter fra {{collections}} samlinger blev genindekseret med succes.',
418+
'{{count}} ud af {{total}} dokumenter fra {{collections}} blev succesfuldt genindekseret, og {{skips}} kladder blev sprunget over.',
419419
takeOver: 'Overtag',
420420
thisLanguage: 'Dansk',
421421
time: 'Tid',

packages/translations/src/languages/de.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ export const deTranslations: DefaultTranslationsObject = {
426426
successfullyCreated: '{{label}} erfolgreich erstellt.',
427427
successfullyDuplicated: '{{label}} wurde erfolgreich dupliziert.',
428428
successfullyReindexed:
429-
'Erfolgreich {{count}} von {{total}} Dokumenten aus {{collections}} Sammlungen neu indiziert.',
429+
'{{count}} von insgesamt {{total}} Dokumenten aus {{collections}} wurden erfolgreich neu indexiert, {{skips}} Entwürfe wurden übersprungen.',
430430
takeOver: 'Übernehmen',
431431
thisLanguage: 'Deutsch',
432432
time: 'Zeit',

0 commit comments

Comments
 (0)