vector search for ecomerce

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

Hi @Umar_Farooq4! Thank you for the question.

If I understand correctly it looks like you want to be using the filter field within $vectorSearch to only consider items within a certain price range. There is an example of how you might do this available in our docs here.

Hope this helps!

1 Like

Not trying to be “That guy,” but variables need to match in your code.

You have “embedding” “embeding” and “ebedding” and other spelling errors throughout. Once a lot of this is fixed, and checking other spellings throughout your code it’ll be a lot easier to debug after that.