22#
33# SPDX-License-Identifier: Apache-2.0
44import re
5- from typing import Any , Literal , Optional , Union
5+ from typing import Any , Literal
66
77from haystack import default_from_dict , default_to_dict , logging
88from haystack .dataclasses .document import Document
@@ -105,10 +105,10 @@ def __init__(
105105 self .full_text_search_index = full_text_search_index
106106 self .embedding_field = embedding_field
107107 self .content_field = content_field
108- self ._connection : Optional [ MongoClient ] = None
109- self ._connection_async : Optional [ AsyncMongoClient ] = None
110- self ._collection : Optional [ Collection ] = None
111- self ._collection_async : Optional [ AsyncCollection ] = None
108+ self ._connection : MongoClient | None = None
109+ self ._connection_async : AsyncMongoClient | None = None
110+ self ._collection : Collection | None = None
111+ self ._collection_async : AsyncCollection | None = None
112112
113113 def __del__ (self ) -> None :
114114 """
@@ -118,7 +118,7 @@ def __del__(self) -> None:
118118 self ._connection .close ()
119119
120120 @property
121- def connection (self ) -> Union [ AsyncMongoClient , MongoClient ] :
121+ def connection (self ) -> AsyncMongoClient | MongoClient :
122122 if self ._connection :
123123 return self ._connection
124124 if self ._connection_async :
@@ -127,7 +127,7 @@ def connection(self) -> Union[AsyncMongoClient, MongoClient]:
127127 raise DocumentStoreError (msg )
128128
129129 @property
130- def collection (self ) -> Union [ AsyncCollection , Collection ] :
130+ def collection (self ) -> AsyncCollection | Collection :
131131 if self ._collection :
132132 return self ._collection
133133 if self ._collection_async :
@@ -278,7 +278,7 @@ async def count_documents_async(self) -> int:
278278 assert self ._collection_async is not None
279279 return await self ._collection_async .count_documents ({})
280280
281- def filter_documents (self , filters : Optional [ dict [str , Any ]] = None ) -> list [Document ]:
281+ def filter_documents (self , filters : dict [str , Any ] | None = None ) -> list [Document ]:
282282 """
283283 Returns the documents that match the filters provided.
284284
@@ -294,7 +294,7 @@ def filter_documents(self, filters: Optional[dict[str, Any]] = None) -> list[Doc
294294 documents = list (self ._collection .find (filters ))
295295 return [self ._mongo_doc_to_haystack_doc (doc ) for doc in documents ]
296296
297- async def filter_documents_async (self , filters : Optional [ dict [str , Any ]] = None ) -> list [Document ]:
297+ async def filter_documents_async (self , filters : dict [str , Any ] | None = None ) -> list [Document ]:
298298 """
299299 Asynchronously returns the documents that match the filters provided.
300300
@@ -332,7 +332,7 @@ def write_documents(self, documents: list[Document], policy: DuplicatePolicy = D
332332 policy = DuplicatePolicy .FAIL
333333
334334 mongo_documents = [self ._haystack_doc_to_mongo_doc (doc ) for doc in documents ]
335- operations : list [Union [ UpdateOne , InsertOne , ReplaceOne ] ]
335+ operations : list [UpdateOne | InsertOne | ReplaceOne ]
336336 written_docs = len (documents )
337337
338338 if policy == DuplicatePolicy .SKIP :
@@ -377,7 +377,7 @@ async def write_documents_async(
377377
378378 mongo_documents = [self ._haystack_doc_to_mongo_doc (doc ) for doc in documents ]
379379
380- operations : list [Union [ UpdateOne , InsertOne , ReplaceOne ] ]
380+ operations : list [UpdateOne | InsertOne | ReplaceOne ]
381381 written_docs = len (documents )
382382
383383 if policy == DuplicatePolicy .SKIP :
@@ -636,7 +636,7 @@ async def delete_all_documents_async(self, *, recreate_collection: bool = False)
636636 def _embedding_retrieval (
637637 self ,
638638 query_embedding : list [float ],
639- filters : Optional [ dict [str , Any ]] = None ,
639+ filters : dict [str , Any ] | None = None ,
640640 top_k : int = 10 ,
641641 ) -> list [Document ]:
642642 """
@@ -686,7 +686,7 @@ def _embedding_retrieval(
686686 return documents
687687
688688 async def _embedding_retrieval_async (
689- self , query_embedding : list [float ], filters : Optional [ dict [str , Any ]] = None , top_k : int = 10
689+ self , query_embedding : list [float ], filters : dict [str , Any ] | None = None , top_k : int = 10
690690 ) -> list [Document ]:
691691 """
692692 Asynchronously find the documents that are most similar to the provided `query_embedding` by using a vector
@@ -738,12 +738,12 @@ async def _embedding_retrieval_async(
738738
739739 def _fulltext_retrieval (
740740 self ,
741- query : Union [ str , list [str ] ],
742- fuzzy : Optional [ dict [str , int ]] = None ,
743- match_criteria : Optional [ Literal ["any" , "all" ]] = None ,
744- score : Optional [ dict [str , dict ]] = None ,
745- synonyms : Optional [ str ] = None ,
746- filters : Optional [ dict [str , Any ]] = None ,
741+ query : str | list [str ],
742+ fuzzy : dict [str , int ] | None = None ,
743+ match_criteria : Literal ["any" , "all" ] | None = None ,
744+ score : dict [str , dict ] | None = None ,
745+ synonyms : str | None = None ,
746+ filters : dict [str , Any ] | None = None ,
747747 top_k : int = 10 ,
748748 ) -> list [Document ]:
749749 """
@@ -831,12 +831,12 @@ def _fulltext_retrieval(
831831
832832 async def _fulltext_retrieval_async (
833833 self ,
834- query : Union [ str , list [str ] ],
835- fuzzy : Optional [ dict [str , int ]] = None ,
836- match_criteria : Optional [ Literal ["any" , "all" ]] = None ,
837- score : Optional [ dict [str , dict ]] = None ,
838- synonyms : Optional [ str ] = None ,
839- filters : Optional [ dict [str , Any ]] = None ,
834+ query : str | list [str ],
835+ fuzzy : dict [str , int ] | None = None ,
836+ match_criteria : Literal ["any" , "all" ] | None = None ,
837+ score : dict [str , dict ] | None = None ,
838+ synonyms : str | None = None ,
839+ filters : dict [str , Any ] | None = None ,
840840 top_k : int = 10 ,
841841 ) -> list [Document ]:
842842 """
0 commit comments