fix!: plugin-search with localization enabled (#8938)

The search plugin was incorrectly retrieving all locales, when it should
just be retrieving the locale of the parent document that was actively
being updated.

## BREAKING CHANGES:

If you have a localized Payload config, and you are using the `plugin-search`, we will now automatically localize the `title` field that is injected by the search plugin and this may lead to data loss. To opt out of this new behavior, you can pass `localize: false` to the plugin options.
This commit is contained in:
James Mikrut
2024-10-30 11:49:54 -04:00
committed by GitHub
parent 04bd502d37
commit 123125185c
6 changed files with 20 additions and 2 deletions

View File

@@ -81,6 +81,10 @@ export default config
The `collections` property is an array of collection slugs to enable syncing to search. Enabled collections receive a `beforeChange` and `afterDelete` hook that creates, updates, and deletes its respective search record as it changes over time.
### `localize`
By default, the search plugin will add `localization: true` to the `title` field of the newly added `search` collection if you have localization enabled. If you would like to disable this behavior, you can set this to `false`.
#### `defaultPriorities`
This plugin automatically adds a `priority` field to the `search` collection that can be used as the `?sort=` parameter in your queries. For example, you may want to list blog posts before pages. Or you may want one specific post to always take appear first.

View File

@@ -30,7 +30,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
docToSyncWith = await payload.findByID({
id,
collection,
locale: 'all',
locale: req.locale,
req,
})
}
@@ -71,6 +71,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
...dataToSave,
priority: defaultPriority,
},
locale: req.locale,
req,
})
}
@@ -82,6 +83,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
const searchDocQuery = await payload.find({
collection: searchSlug,
depth: 0,
locale: req.locale,
req,
where: {
'doc.relationTo': {
@@ -128,6 +130,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
...dataToSave,
priority: foundDoc.priority || defaultPriority,
},
locale: req.locale,
req,
})
} catch (err: unknown) {
@@ -154,6 +157,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
...dataToSave,
priority: defaultPriority,
},
locale: req.locale,
req,
})
} catch (err: unknown) {

View File

@@ -11,6 +11,7 @@ export const generateSearchCollection = (pluginConfig: SearchPluginConfig): Coll
admin: {
readOnly: true,
},
localized: pluginConfig.localize,
},
{
name: 'priority',

View File

@@ -14,6 +14,14 @@ export const searchPlugin =
(config: Config): Config => {
const { collections } = config
// If the user defines `localize` to either true or false, use that
// Otherwise, set it based on if their config has localization enabled or disabled
const shouldLocalize =
typeof incomingPluginConfig.localize === 'boolean'
? incomingPluginConfig.localize
: Boolean(config.localization)
incomingPluginConfig.localize = shouldLocalize
if (collections) {
const pluginConfig: SearchPluginConfig = {
// write any config defaults here

View File

@@ -34,6 +34,7 @@ export type SearchPluginConfig = {
[collection: string]: ((doc: any) => number | Promise<number>) | number
}
deleteDrafts?: boolean
localize?: boolean
searchOverrides?: { fields?: FieldsOverride } & Partial<Omit<CollectionConfig, 'fields'>>
syncDrafts?: boolean
}

View File

@@ -193,7 +193,7 @@ describe('@payloadcms/plugin-search', () => {
const createdDoc = await payload.create({
collection: 'posts',
data: {
_status: 'draft',
_status: 'published',
title: 'test title',
slug: 'es',
},