实现向量搜索应用层的命令和查询处理器,协调领域对象完成用例。
任务详情:
application/vector_search/handlers.py,实现 CreateDocumentHandlerUpdateDocumentHandler 和 DeleteDocumentHandlerSearchDocumentsHandler 和 GetDocumentHandler相关需求:
DocumentRepositoryDocumentRepositoryResourceNotFoundExceptionDocumentRepositoryDocumentRepositoryHybridSearchService(可选)DocumentRepositoryResourceNotFoundException所有处理器通过构造函数注入依赖:
# 命令处理器
create_handler = CreateDocumentHandler(document_repository)
update_handler = UpdateDocumentHandler(document_repository)
delete_handler = DeleteDocumentHandler(document_repository)
# 查询处理器
search_handler = SearchDocumentsHandler(
document_repository,
hybrid_search_service
)
get_handler = GetDocumentHandler(document_repository)
处理器统一捕获并转换异常:
| 原始异常 | 转换后异常 | 场景 |
|---|---|---|
DomainException |
ValidationException |
领域对象验证失败 |
ResourceNotFoundException |
直接重新抛出 | 资源不存在 |
Exception |
ApplicationException |
意外错误 |
处理器使用结构化日志记录关键操作:
INFO 级别:
WARNING 级别:
ERROR 级别:
日志包含上下文信息:
为 SearchDocumentsQuery 添加了 include_embedding 属性:
@dataclass
class SearchDocumentsQuery:
query_text: str
top_k: int = 10
filters: Optional[Dict[str, Any]] = None
use_hybrid: bool = True
min_score: Optional[float] = None
include_embedding: bool = False # 新增
src/application/vector_search/handlers.py
tests/unit/application/vector_search/test_handlers.py
src/application/vector_search/README.md
SearchDocumentsQuery 添加 include_embedding 属性所有单元测试通过:
========================= test session starts =========================
collected 14 items
tests/unit/application/vector_search/test_handlers.py::TestCreateDocumentHandler::test_handle_creates_document_successfully PASSED [ 7%]
tests/unit/application/vector_search/test_handlers.py::TestCreateDocumentHandler::test_handle_with_empty_metadata PASSED [ 14%]
tests/unit/application/vector_search/test_handlers.py::TestCreateDocumentHandler::test_handle_raises_application_exception_on_repository_error PASSED [ 21%]
tests/unit/application/vector_search/test_handlers.py::TestUpdateDocumentHandler::test_handle_updates_content_successfully PASSED [ 28%]
tests/unit/application/vector_search/test_handlers.py::TestUpdateDocumentHandler::test_handle_updates_metadata_with_merge PASSED [ 35%]
tests/unit/application/vector_search/test_handlers.py::TestUpdateDocumentHandler::test_handle_updates_metadata_without_merge PASSED [ 42%]
tests/unit/application/vector_search/test_handlers.py::TestUpdateDocumentHandler::test_handle_raises_not_found_when_document_does_not_exist PASSED [ 50%]
tests/unit/application/vector_search/test_handlers.py::TestDeleteDocumentHandler::test_handle_deletes_document_successfully PASSED [ 57%]
tests/unit/application/vector_search/test_handlers.py::TestDeleteDocumentHandler::test_handle_raises_application_exception_on_repository_error PASSED [ 64%]
tests/unit/application/vector_search/test_handlers.py::TestSearchDocumentsHandler::test_handle_searches_documents_successfully PASSED [ 71%]
tests/unit/application/vector_search/test_handlers.py::TestSearchDocumentsHandler::test_handle_with_filters PASSED [ 78%]
tests/unit/application/vector_search/test_handlers.py::TestGetDocumentHandler::test_handle_gets_document_successfully PASSED [ 85%]
tests/unit/application/vector_search/test_handlers.py::TestGetDocumentHandler::test_handle_raises_not_found_when_document_does_not_exist PASSED [ 92%]
tests/unit/application/vector_search/test_handlers.py::TestGetDocumentHandler::test_handle_with_include_embedding PASSED [100%]
========================= 14 passed in 0.42s ==========================
测试覆盖:
严格分离命令和查询:
通过构造函数注入依赖,遵循依赖倒置原则:
DocumentRepository)统一的异常处理策略:
使用结构化日志记录关键操作:
使用 DTO 在层之间传输数据:
领域层:
src/domain/vector_search/entities.py - Document, SearchResultsrc/domain/vector_search/repositories.py - DocumentRepositorysrc/domain/vector_search/services.py - HybridSearchServicesrc/domain/vector_search/value_objects.py - Vector, SearchQuerysrc/domain/shared/value_objects.py - EntityId, Timestampsrc/domain/shared/exceptions.py - DomainException应用层共享:
src/application/shared/exceptions.py - ApplicationException, ResourceNotFoundException, ValidationExceptionsrc/application/shared/interfaces.py - CommandHandler, QueryHandler应用层向量搜索:
src/application/vector_search/commands.py - 命令定义src/application/vector_search/queries.py - 查询定义src/application/vector_search/dtos.py - DTO 定义表现层(未来):
基础设施层(未来):
根据任务列表,接下来的任务是:
✅ 所有验收标准已满足:
application/vector_search/handlers.pyCreateDocumentHandlerUpdateDocumentHandlerDeleteDocumentHandlerSearchDocumentsHandlerGetDocumentHandlerTask 5.4 已成功完成。实现了向量搜索应用层的所有命令和查询处理器,遵循 CQRS 模式和依赖注入原则。所有处理器都有完整的文档字符串、异常处理和日志记录。单元测试覆盖了所有主要场景,确保代码质量。
关键成果: