i have issue of vector search when i query the product with price it bring me the exact record it it search the relevant products my code is
async function getEmbedding(query) {
const url = config.openai.openaiUrl;
const openaiKey = config.openai.openaiKey;
try {
let embeding =await productUtil({
input: query,
model: "text-embedding-3-small"
},{url:url,method:"POST",token:openaiKey})
if (!embeding && !embeding.data && !embeding.data[0] && !embeding.data[0].embedding) {
throw new Error('vector not created against your query.');
}
return embeding.data[0].embedding;
} catch (error) {
throw new Error(`Error getting embedding: ${error.message}`);
}
}
async function findSimilarDocuments(embedding,limit) {
let queryLimit=limit?limit:semanticSearch.limit
try {
const documents = await Product.aggregate([
{
"$vectorSearch": {
"queryVector": embedding,
"path": "embedding",
"numCandidates": 100,
"limit": queryLimit,
"index": semanticSearch.index,
}
},
{
$addFields:{
'score': {
'$meta': 'vectorSearchScore'
},
}
},
{
$project:{
"embedding":0
}
},
{
'$match': {
'score': { '$gte': semanticSearch.similarity }
}
}
])
console.log(documents);
return documents;
} catch (error) {
throw new Error(httpStatus.BAD_REQUEST, `${error.message}`);
}
}
const vectorSearch = async (query)=>{
try {
const embedding = await getEmbedding(query.query);
const documents = await findSimilarDocuments(embedding,query.limit);
return documents
} catch (error) {
console.error('Error:', error.message);
throw new ApiError(httpStatus.BAD_REQUEST,error.message)
}
}
const allProductsEmbedd = async (options) => {
try {
let pageNumber = parseInt(options.page);
let limit = parseInt(options.limit);
let products = await Product.find({ productEbedding: { $exists: false },active:true, price: { $exists: true }})
.skip((pageNumber - 1) * limit)
.limit(limit)
let updateOperations=[]
if(products && products.length)
updateOperations = await Promise.all(products.map(async product => {
let combinedText = "";
if (product.productName) combinedText += `${product.productName}. `;
if (product.price) combinedText += `price: ${product.price}. `;
if (product.description) combinedText += `${product.description}. `;
if ( product.selectedAttributes && product.selectedAttributes.length > 0) {
product.selectedAttributes.forEach(attribute => {
combinedText += `${attribute.name}: ${attribute.value}, `;
});
}
if (combinedText.length === 0) {
console.log("No combined text to process. Skipping embedding and update.");
return;
}
let embedding = await getEmbedding(combinedText);
return {
updateOne: {
filter: { _id: product._id },
update: { $set: { embedding: embedding } }
}
};
}));
let result = await Product.bulkWrite(updateOperations);
console.log('All products embedded and saved');
return result
} catch (error) {
throw new ApiError(httpStatus.BAD_REQUEST,error.message)
}
};
and search index is
{
"fields": [
{
"numDimensions": 1536,
"path": "embedding",
"similarity": "cosine",
"type": "vector"
},
{
"path": "embedding",
"type": "filter"
}
]
} like for example if i search product with price 100 it also bring product with price 1000