# Create the vector search index with specific filters for optimized metadata searchingvector_store.create_vector_search_index( dimensions=1024, wait_until_complete=45 # waits 45 seconds for index readiness)
# Create the vector search index with specific filters for optimized metadata searchingvector_store.create_vector_search_index( dimensions=1024, wait_until_complete=60, update=True, filters=["source"])
使用元数据过滤器缩小结果范围。请注意,Atlas 向量搜索需要显式运算符如 $eq。
# Use a source known to exist in our loaded docssample_source = "https://en.wikipedia.org/wiki/MongoDB"filtered_results = vector_store.similarity_search( query, k=3, pre_filter={"source": {"$eq": sample_source}})print(f"Retrieved {len(filtered_results)} documents matching source: {sample_source}")
from langchain_mongodb.index import create_fulltext_search_index# Use helper method to create the search indexcreate_fulltext_search_index( collection = atlas_collection, wait_until_complete=60, field = "text", index_name = "search_index")
# Requires a standard Search Index named 'default' on the collectionretriever = MongoDBAtlasHybridSearchRetriever( vectorstore=vector_store, search_index_name="search_index", fulltext_penalty = 50, vector_penalty = 50, top_k=5)hybrid_docs = retriever.invoke("database-as-a-service")for doc in hybrid_docs: print("Title: " + doc.metadata["title"]) print("Plot: " + doc.page_content) print("Search score: {}".format(doc.metadata["fulltext_score"])) print("Vector Search score: {}".format(doc.metadata["vector_score"])) print("Total score: {}\n".format(doc.metadata["fulltext_score"] + doc.metadata["vector_score"]))