Replies: 2 comments
-
Hey @sunwupark! I'm here to help you with bugs, questions, and becoming a contributor. Let's tackle this together! 🤖 To search with metadata in Milvus using results = vector_store.similarity_search(
"LangChain provides abstractions to make working with LLMs easy",
k=2,
filter={"source": "tweet"},
)
for res in results:
print(f"* {res.page_content} [{res.metadata}]") This code snippet performs a similarity search for the query "LangChain provides abstractions to make working with LLMs easy" and filters the results to only include documents where the metadata Additionally, if you are using the from langchain_milvus.retrievers.milvus_hybrid_search import MilvusCollectionHybridSearchRetriever
from pymilvus import Collection
# Initialize the Milvus collection
collection = Collection("your_collection_name")
# Define the retriever
retriever = MilvusCollectionHybridSearchRetriever(
collection=collection,
rerank=None, # Replace with your ranker if needed
anns_fields=["vector_field_1", "vector_field_2"],
field_embeddings=[embedding_function_1, embedding_function_2],
field_search_params=[{"metric_type": "L2", "params": {"nprobe": 10}}],
field_limits=[10, 10],
field_exprs=["source == 'tweet'", None],
top_k=2,
text_field="text",
output_fields=["source", "author"]
)
# Perform the search
results = retriever._get_relevant_documents("LangChain provides abstractions to make working with LLMs easy")
for res in results:
print(f"* {res.page_content} [{res.metadata}]") This example demonstrates how to set up a hybrid search retriever with multiple vector fields and filter the results based on metadata using boolean expressions. For more detailed examples and tests, you can refer to the integration tests provided in the def test_milvus_with_metadata() -> None:
"""Test with metadata"""
docsearch = _milvus_from_texts(metadatas=[{"label": "test"}] * len(fake_texts))
output = docsearch.similarity_search("foo", k=1)
assert_docs_equal_without_pk(
output, [Document(page_content="foo", metadata={"label": "test"})]
) This test function demonstrates how to set up a Milvus instance with metadata and perform a similarity search, ensuring that the metadata is correctly associated with the search results [1][2][3]. |
Beta Was this translation helpful? Give feedback.
-
Have you solved this problem? |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I’m trying to retrieve all documents from Milvus using
langchain_milvus
by querying with metadata,such as
id >= 1 AND id < 50
.However, it seems there’s no straightforward way to perform metadata-based searches directly within
langchain_milvus
.The only option I’ve found so far is leveraging the
query
method inpymilvus
, which allows metadata querying.I’d like to know if there’s a way to achieve this in
langchain_milvus
, or if there are any plans to introduce this functionality in the future.System Info
langchain==0.2.16
langchain-community==0.2.16
langchain-core==0.2.38
langchain-milvus==0.1.4
langchain-openai==0.1.23
langchain-text-splitters==0.2.4
platform: mac
python version: Python 3.9.18
Beta Was this translation helpful? Give feedback.
All reactions