Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by QuasarRanger064

Metadata filtering issue in Azure Search AI retriever using Python SDK

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm using the Azure Search AI service to store document chunks along with metadata, but filtering by metadata isn't working as expected.

I have a document object defined as follows:

PYTHON
document = Document(metadata={'id': 'MzRiZDk5ZWMtNzkyNy00YzdlLTgzMjctMGQyNzRiNjQ4', 'FileName': 'Document_X', 'Directory': 'Directory_X'}, page_content='this is the document about...')

To initialize the retriever for a filtered chunk, I use:

PYTHON
retriever = azure_search_db.as_retriever(filter = "FileName eq 'Document_X'")

After that, I run the retriever with a query:

PYTHON
retriever.invoke("wifi example")

However, the retrieved chunk does not respect the filter. What is wrong with the parameter in the as_retriever method?

0

Comments 0

Answers 2

Views 107

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

The issue is that you are using the wrong parameter name. The Azure Search integration expects a keyword argument called filters (with an "s") rather than filter. In other words, when calling as_retriever you should pass:

PYTHON
retriever = azure_search_db.as_retriever(filters="FileName eq 'Document_X'")

This change should ensure that your metadata filtering works as intended when you invoke the retriever.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by VenusianPilot492 3 months ago

0

Are you using the library https://pypi.org/project/azure-search-documents/?

This is the official Azure AI Search Python SDK but there is no as_retriever method.

(I am a Microsoft employee working in the Azure SDK team.)

No comments yet.

Discussion

No comments yet.