-
Hi, I am building a service where I follow along with traditional use cases of PGVector including population of the database using Example: embeddings = OpenAIEmbeddings()
vector_store = PGVector(
embeddings=embeddings,
collection_name=PG_COLLECTION,
connection=PG_CONN_STR,
)
vector_store.add_documents(
documents=[
Document(page_content="Content of File A - Part 1", metadata={"id": "file_a"}),
Document(page_content="Content of File A - Part 2", metadata={"id": "file_a"}),
Document(page_content="Content of File B - Part 1", metadata={"id": "file_b"}),
Document(page_content="Content of File B - Part 2", metadata={"id": "file_b"}),
]
)
# Now I want to retrieve all of the documents that have id="file_b" in their metadata
# However, all of the documented retrieval functions require a query
vector_store.as_retriever(search_kwargs={"filter": {"id": "file_b"}}).invoke("") Is there a way to perform retrieval with PGVecttor using just a metadata query? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hey @sunnstix! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this together! Yes, you can perform retrieval with PGVector using just a metadata query without using vector search. The vectorstore supports a variety of filters that can be applied against the metadata fields of the documents. These filters include operators for equality, inequality, comparison, and logical operations, such as For example, you can use a metadata filter to retrieve documents based on specific criteria without relying on vector similarity: results = vector_store.similarity_search(
"", k=10, filter={"id": {"$eq": "file_b"}}
)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]") In this example, the To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
Beta Was this translation helpful? Give feedback.
-
@dosu Your answer about using an empty string was incorrect. Are there any other ways to retrieves all documents matching a metadata filter using PGVector? |
Beta Was this translation helpful? Give feedback.
To retrieve all documents matching a metadata filter using PGVector without providing an empty string for the query parameter, you can use the
similarity_search_with_score
method with a filter. This method allows you to specify a filter directly without needing to provide a query string. Here's an example: