Hi guys, I am using MongoDB atlas and I am facing the issue of filtering the documents before doing vector search. I made two codes but none of them worked.
from pymongo import MongoClient
from langchain.vectorstores.mongodb_atlas import MongoDBAtlasVectorSearch
from langchain.embeddings import OpenAIEmbeddings
client_db = MongoClient(CONNECTION_STRING)
db = client_db[“main”]
collection = db[“data”]
embedding_model = OpenAIEmbeddings(api_key=openai_key)
vector_search = MongoDBAtlasVectorSearch(
collection,
embedding_model,
index_name=“vector_index”,
text_key=“description_embedding”,
relevance_score_fn=“cosine”
)
query = “Show me some properties for sale at Accra, Ghana with 4 beds”
Get relevant documents using vector search
search_results = vector_search.similarity_search(query, k=5, pre_filter={ “$and”: [{ “price”: { “$lt”: 20000 }}]})
I am getting error:
OperationFailure: PlanExecutor error during aggregation :: caused by :: Path ‘price’ needs to be indexed as number, full error: {‘ok’: 0.0, ‘errmsg’: “PlanExecutor error during aggregation :: caused by :: Path ‘price’ needs to be indexed as number”, ‘code’: 8, ‘codeName’: ‘UnknownError’, ‘$clusterTime’: {‘clusterTime’: Timestamp(1712542957, 1), ‘signature’: {‘hash’: b’\xbfb\xbb\xba\x84!\xa1\x05@\x8fXF\xe5\x82X_\x0ea\x88\xab’, ‘keyId’: 7349586743160471558}}, ‘operationTime’: Timestamp(1712542957, 1)}
Other code block:
import pymongo
import datetime
query = “Show me some properties for sale at Accra, Ghana with 4 beds”
client_embedding = OpenAIEmbeddings(api_key=openai_key)
query_embedding = client_embedding.embed_query(query)
connect to your Atlas cluster
client_db = MongoClient(CONNECTION_STRING)
db = client_db[“rehaniAI”]
collection = db[“data”]
define pipeline
pipeline=[{
“$vectorSearch”: {
“index”: “vector_index”,
“path”: “embedding”,
“filter”: {
“$and”: [
{
“price”: {
“$gt”: 0
},
},
{
“price”: {
“$lt”: 20000
}
}
]
},
“queryVector”: query_embedding,
“numCandidates”: 150,
“limit”: 10,
}
}]
run pipeline
result = collection.aggregate(pipeline)
print results
for i in result:
print(I)
still the same error
OperationFailure: PlanExecutor error during aggregation :: caused by :: Path ‘price’ needs to be indexed as number, full error: {‘ok’: 0.0, ‘errmsg’: “PlanExecutor error during aggregation :: caused by :: Path ‘price’ needs to be indexed as number”, ‘code’: 8, ‘codeName’: ‘UnknownError’, ‘$clusterTime’: {‘clusterTime’: Timestamp(1712542705, 1), ‘signature’: {‘hash’: b’\x15\xd5\xa8\x1d\x9d\x1f\x97\xe8\xc5\xef\xc2\xc7\xb9"\x11y\x89`\xd6K’, ‘keyId’: 7349586743160471558}}, ‘operationTime’: Timestamp(1712542705, 1)
I created the index named price for the price attribute as ascending order still same issue. Any help would be much appreciated.
Thanks
Best Regards,
Owais Ahmed

