Skip to content

Commit b19479b

Browse files
committed
docs: make sure all extends use context manager
1 parent 5619e2c commit b19479b

3 files changed

Lines changed: 111 additions & 101 deletions

File tree

docs/advanced/document-store/elasticsearch.md

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ da = DocumentArray(
8282
config={'index_name': 'old_stuff', 'n_dim': 128},
8383
)
8484

85-
da.extend([Document() for _ in range(1000)])
85+
with da:
86+
da.extend([Document() for _ in range(1000)])
8687

8788
da2 = DocumentArray(
8889
storage='elasticsearch',
@@ -304,13 +305,14 @@ for those that have `pizza` in their text description.
304305
from docarray import DocumentArray, Document
305306

306307
da = DocumentArray(storage='elasticsearch', config={'n_dim': 2, 'index_text': True})
307-
da.extend(
308-
[
309-
Document(text='Person eating'),
310-
Document(text='Person eating pizza'),
311-
Document(text='Pizza restaurant'),
312-
]
313-
)
308+
with da:
309+
da.extend(
310+
[
311+
Document(text='Person eating'),
312+
Document(text='Person eating pizza'),
313+
Document(text='Pizza restaurant'),
314+
]
315+
)
314316

315317
pizza_docs = da.find('pizza')
316318
pizza_docs[:, 'text']
@@ -336,28 +338,29 @@ from docarray import DocumentArray, Document
336338
da = DocumentArray(
337339
storage='elasticsearch', config={'n_dim': 32, 'tag_indices': ['food_type', 'price']}
338340
)
339-
da.extend(
340-
[
341-
Document(
342-
tags={
343-
'food_type': 'Italian and Spanish food',
344-
'price': 'cheap but not that cheap',
345-
},
346-
),
347-
Document(
348-
tags={
349-
'food_type': 'French and Italian food',
350-
'price': 'on the expensive side',
351-
},
352-
),
353-
Document(
354-
tags={
355-
'food_type': 'chinese noddles',
356-
'price': 'quite cheap for what you get!',
357-
},
358-
),
359-
]
360-
)
341+
with da:
342+
da.extend(
343+
[
344+
Document(
345+
tags={
346+
'food_type': 'Italian and Spanish food',
347+
'price': 'cheap but not that cheap',
348+
},
349+
),
350+
Document(
351+
tags={
352+
'food_type': 'French and Italian food',
353+
'price': 'on the expensive side',
354+
},
355+
),
356+
Document(
357+
tags={
358+
'food_type': 'chinese noddles',
359+
'price': 'quite cheap for what you get!',
360+
},
361+
),
362+
]
363+
)
361364

362365
results_cheap = da.find('cheap', index='price')
363366
print('searching "cheap" in <price>:\n\t', results_cheap[:, 'tags__price'])

docs/advanced/document-store/redis.md

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,27 @@ da = DocumentArray(
144144
},
145145
)
146146

147-
da.extend(
148-
[
149-
Document(
150-
id=f'{i}',
151-
embedding=i * np.ones(n_dim),
152-
tags={'price': i, 'color': 'blue', 'stock': i % 2 == 0},
153-
)
154-
for i in range(10)
155-
]
156-
)
157-
da.extend(
158-
[
159-
Document(
160-
id=f'{i+10}',
161-
embedding=i * np.ones(n_dim),
162-
tags={'price': i, 'color': 'red', 'stock': i % 2 == 0},
163-
)
164-
for i in range(10)
165-
]
166-
)
147+
with da:
148+
da.extend(
149+
[
150+
Document(
151+
id=f'{i}',
152+
embedding=i * np.ones(n_dim),
153+
tags={'price': i, 'color': 'blue', 'stock': i % 2 == 0},
154+
)
155+
for i in range(10)
156+
]
157+
)
158+
da.extend(
159+
[
160+
Document(
161+
id=f'{i+10}',
162+
embedding=i * np.ones(n_dim),
163+
tags={'price': i, 'color': 'red', 'stock': i % 2 == 0},
164+
)
165+
for i in range(10)
166+
]
167+
)
167168

168169
print('\nIndexed price, color and stock:\n')
169170
for doc in da:
@@ -301,7 +302,8 @@ da = DocumentArray(
301302
},
302303
)
303304

304-
da.extend([Document(id=f'{i}', embedding=i * np.ones(n_dim)) for i in range(10)])
305+
with da:
306+
da.extend([Document(id=f'{i}', embedding=i * np.ones(n_dim)) for i in range(10)])
305307

306308
np_query = np.ones(n_dim) * 8
307309
n_limit = 5
@@ -367,13 +369,14 @@ The following example builds a `DocumentArray` with several documents containing
367369
from docarray import Document, DocumentArray
368370

369371
da = DocumentArray(storage='redis', config={'n_dim': 2, 'index_text': True})
370-
da.extend(
371-
[
372-
Document(id='1', text='token1 token2 token3'),
373-
Document(id='2', text='token1 token2'),
374-
Document(id='3', text='token2 token3 token4'),
375-
]
376-
)
372+
with da:
373+
da.extend(
374+
[
375+
Document(id='1', text='token1 token2 token3'),
376+
Document(id='2', text='token1 token2'),
377+
Document(id='3', text='token2 token3 token4'),
378+
]
379+
)
377380

378381
results = da.find('token1')
379382
print(results[:, 'text'])
@@ -420,28 +423,29 @@ da = DocumentArray(
420423
storage='redis',
421424
config={'n_dim': 32, 'tag_indices': ['food_type', 'price']},
422425
)
423-
da.extend(
424-
[
425-
Document(
426-
tags={
427-
'food_type': 'Italian and Spanish food',
428-
'price': 'cheap but not that cheap',
429-
},
430-
),
431-
Document(
432-
tags={
433-
'food_type': 'French and Italian food',
434-
'price': 'on the expensive side',
435-
},
436-
),
437-
Document(
438-
tags={
439-
'food_type': 'chinese noddles',
440-
'price': 'quite cheap for what you get!',
441-
},
442-
),
443-
]
444-
)
426+
with da:
427+
da.extend(
428+
[
429+
Document(
430+
tags={
431+
'food_type': 'Italian and Spanish food',
432+
'price': 'cheap but not that cheap',
433+
},
434+
),
435+
Document(
436+
tags={
437+
'food_type': 'French and Italian food',
438+
'price': 'on the expensive side',
439+
},
440+
),
441+
Document(
442+
tags={
443+
'food_type': 'chinese noddles',
444+
'price': 'quite cheap for what you get!',
445+
},
446+
),
447+
]
448+
)
445449

446450
results_cheap = da.find('cheap', index='price')
447451
print('searching "cheap" in <price>:\n\t', results_cheap[:, 'tags__price'])

docs/advanced/document-store/weaviate.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ Then, we can index some Documents:
127127
```python
128128
from docarray import Document
129129

130-
da.extend(
131-
[
132-
Document(text='Persist Documents with Weaviate.'),
133-
Document(text='And enjoy fast nearest neighbor search.'),
134-
Document(text='All while using DocArray API.'),
135-
]
136-
)
130+
with da:
131+
da.extend(
132+
[
133+
Document(text='Persist Documents with Weaviate.'),
134+
Document(text='And enjoy fast nearest neighbor search.'),
135+
Document(text='All while using DocArray API.'),
136+
]
137+
)
137138
```
138139

139140
Now, we can generate embeddings inside the database using BERT model:
@@ -426,13 +427,14 @@ da = DocumentArray(
426427
)
427428

428429
# load the dummy data
429-
da.extend(
430-
[
431-
Document(text='Persist Documents with Weaviate.'),
432-
Document(text='And enjoy fast nearest neighbor search.'),
433-
Document(text='All while using DocArray API.'),
434-
]
435-
)
430+
with da:
431+
da.extend(
432+
[
433+
Document(text='Persist Documents with Weaviate.'),
434+
Document(text='And enjoy fast nearest neighbor search.'),
435+
Document(text='All while using DocArray API.'),
436+
]
437+
)
436438

437439
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
438440
model = AutoModel.from_pretrained('bert-base-uncased')
@@ -493,13 +495,14 @@ da = DocumentArray(
493495
)
494496

495497
# load some dummy data
496-
da.extend(
497-
[
498-
Document(text='Persist Documents with Weaviate.'),
499-
Document(text='And enjoy fast nearest neighbor search.'),
500-
Document(text='All while using DocArray API.'),
501-
]
502-
)
498+
with da:
499+
da.extend(
500+
[
501+
Document(text='Persist Documents with Weaviate.'),
502+
Document(text='And enjoy fast nearest neighbor search.'),
503+
Document(text='All while using DocArray API.'),
504+
]
505+
)
503506

504507
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
505508
model = AutoModel.from_pretrained('bert-base-uncased')

0 commit comments

Comments
 (0)