feat: ai search
This commit is contained in:
@@ -37,6 +37,51 @@ export const Products: CollectionConfig = {
|
||||
};
|
||||
},
|
||||
},
|
||||
hooks: {
|
||||
afterChange: [
|
||||
async ({ doc, req, operation }) => {
|
||||
// Run index sync asynchronously to not block the CMS save operation
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
const { upsertProductVector, deleteProductVector } = await import('../../lib/qdrant');
|
||||
|
||||
// Check if product is published
|
||||
if (doc._status !== 'published') {
|
||||
await deleteProductVector(doc.id);
|
||||
req.payload.logger.info(`Removed drafted product ${doc.sku} from Qdrant`);
|
||||
} else {
|
||||
// Serialize payload
|
||||
const contentText = `${doc.title} - SKU: ${doc.sku}\n${doc.description || ''}`;
|
||||
const payload = {
|
||||
id: doc.id,
|
||||
title: doc.title,
|
||||
sku: doc.sku,
|
||||
slug: doc.slug,
|
||||
description: doc.description,
|
||||
featuredImage: doc.featuredImage, // usually just ID or URL depending on depth
|
||||
};
|
||||
await upsertProductVector(doc.id, contentText, payload);
|
||||
req.payload.logger.info(`Upserted product ${doc.sku} to Qdrant`);
|
||||
}
|
||||
} catch (error) {
|
||||
req.payload.logger.error({ msg: 'Error syncing product to Qdrant', err: error, productId: doc.id });
|
||||
}
|
||||
}, 0);
|
||||
return doc;
|
||||
},
|
||||
],
|
||||
afterDelete: [
|
||||
async ({ id, req }) => {
|
||||
try {
|
||||
const { deleteProductVector } = await import('../../lib/qdrant');
|
||||
await deleteProductVector(id as string | number);
|
||||
req.payload.logger.info(`Deleted product ${id} from Qdrant`);
|
||||
} catch (error) {
|
||||
req.payload.logger.error({ msg: 'Error deleting product from Qdrant', err: error, productId: id });
|
||||
}
|
||||
},
|
||||
],
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
|
||||
Reference in New Issue
Block a user