-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathtest_document_store.py
More file actions
636 lines (537 loc) · 27 KB
/
test_document_store.py
File metadata and controls
636 lines (537 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import random
from unittest.mock import patch
import pytest
from haystack.dataclasses.document import Document
from haystack.document_stores.errors import DocumentStoreError, DuplicateDocumentError
from haystack.document_stores.types import DuplicatePolicy
from haystack.testing.document_store import CountDocumentsTest, DeleteDocumentsTest, WriteDocumentsTest
from opensearchpy.exceptions import RequestError
from haystack_integrations.document_stores.opensearch import OpenSearchDocumentStore
from haystack_integrations.document_stores.opensearch.document_store import DEFAULT_MAX_CHUNK_BYTES
@patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch")
def test_to_dict(_mock_opensearch_client):
document_store = OpenSearchDocumentStore(hosts="some hosts")
res = document_store.to_dict()
assert res == {
"type": "haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore",
"init_parameters": {
"embedding_dim": 768,
"hosts": "some hosts",
"index": "default",
"mappings": {
"dynamic_templates": [{"strings": {"mapping": {"type": "keyword"}, "match_mapping_type": "string"}}],
"properties": {
"content": {"type": "text"},
"embedding": {"dimension": 768, "index": True, "type": "knn_vector"},
},
},
"max_chunk_bytes": DEFAULT_MAX_CHUNK_BYTES,
"method": None,
"settings": {"index.knn": True},
"return_embedding": False,
"create_index": True,
"http_auth": None,
"use_ssl": None,
"verify_certs": None,
"timeout": None,
},
}
@patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch")
def test_from_dict(_mock_opensearch_client):
data = {
"type": "haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore",
"init_parameters": {
"hosts": "some hosts",
"index": "default",
"max_chunk_bytes": 1000,
"embedding_dim": 1536,
"create_index": False,
"return_embedding": True,
"aws_service": "es",
"http_auth": ("admin", "admin"),
"use_ssl": True,
"verify_certs": True,
"timeout": 60,
},
}
document_store = OpenSearchDocumentStore.from_dict(data)
assert document_store._hosts == "some hosts"
assert document_store._index == "default"
assert document_store._max_chunk_bytes == 1000
assert document_store._embedding_dim == 1536
assert document_store._method is None
assert document_store._mappings == {
"properties": {
"embedding": {"type": "knn_vector", "index": True, "dimension": 1536},
"content": {"type": "text"},
},
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {"type": "keyword"},
}
}
],
}
assert document_store._settings == {"index.knn": True}
assert document_store._return_embedding is True
assert document_store._create_index is False
assert document_store._http_auth == ("admin", "admin")
assert document_store._use_ssl is True
assert document_store._verify_certs is True
assert document_store._timeout == 60
@patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch")
def test_init_is_lazy(_mock_opensearch_client):
OpenSearchDocumentStore(hosts="testhost")
_mock_opensearch_client.assert_not_called()
@patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch")
def test_get_default_mappings(_mock_opensearch_client):
store = OpenSearchDocumentStore(hosts="testhost", embedding_dim=1536, method={"name": "hnsw"})
assert store._mappings["properties"]["embedding"] == {
"type": "knn_vector",
"index": True,
"dimension": 1536,
"method": {"name": "hnsw"},
}
@patch("haystack_integrations.document_stores.opensearch.document_store.bulk")
def test_routing_extracted_from_metadata(mock_bulk, document_store):
"""Test routing extraction from document metadata"""
mock_bulk.return_value = (2, [])
docs = [
Document(id="1", content="Doc", meta={"_routing": "user_a", "other": "data"}),
Document(id="2", content="Doc"),
]
document_store.write_documents(docs)
actions = list(mock_bulk.call_args.kwargs["actions"])
# Routing should be at action level, not in _source
assert actions[0]["_routing"] == "user_a"
assert "_routing" not in actions[0]["_source"].get("meta", {})
# Other metadata should be preserved
assert actions[0]["_source"]["other"] == "data"
# Second doc has no routing
assert "_routing" not in actions[1]
assert "_routing" not in actions[1]["_source"].get("meta", {})
@patch("haystack_integrations.document_stores.opensearch.document_store.bulk")
def test_routing_in_delete(mock_bulk, document_store):
"""Test routing parameter in delete operations"""
mock_bulk.return_value = (2, [])
routing_map = {"1": "user_a", "2": "user_b"}
document_store.delete_documents(["1", "2", "3"], routing=routing_map)
actions = list(mock_bulk.call_args.kwargs["actions"])
assert actions[0]["_routing"] == "user_a"
assert actions[1]["_routing"] == "user_b"
assert "_routing" not in actions[2]
@pytest.mark.integration
class TestDocumentStore(CountDocumentsTest, WriteDocumentsTest, DeleteDocumentsTest):
"""
Common test cases will be provided by `DocumentStoreBaseTests` but
you can add more to this class.
"""
def assert_documents_are_equal(self, received: list[Document], expected: list[Document]):
"""
The OpenSearchDocumentStore.filter_documents() method returns a Documents with their score set.
We don't want to compare the score, so we set it to None before comparing the documents.
"""
for doc in received:
doc.score = None
assert received == expected
def test_write_documents(self, document_store: OpenSearchDocumentStore):
docs = [Document(id="1")]
assert document_store.write_documents(docs) == 1
with pytest.raises(DuplicateDocumentError):
document_store.write_documents(docs, DuplicatePolicy.FAIL)
def test_write_documents_readonly(self, document_store_readonly: OpenSearchDocumentStore):
docs = [Document(id="1")]
with pytest.raises(DocumentStoreError, match="index_not_found_exception"):
document_store_readonly.write_documents(docs)
def test_create_index(self, document_store_readonly: OpenSearchDocumentStore):
document_store_readonly.create_index()
assert document_store_readonly._client.indices.exists(index=document_store_readonly._index)
def test_bm25_retrieval(self, document_store: OpenSearchDocumentStore, test_documents: list[Document]):
document_store.write_documents(test_documents)
res = document_store._bm25_retrieval("functional", top_k=3)
assert len(res) == 3
assert "functional" in res[0].content
assert "functional" in res[1].content
assert "functional" in res[2].content
def test_bm25_retrieval_pagination(self, document_store: OpenSearchDocumentStore, test_documents: list[Document]):
"""
Test that handling of pagination works as expected, when the matching documents are > 10.
"""
document_store.write_documents(test_documents)
res = document_store._bm25_retrieval("programming", top_k=11)
assert len(res) == 11
assert all("programming" in doc.content for doc in res)
def test_bm25_retrieval_all_terms_must_match(
self, document_store: OpenSearchDocumentStore, test_documents: list[Document]
):
document_store.write_documents(test_documents)
res = document_store._bm25_retrieval("functional Haskell", top_k=3, all_terms_must_match=True)
assert len(res) == 1
assert "Haskell is a functional programming language" in res[0].content
def test_bm25_retrieval_all_terms_must_match_false(
self, document_store: OpenSearchDocumentStore, test_documents: list[Document]
):
document_store.write_documents(test_documents)
res = document_store._bm25_retrieval("functional Haskell", top_k=10, all_terms_must_match=False)
assert len(res) == 5
assert all("functional" in doc.content for doc in res)
def test_bm25_retrieval_with_fuzziness(
self, document_store: OpenSearchDocumentStore, test_documents: list[Document]
):
document_store.write_documents(test_documents)
query_with_typo = "functinal"
# Query without fuzziness to search for the exact match
res = document_store._bm25_retrieval(query_with_typo, top_k=3, fuzziness="0")
# Nothing is found as the query contains a typo
assert res == []
# Query with fuzziness with the same query
res = document_store._bm25_retrieval(query_with_typo, top_k=3, fuzziness="1")
assert len(res) == 3
assert "functional" in res[0].content
assert "functional" in res[1].content
assert "functional" in res[2].content
def test_bm25_retrieval_with_filters(self, document_store: OpenSearchDocumentStore, test_documents: list[Document]):
document_store.write_documents(test_documents)
res = document_store._bm25_retrieval(
"programming",
top_k=10,
filters={"field": "language_type", "operator": "==", "value": "functional"},
)
assert len(res) == 5
retrieved_ids = sorted([doc.id for doc in res])
assert retrieved_ids == ["1", "2", "3", "4", "5"]
def test_bm25_retrieval_with_custom_query(
self, document_store: OpenSearchDocumentStore, test_documents: list[Document]
):
document_store.write_documents(test_documents)
custom_query = {
"query": {
"function_score": {
"query": {"bool": {"must": {"match": {"content": "$query"}}, "filter": "$filters"}},
"field_value_factor": {"field": "likes", "factor": 0.1, "modifier": "log1p", "missing": 0},
}
}
}
res = document_store._bm25_retrieval(
"functional",
top_k=3,
custom_query=custom_query,
filters={"field": "language_type", "operator": "==", "value": "functional"},
)
assert len(res) == 3
assert "1" == res[0].id
assert "2" == res[1].id
assert "3" == res[2].id
def test_bm25_retrieval_with_custom_query_empty_filters(
self, document_store: OpenSearchDocumentStore, test_documents: list[Document]
):
document_store.write_documents(test_documents)
custom_query = {
"query": {
"function_score": {
"query": {"bool": {"must": {"match": {"content": "$query"}}, "filter": "$filters"}},
"field_value_factor": {"field": "likes", "factor": 0.1, "modifier": "log1p", "missing": 0},
}
}
}
res = document_store._bm25_retrieval(
"functional",
top_k=3,
custom_query=custom_query,
)
assert len(res) == 3
assert "1" == res[0].id
assert "2" == res[1].id
assert "3" == res[2].id
def test_embedding_retrieval(self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore):
docs = [
Document(content="Most similar document", embedding=[1.0, 1.0, 1.0, 1.0]),
Document(content="2nd best document", embedding=[0.8, 0.8, 0.8, 1.0]),
Document(content="Not very similar document", embedding=[0.0, 0.8, 0.3, 0.9]),
]
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
results = document_store_embedding_dim_4_no_emb_returned._embedding_retrieval(
query_embedding=[0.1, 0.1, 0.1, 0.1], top_k=2, filters={}
)
assert len(results) == 2
assert results[0].content == "Most similar document"
assert results[1].content == "2nd best document"
def test_embedding_retrieval_with_filters(
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
):
docs = [
Document(content="Most similar document", embedding=[1.0, 1.0, 1.0, 1.0]),
Document(content="2nd best document", embedding=[0.8, 0.8, 0.8, 1.0]),
Document(
content="Not very similar document with meta field",
embedding=[0.0, 0.8, 0.3, 0.9],
meta={"meta_field": "custom_value"},
),
]
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
filters = {"field": "meta_field", "operator": "==", "value": "custom_value"}
# we set top_k=3, to make the test pass as we are not sure whether efficient filtering is supported for nmslib
# TODO: remove top_k=3, when efficient filtering is supported for nmslib
results = document_store_embedding_dim_4_no_emb_returned._embedding_retrieval(
query_embedding=[0.1, 0.1, 0.1, 0.1], top_k=3, filters=filters
)
assert len(results) == 1
assert results[0].content == "Not very similar document with meta field"
def test_embedding_retrieval_with_filters_efficient_filtering(
self, document_store_embedding_dim_4_no_emb_returned_faiss: OpenSearchDocumentStore
):
docs = [
Document(content="Most similar document", embedding=[1.0, 1.0, 1.0, 1.0]),
Document(content="2nd best document", embedding=[0.8, 0.8, 0.8, 1.0]),
Document(
content="Not very similar document with meta field",
embedding=[0.0, 0.8, 0.3, 0.9],
meta={"meta_field": "custom_value"},
),
]
document_store_embedding_dim_4_no_emb_returned_faiss.write_documents(docs)
filters = {"field": "meta_field", "operator": "==", "value": "custom_value"}
results = document_store_embedding_dim_4_no_emb_returned_faiss._embedding_retrieval(
query_embedding=[0.1, 0.1, 0.1, 0.1],
filters=filters,
efficient_filtering=True,
)
assert len(results) == 1
assert results[0].content == "Not very similar document with meta field"
def test_embedding_retrieval_pagination(
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
):
"""
Test that handling of pagination works as expected, when the matching documents are > 10.
"""
docs = [
Document(content=f"Document {i}", embedding=[random.random() for _ in range(4)]) # noqa: S311
for i in range(20)
]
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
results = document_store_embedding_dim_4_no_emb_returned._embedding_retrieval(
query_embedding=[0.1, 0.1, 0.1, 0.1], top_k=11, filters={}
)
assert len(results) == 11
def test_embedding_retrieval_with_custom_query(
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
):
docs = [
Document(content="Most similar document", embedding=[1.0, 1.0, 1.0, 1.0]),
Document(content="2nd best document", embedding=[0.8, 0.8, 0.8, 1.0]),
Document(
content="Not very similar document with meta field",
embedding=[0.0, 0.8, 0.3, 0.9],
meta={"meta_field": "custom_value"},
),
]
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
custom_query = {
"query": {
"bool": {"must": [{"knn": {"embedding": {"vector": "$query_embedding", "k": 3}}}], "filter": "$filters"}
}
}
filters = {"field": "meta_field", "operator": "==", "value": "custom_value"}
results = document_store_embedding_dim_4_no_emb_returned._embedding_retrieval(
query_embedding=[0.1, 0.1, 0.1, 0.1], top_k=1, filters=filters, custom_query=custom_query
)
assert len(results) == 1
assert results[0].content == "Not very similar document with meta field"
def test_embedding_retrieval_query_documents_different_embedding_sizes(
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
):
"""
Test that the retrieval fails if the query embedding and the documents have different embedding sizes.
"""
docs = [Document(content="Hello world", embedding=[0.1, 0.2, 0.3, 0.4])]
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
with pytest.raises(RequestError):
document_store_embedding_dim_4_no_emb_returned._embedding_retrieval(query_embedding=[0.1, 0.1])
def test_write_documents_different_embedding_sizes_fail(
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
):
"""
Test that write_documents fails if the documents have different embedding sizes.
"""
docs = [
Document(content="Hello world", embedding=[0.1, 0.2, 0.3, 0.4]),
Document(content="Hello world", embedding=[0.1, 0.2]),
]
with pytest.raises(DocumentStoreError):
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
@patch("haystack_integrations.document_stores.opensearch.document_store.bulk")
def test_write_documents_with_badly_formatted_bulk_errors(self, mock_bulk, document_store):
error = {"some_key": "some_value"}
mock_bulk.return_value = ([], [error])
with pytest.raises(DocumentStoreError) as e:
document_store.write_documents([Document(content="Hello world")])
e.match(f"{error}")
@patch("haystack_integrations.document_stores.opensearch.document_store.bulk")
def test_write_documents_max_chunk_bytes(self, mock_bulk, document_store):
mock_bulk.return_value = (1, [])
document_store.write_documents([Document(content="Hello world")])
assert mock_bulk.call_args.kwargs["max_chunk_bytes"] == DEFAULT_MAX_CHUNK_BYTES
def test_embedding_retrieval_but_dont_return_embeddings_for_embedding_retrieval(
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
):
docs = [
Document(content="Most similar document", embedding=[1.0, 1.0, 1.0, 1.0]),
Document(content="2nd best document", embedding=[0.8, 0.8, 0.8, 1.0]),
Document(content="Not very similar document", embedding=[0.0, 0.8, 0.3, 0.9]),
]
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
results = document_store_embedding_dim_4_no_emb_returned._embedding_retrieval(
query_embedding=[0.1, 0.1, 0.1, 0.1], top_k=2, filters={}
)
assert len(results) == 2
assert results[0].embedding is None
def test_embedding_retrieval_but_dont_return_embeddings_for_bm25_retrieval(
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
):
docs = [
Document(content="Most similar document", embedding=[1.0, 1.0, 1.0, 1.0]),
Document(content="2nd best document", embedding=[0.8, 0.8, 0.8, 1.0]),
Document(content="Not very similar document", embedding=[0.0, 0.8, 0.3, 0.9]),
]
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
results = document_store_embedding_dim_4_no_emb_returned._bm25_retrieval("document", top_k=2)
assert len(results) == 2
assert results[0].embedding is None
def test_filter_documents_no_embedding_returned(
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
):
docs = [
Document(content="Most similar document", embedding=[1.0, 1.0, 1.0, 1.0]),
Document(content="2nd best document", embedding=[0.8, 0.8, 0.8, 1.0]),
Document(content="Not very similar document", embedding=[0.0, 0.8, 0.3, 0.9]),
]
document_store_embedding_dim_4_no_emb_returned.write_documents(docs)
results = document_store_embedding_dim_4_no_emb_returned.filter_documents()
assert len(results) == 3
assert results[0].embedding is None
assert results[1].embedding is None
assert results[2].embedding is None
def test_delete_all_documents_index_recreation(self, document_store: OpenSearchDocumentStore):
# populate the index with some documents
docs = [Document(id="1", content="A first document"), Document(id="2", content="Second document")]
document_store.write_documents(docs)
# capture index structure before deletion
assert document_store._client is not None
index_info_before = document_store._client.indices.get(index=document_store._index)
mappings_before = index_info_before[document_store._index]["mappings"]
settings_before = index_info_before[document_store._index]["settings"]
# delete all documents
document_store.delete_all_documents(recreate_index=True)
assert document_store.count_documents() == 0
# verify index structure is preserved
index_info_after = document_store._client.indices.get(index=document_store._index)
mappings_after = index_info_after[document_store._index]["mappings"]
settings_after = index_info_after[document_store._index]["settings"]
assert mappings_after == mappings_before, "delete_all_documents should preserve index mappings"
settings_after["index"].pop("uuid", None)
settings_after["index"].pop("creation_date", None)
settings_before["index"].pop("uuid", None)
settings_before["index"].pop("creation_date", None)
assert settings_after == settings_before, "delete_all_documents should preserve index settings"
new_doc = Document(id="4", content="New document after delete all")
document_store.write_documents([new_doc])
assert document_store.count_documents() == 1
results = document_store.filter_documents()
assert len(results) == 1
assert results[0].content == "New document after delete all"
def test_delete_all_documents_no_index_recreation(self, document_store: OpenSearchDocumentStore):
docs = [Document(id="1", content="A first document"), Document(id="2", content="Second document")]
document_store.write_documents(docs)
assert document_store.count_documents() == 2
document_store.delete_all_documents(recreate_index=False, refresh=True)
assert document_store.count_documents() == 0
new_doc = Document(id="3", content="New document after delete all")
document_store.write_documents([new_doc])
assert document_store.count_documents() == 1
results = document_store.filter_documents()
assert len(results) == 1
assert results[0].content == "New document after delete all"
def test_delete_by_filter(self, document_store: OpenSearchDocumentStore):
docs = [
Document(content="Doc 1", meta={"category": "A"}),
Document(content="Doc 2", meta={"category": "B"}),
Document(content="Doc 3", meta={"category": "A"}),
]
document_store.write_documents(docs)
assert document_store.count_documents() == 3
# Delete documents with category="A"
deleted_count = document_store.delete_by_filter(
filters={"field": "meta.category", "operator": "==", "value": "A"}, refresh=True
)
assert deleted_count == 2
assert document_store.count_documents() == 1
# Verify only category B remains
remaining_docs = document_store.filter_documents()
assert len(remaining_docs) == 1
assert remaining_docs[0].meta["category"] == "B"
def test_update_by_filter(self, document_store: OpenSearchDocumentStore):
docs = [
Document(content="Doc 1", meta={"category": "A", "status": "draft"}),
Document(content="Doc 2", meta={"category": "B", "status": "draft"}),
Document(content="Doc 3", meta={"category": "A", "status": "draft"}),
]
document_store.write_documents(docs)
assert document_store.count_documents() == 3
# Update status for category="A" documents
updated_count = document_store.update_by_filter(
filters={"field": "meta.category", "operator": "==", "value": "A"},
meta={"status": "published"},
refresh=True,
)
assert updated_count == 2
# Verify the updates
published_docs = document_store.filter_documents(
filters={"field": "meta.status", "operator": "==", "value": "published"}
)
assert len(published_docs) == 2
for doc in published_docs:
assert doc.meta["category"] == "A"
assert doc.meta["status"] == "published"
# Verify category B still has draft status
draft_docs = document_store.filter_documents(
filters={"field": "meta.status", "operator": "==", "value": "draft"}
)
assert len(draft_docs) == 1
assert draft_docs[0].meta["category"] == "B"
@pytest.mark.integration
def test_write_with_routing(self, document_store: OpenSearchDocumentStore):
"""Test writing documents with routing metadata"""
docs = [
Document(id="1", content="User A doc", meta={"_routing": "user_a", "category": "test"}),
Document(id="2", content="User B doc", meta={"_routing": "user_b"}),
Document(id="3", content="No routing"),
]
written = document_store.write_documents(docs)
assert written == 3
assert document_store.count_documents() == 3
# Verify _routing not stored in metadata
retrieved = document_store.filter_documents()
retrieved_by_id = {doc.id: doc for doc in retrieved}
# Check _routing is not stored for any document
for doc in retrieved:
assert "_routing" not in doc.meta
assert retrieved_by_id["1"].meta["category"] == "test"
assert retrieved_by_id["2"].meta == {}
assert retrieved_by_id["3"].meta == {}
@pytest.mark.integration
def test_delete_with_routing(self, document_store: OpenSearchDocumentStore):
"""Test deleting documents with routing"""
docs = [
Document(id="1", content="Doc 1", meta={"_routing": "user_a"}),
Document(id="2", content="Doc 2", meta={"_routing": "user_b"}),
Document(id="3", content="Doc 3"),
]
document_store.write_documents(docs)
routing_map = {"1": "user_a", "2": "user_b"}
document_store.delete_documents(["1", "2"], routing=routing_map)
assert document_store.count_documents() == 1