@@ -230,6 +230,16 @@ def execute_query(self, query: Any, *args, **kwargs) -> Any:
230230 """
231231 ...
232232
233+ @abstractmethod
234+ def _doc_exists (self , doc_id : str ) -> bool :
235+ """
236+ Checks if a given document exists in the index.
237+
238+ :param doc_id: The id of a document to check.
239+ :return: True if the document exists in the index, False otherwise.
240+ """
241+ ...
242+
233243 @abstractmethod
234244 def _find (
235245 self ,
@@ -403,6 +413,21 @@ def __delitem__(self, key: Union[str, Sequence[str]]):
403413 # delete data
404414 self ._del_items (key )
405415
416+ def __contains__ (self , item : BaseDoc ) -> bool :
417+ """
418+ Checks if a given document exists in the index.
419+
420+ :param item: The document to check.
421+ It must be an instance of BaseDoc or its subclass.
422+ :return: True if the document exists in the index, False otherwise.
423+ """
424+ if safe_issubclass (type (item ), BaseDoc ):
425+ return self ._doc_exists (str (item .id ))
426+ else :
427+ raise TypeError (
428+ f"item must be an instance of BaseDoc or its subclass, not '{ type (item ).__name__ } '"
429+ )
430+
406431 def configure (self , runtime_config = None , ** kwargs ):
407432 """
408433 Configure the DocumentIndex.
@@ -1170,14 +1195,6 @@ def _get_root_doc_id(self, id: str, root: str, sub: str) -> str:
11701195 )
11711196 return self ._get_root_doc_id (cur_root_id , root , '' )
11721197
1173- def __contains__ (self , item : BaseDoc ) -> bool :
1174- """Checks if a given BaseDoc item is contained in the index.
1175-
1176- :param item: the given BaseDoc
1177- :return: if the given BaseDoc item is contained in the index
1178- """
1179- return False # Will be overridden by backends
1180-
11811198 def subindex_contains (self , item : BaseDoc ) -> bool :
11821199 """Checks if a given BaseDoc item is contained in the index or any of its subindices.
11831200
0 commit comments