diff --git a/libs/community/langchain_community/retrievers/elastic_search_bm25.py b/libs/community/langchain_community/retrievers/elastic_search_bm25.py index c67f5aa5c53ea..6c346c2b17f2f 100644 --- a/libs/community/langchain_community/retrievers/elastic_search_bm25.py +++ b/libs/community/langchain_community/retrievers/elastic_search_bm25.py @@ -183,3 +183,26 @@ def _get_relevant_documents( ) ) return docs[: self.k] + + async def _aget_relevant_documents( + self, query: str, *, run_manager: CallbackManagerForRetrieverRun + ) -> List[Document]: + """ + Async implementation of the retriever + + Async client (from elasticsearch import AsyncElasticsearch) need to be passed as an attribute to the class before invoking. + """ + query_dict = self.build_query_body(query) + res = await self.client.search( + index=self.index_name, body=query_dict, source=["content", "metadata"] + ) + + docs = [] + for r in res["hits"]["hits"]: + docs.append( + Document( + metadata=r["_source"]["metadata"], + page_content=r["_source"]["content"], + ) + ) + return docs[: self.k] \ No newline at end of file