77import rank_bm25
88from tqdm .auto import tqdm
99
10- from haystack .preview .document_stores .decorator import store
10+ from haystack .preview .document_stores .decorator import (
11+ document_store ,
12+ default_document_store_to_dict ,
13+ default_document_store_from_dict ,
14+ )
1115from haystack .preview .dataclasses import Document
12- from haystack .preview .document_stores .protocols import DuplicatePolicy
16+ from haystack .preview .document_stores .protocols import DuplicatePolicy , DocumentStore
1317from haystack .preview .document_stores .memory ._filters import match
1418from haystack .preview .document_stores .errors import DuplicateDocumentError , MissingDocumentError
1519from haystack .utils .scipy_utils import expit
2428SCALING_FACTOR = 8
2529
2630
27- @store
31+ @document_store
2832class MemoryDocumentStore :
2933 """
3034 Stores data in-memory. It's ephemeral and cannot be saved to disk.
@@ -37,7 +41,7 @@ def __init__(
3741 bm25_parameters : Optional [Dict ] = None ,
3842 ):
3943 """
40- Initializes the store .
44+ Initializes the DocumentStore .
4145 """
4246 self .storage : Dict [str , Document ] = {}
4347 self .tokenizer = re .compile (bm25_tokenization_regex ).findall
@@ -54,9 +58,22 @@ def __init__(
5458 "bm25_parameters" : self .bm25_parameters ,
5559 }
5660
61+ def to_dict (self ) -> Dict [str , Any ]:
62+ """
63+ Serializes this store to a dictionary.
64+ """
65+ return default_document_store_to_dict (self )
66+
67+ @classmethod
68+ def from_dict (cls , data : Dict [str , Any ]) -> "DocumentStore" :
69+ """
70+ Deserializes the store from a dictionary.
71+ """
72+ return default_document_store_from_dict (cls , data )
73+
5774 def count_documents (self ) -> int :
5875 """
59- Returns the number of how many documents are present in the document store .
76+ Returns the number of how many documents are present in the DocumentStore .
6077 """
6178 return len (self .storage .keys ())
6279
@@ -137,11 +154,11 @@ def filter_documents(self, filters: Optional[Dict[str, Any]] = None) -> List[Doc
137154
138155 def write_documents (self , documents : List [Document ], policy : DuplicatePolicy = DuplicatePolicy .FAIL ) -> None :
139156 """
140- Writes (or overwrites) documents into the store .
157+ Writes (or overwrites) documents into the DocumentStore .
141158
142159 :param documents: a list of documents.
143160 :param policy: documents with the same ID count as duplicates. When duplicates are met,
144- the store can:
161+ the DocumentStore can:
145162 - skip: keep the existing document and ignore the new one.
146163 - overwrite: remove the old document and write the new one.
147164 - fail: an error is raised
@@ -165,8 +182,8 @@ def write_documents(self, documents: List[Document], policy: DuplicatePolicy = D
165182
166183 def delete_documents (self , document_ids : List [str ]) -> None :
167184 """
168- Deletes all documents with a matching document_ids from the document store .
169- Fails with `MissingDocumentError` if no document with this id is present in the store .
185+ Deletes all documents with a matching document_ids from the DocumentStore .
186+ Fails with `MissingDocumentError` if no document with this id is present in the DocumentStore .
170187
171188 :param object_ids: the object_ids to delete
172189 """
@@ -218,7 +235,7 @@ def bm25_retrieval(
218235 csv_content = str_content .to_csv (index = False )
219236 lower_case_documents .append (csv_content .lower ())
220237
221- # Tokenize the entire content of the document store
238+ # Tokenize the entire content of the DocumentStore
222239 tokenized_corpus = [
223240 self .tokenizer (doc ) for doc in tqdm (lower_case_documents , unit = " docs" , desc = "Ranking by BM25..." )
224241 ]
0 commit comments