@@ -213,4 +213,56 @@ To delete nested data, you need to specify the `id`.
213213del doc_index[index_docs[6 ].id]
214214```
215215
216- Check [ here] ( docindex#nested-data-with-subindex ) for nested data with subindex.
216+ Check [ here] ( ../docindex#nested-data-with-subindex ) for nested data with subindex.
217+
218+ ### Update elements
219+ In order to update a Document inside the index, you only need to reindex it with the updated attributes.
220+
221+ First lets create a schema for our Index
222+ ``` python
223+ import numpy as np
224+ from docarray import BaseDoc, DocList
225+ from docarray.typing import NdArray
226+ from docarray.index import HnswDocumentIndex
227+ class MyDoc (BaseDoc ):
228+ text: str
229+ embedding: NdArray[128 ]
230+ ```
231+ Now we can instantiate our Index and index some data.
232+
233+ ``` python
234+ docs = DocList[MyDoc](
235+ [MyDoc(embedding = np.random.rand(10 ), text = f ' I am the first version of Document { i} ' ) for i in range (100 )]
236+ )
237+ index = HnswDocumentIndex[MyDoc]()
238+ index.index(docs)
239+ assert index.num_docs() == 100
240+ ```
241+
242+ Now we can find relevant documents
243+
244+ ``` python
245+ res = index.find(query = docs[0 ], search_field = ' tens' , limit = 100 )
246+ assert len (res.documents) == 100
247+ for doc in res.documents:
248+ assert ' I am the first version' in doc.text
249+ ```
250+
251+ and update all of the text of this documents and reindex them
252+
253+ ``` python
254+ for i, doc in enumerate (docs):
255+ doc.text = f ' I am the second version of Document { i} '
256+
257+ index.index(docs)
258+ assert index.num_docs() == 100
259+ ```
260+
261+ When we retrieve them again we can see that their text attribute has been updated accordingly
262+
263+ ``` python
264+ res = index.find(query = docs[0 ], search_field = ' tens' , limit = 100 )
265+ assert len (res.documents) == 100
266+ for doc in res.documents:
267+ assert ' I am the second version' in doc.text
268+ ```
0 commit comments