Skip to content

The agent engineering platform.

community[patch]:Support filter for AzureAISearchRetriever (#22303)

Jeffrey Mak5fc5ed463cfc
diff --git a/libs/community/langchain_community/retrievers/azure_ai_search.py b/libs/community/langchain_community/retrievers/azure_ai_search.py
index 5e4c0594bb..c5146f8170 100644
--- a/libs/community/langchain_community/retrievers/azure_ai_search.py
+++ b/libs/community/langchain_community/retrievers/azure_ai_search.py
@@ -36,6 +36,8 @@ class AzureAISearchRetriever(BaseRetriever):
"""Key in a retrieved result to set as the Document page_content."""
top_k: Optional[int] = None
"""Number of results to retrieve. Set to None to retrieve all results."""
+ filter: Optional[str] = None
+ """OData $filter expression to apply to the search query."""
class Config:
extra = Extra.forbid
@@ -72,7 +74,8 @@ class AzureAISearchRetriever(BaseRetriever):
base_url = self.service_name
endpoint_path = f"indexes/{self.index_name}/docs?api-version={self.api_version}"
top_param = f"&$top={self.top_k}" if self.top_k else ""
- return base_url + endpoint_path + f"&search={query}" + top_param
+ filter_param = f"&$filter={self.filter}" if self.filter else ""
+ return base_url + endpoint_path + f"&search={query}" + top_param + filter_param
@property
def _headers(self) -> Dict[str, str]: