From 12cbd63dbf498a125eda6b26ff630a755805ec73 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 8 Sep 2025 20:10:48 -0300 Subject: [PATCH] Refactor SearchService to improve data retrieval from search results This commit updates the SearchService class to utilize the `get` method for safely accessing fields in the search results. This change enhances the robustness of the code by preventing potential KeyErrors and aligns with best practices for building maintainable async code. Additionally, it simplifies the data extraction process from the search results, improving overall code clarity. --- src/services/search_service.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/services/search_service.py b/src/services/search_service.py index 222d6541..230c052f 100644 --- a/src/services/search_service.py +++ b/src/services/search_service.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Optional +from typing import Any, Dict from agentd.tool_decorator import tool from config.settings import clients, INDEX_NAME, EMBED_MODEL from auth_context import get_auth_context @@ -166,11 +166,11 @@ class SearchService: for hit in results["hits"]["hits"]: chunks.append( { - "filename": hit["_source"]["filename"], - "mimetype": hit["_source"]["mimetype"], - "page": hit["_source"]["page"], - "text": hit["_source"]["text"], - "score": hit["_score"], + "filename": hit["_source"].get("filename"), + "mimetype": hit["_source"].get("mimetype"), + "page": hit["_source"].get("page"), + "text": hit["_source"].get("text"), + "score": hit.get("_score"), "source_url": hit["_source"].get("source_url"), "owner": hit["_source"].get("owner"), "owner_name": hit["_source"].get("owner_name"),