Skip to content

Commit 03ea527

Browse files
committed
✨ Now supports add/delete/edit chunks
✨ Chunk hybrid search now works in the knowledgebase rather than the document
1 parent 7fbfe68 commit 03ea527

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

test/backend/services/test_vectordatabase_service.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,70 @@ def test_search_hybrid_exception(self):
11441144
)
11451145
self.assertIn("Error executing hybrid search", str(context.exception))
11461146

1147+
def test_search_hybrid_weight_accurate_boundary_values(self):
1148+
"""Test search_hybrid with different weight_accurate values to ensure line 1146 is covered."""
1149+
# Test with weight_accurate = 0.0 (semantic only)
1150+
self.mock_vdb_core.hybrid_search.return_value = [
1151+
{
1152+
"document": {"title": "Doc1", "content": "Content1"},
1153+
"score": 0.90,
1154+
"index": "test_index",
1155+
}
1156+
]
1157+
1158+
result = ElasticSearchService.search_hybrid(
1159+
index_names=["test_index"],
1160+
query="test query",
1161+
tenant_id="test_tenant",
1162+
top_k=10,
1163+
weight_accurate=0.0,
1164+
vdb_core=self.mock_vdb_core
1165+
)
1166+
self.assertEqual(len(result["results"]), 1)
1167+
self.mock_vdb_core.hybrid_search.assert_called_with(
1168+
index_names=["test_index"],
1169+
query_text="test query",
1170+
embedding_model=self.mock_embedding,
1171+
top_k=10,
1172+
weight_accurate=0.0
1173+
)
1174+
1175+
# Test with weight_accurate = 1.0 (accurate only)
1176+
self.mock_vdb_core.hybrid_search.reset_mock()
1177+
result = ElasticSearchService.search_hybrid(
1178+
index_names=["test_index"],
1179+
query="test query",
1180+
tenant_id="test_tenant",
1181+
top_k=10,
1182+
weight_accurate=1.0,
1183+
vdb_core=self.mock_vdb_core
1184+
)
1185+
self.mock_vdb_core.hybrid_search.assert_called_with(
1186+
index_names=["test_index"],
1187+
query_text="test query",
1188+
embedding_model=self.mock_embedding,
1189+
top_k=10,
1190+
weight_accurate=1.0
1191+
)
1192+
1193+
# Test with weight_accurate = 0.3 (more semantic)
1194+
self.mock_vdb_core.hybrid_search.reset_mock()
1195+
result = ElasticSearchService.search_hybrid(
1196+
index_names=["test_index"],
1197+
query="test query",
1198+
tenant_id="test_tenant",
1199+
top_k=10,
1200+
weight_accurate=0.3,
1201+
vdb_core=self.mock_vdb_core
1202+
)
1203+
self.mock_vdb_core.hybrid_search.assert_called_with(
1204+
index_names=["test_index"],
1205+
query_text="test query",
1206+
embedding_model=self.mock_embedding,
1207+
top_k=10,
1208+
weight_accurate=0.3
1209+
)
1210+
11471211
def test_health_check_healthy(self):
11481212
"""
11491213
Test health check when Elasticsearch is healthy.

test/sdk/vector_database/test_elasticsearch_core.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,88 @@ def test_delete_chunk_not_found(elasticsearch_core_instance):
645645
assert result is False
646646

647647

648+
def test_create_chunk_exception(elasticsearch_core_instance):
649+
"""Test create_chunk raises exception when client.index fails."""
650+
elasticsearch_core_instance.client = MagicMock()
651+
elasticsearch_core_instance.client.index.side_effect = Exception("Index operation failed")
652+
653+
payload = {"id": "chunk-1", "content": "A"}
654+
655+
with pytest.raises(Exception) as exc_info:
656+
elasticsearch_core_instance.create_chunk("kb-index", payload)
657+
658+
assert "Index operation failed" in str(exc_info.value)
659+
elasticsearch_core_instance.client.index.assert_called_once()
660+
661+
662+
def test_update_chunk_exception_from_resolve(elasticsearch_core_instance):
663+
"""Test update_chunk raises exception when _resolve_chunk_document_id fails."""
664+
elasticsearch_core_instance.client = MagicMock()
665+
with patch.object(
666+
elasticsearch_core_instance,
667+
"_resolve_chunk_document_id",
668+
side_effect=Exception("Resolve failed"),
669+
):
670+
updates = {"content": "updated"}
671+
672+
with pytest.raises(Exception) as exc_info:
673+
elasticsearch_core_instance.update_chunk("kb-index", "chunk-1", updates)
674+
675+
assert "Resolve failed" in str(exc_info.value)
676+
elasticsearch_core_instance.client.update.assert_not_called()
677+
678+
679+
def test_update_chunk_exception_from_update(elasticsearch_core_instance):
680+
"""Test update_chunk raises exception when client.update fails."""
681+
elasticsearch_core_instance.client = MagicMock()
682+
with patch.object(
683+
elasticsearch_core_instance,
684+
"_resolve_chunk_document_id",
685+
return_value="es-id-1",
686+
):
687+
elasticsearch_core_instance.client.update.side_effect = Exception("Update operation failed")
688+
689+
updates = {"content": "updated"}
690+
691+
with pytest.raises(Exception) as exc_info:
692+
elasticsearch_core_instance.update_chunk("kb-index", "chunk-1", updates)
693+
694+
assert "Update operation failed" in str(exc_info.value)
695+
elasticsearch_core_instance.client.update.assert_called_once()
696+
697+
698+
def test_delete_chunk_exception_from_resolve(elasticsearch_core_instance):
699+
"""Test delete_chunk raises exception when _resolve_chunk_document_id fails with non-NotFoundError."""
700+
elasticsearch_core_instance.client = MagicMock()
701+
with patch.object(
702+
elasticsearch_core_instance,
703+
"_resolve_chunk_document_id",
704+
side_effect=Exception("Resolve failed"),
705+
):
706+
with pytest.raises(Exception) as exc_info:
707+
elasticsearch_core_instance.delete_chunk("kb-index", "chunk-1")
708+
709+
assert "Resolve failed" in str(exc_info.value)
710+
elasticsearch_core_instance.client.delete.assert_not_called()
711+
712+
713+
def test_delete_chunk_exception_from_delete(elasticsearch_core_instance):
714+
"""Test delete_chunk raises exception when client.delete fails with non-NotFoundError."""
715+
elasticsearch_core_instance.client = MagicMock()
716+
with patch.object(
717+
elasticsearch_core_instance,
718+
"_resolve_chunk_document_id",
719+
return_value="es-id-1",
720+
):
721+
elasticsearch_core_instance.client.delete.side_effect = Exception("Delete operation failed")
722+
723+
with pytest.raises(Exception) as exc_info:
724+
elasticsearch_core_instance.delete_chunk("kb-index", "chunk-1")
725+
726+
assert "Delete operation failed" in str(exc_info.value)
727+
elasticsearch_core_instance.client.delete.assert_called_once()
728+
729+
648730
def test_resolve_chunk_document_id_direct_hit(elasticsearch_core_instance):
649731
"""Test _resolve_chunk_document_id returns given id when ES _id exists."""
650732
elasticsearch_core_instance.client = MagicMock()

0 commit comments

Comments
 (0)