fix: search plugin localized fields (#7292)

This commit is contained in:
Jarrod Flesch
2024-07-22 15:47:39 -04:00
committed by GitHub
parent 45844789f2
commit 5aa3283dc0
6 changed files with 78 additions and 8 deletions

View File

@@ -23,8 +23,17 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
}
if (typeof beforeSync === 'function') {
let docToSyncWith = doc
if (payload.config?.localization) {
docToSyncWith = await payload.findByID({
id,
collection,
locale: 'all',
req,
})
}
dataToSave = await beforeSync({
originalDoc: doc,
originalDoc: docToSyncWith,
payload,
req,
searchDoc: dataToSave,

View File

@@ -13,10 +13,10 @@ export const searchPlugin =
if (collections) {
const pluginConfig: SearchPluginConfig = {
...incomingPluginConfig,
// write any config defaults here
deleteDrafts: true,
syncDrafts: false,
// write any config defaults here
...incomingPluginConfig,
}
// add afterChange and afterDelete hooks to every search-enabled collection

View File

@@ -26,5 +26,10 @@ export const Posts: CollectionConfig = {
label: 'Excerpt',
type: 'text',
},
{
type: 'text',
name: 'slug',
localized: true,
},
],
}

View File

@@ -31,10 +31,13 @@ export default buildConfigWithDefaults({
},
plugins: [
searchPlugin({
beforeSync: ({ originalDoc, searchDoc }) => ({
beforeSync: ({ originalDoc, searchDoc }) => {
return {
...searchDoc,
excerpt: originalDoc?.excerpt || 'This is a fallback excerpt',
}),
slug: originalDoc.slug,
}
},
collections: ['pages', 'posts'],
defaultPriorities: {
pages: 10,
@@ -50,6 +53,12 @@ export default buildConfigWithDefaults({
position: 'sidebar',
},
},
{
name: 'slug',
required: false,
type: 'text',
localized: true,
},
],
},
}),

View File

@@ -184,4 +184,43 @@ describe('@payloadcms/plugin-search', () => {
expect(deletedResults).toHaveLength(0)
})
it('should sync localized data', async () => {
const createdDoc = await payload.create({
collection: 'posts',
data: {
_status: 'draft',
title: 'test title',
slug: 'es',
},
locale: 'es',
})
await payload.update({
collection: 'posts',
id: createdDoc.id,
data: {
_status: 'published',
title: 'test title',
slug: 'en',
},
locale: 'en',
})
const syncedSearchData = await payload.find({
collection: 'search',
locale: 'es',
where: {
and: [
{
'doc.value': {
equals: createdDoc.id,
},
},
],
},
})
expect(syncedSearchData.docs[0].slug).toEqual('es')
})
})

View File

@@ -18,6 +18,9 @@ export interface Config {
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
db: {
defaultIDType: string;
};
globals: {};
locale: 'en' | 'es' | 'de';
user: User & {
@@ -29,13 +32,16 @@ export interface UserAuthOperations {
email: string;
};
login: {
password: string;
email: string;
password: string;
};
registerFirstUser: {
email: string;
password: string;
};
unlock: {
email: string;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
@@ -74,6 +80,7 @@ export interface Post {
id: string;
title: string;
excerpt?: string | null;
slug?: string | null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
@@ -96,6 +103,7 @@ export interface Search {
value: string | Post;
};
excerpt?: string | null;
slug?: string | null;
updatedAt: string;
createdAt: string;
}