From 98ff8bfb122d4acedf75933ee6ea601f91aec6cd Mon Sep 17 00:00:00 2001 From: David Straub Date: Tue, 10 Dec 2024 22:41:33 +0100 Subject: [PATCH] More fixes --- gramps_webapi/api/resources/search.py | 24 +++++++++++++++++------- gramps_webapi/api/search/indexer.py | 2 +- gramps_webapi/api/tasks.py | 4 +++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/gramps_webapi/api/resources/search.py b/gramps_webapi/api/resources/search.py index 5786b449..e918a723 100644 --- a/gramps_webapi/api/resources/search.py +++ b/gramps_webapi/api/resources/search.py @@ -33,7 +33,12 @@ from ...auth.const import PERM_TRIGGER_REINDEX, PERM_VIEW_PRIVATE from ...const import PRIMARY_GRAMPS_OBJECTS from ..auth import has_permissions, require_permissions -from ..search import get_search_indexer +from ..search import ( + get_search_indexer, + get_semantic_search_indexer, + SearchIndexer, + SemanticSearchIndexer, +) from ..tasks import ( AsyncResult, make_task_response, @@ -44,7 +49,7 @@ from ..util import ( get_db_handle, get_locale_for_language, - get_tree_from_jwt, + get_tree_from_jwt_or_fail, use_args, ) from . import ProtectedResource @@ -73,6 +78,7 @@ def get_object_from_handle( ) -> GrampsObject: """Get the object given a Gramps handle.""" query_method = self.db_handle.method("get_%s_from_handle", class_name) + assert query_method is not None # type checker obj = query_method(handle) if "profile" in args: if class_name == "person": @@ -131,12 +137,16 @@ def get_object_from_handle( ) def get(self, args: Dict): """Get search result.""" - tree = get_tree_from_jwt() - assert tree is not None # mypy; cannot be None if authenticated + tree = get_tree_from_jwt_or_fail() try: - searcher = get_search_indexer(tree, semantic=args["semantic"]) + if args["semantic"]: + searcher: SearchIndexer | SemanticSearchIndexer = ( + get_semantic_search_indexer(tree) + ) + else: + searcher = get_search_indexer(tree) except ValueError: - abort_with_message(500, "Failed initializing semantic search") + abort_with_message(500, "Failed initializing search") if args["semantic"] and args.get("sort"): abort_with_message( 422, "the sort parameter is not allowed with semantic search" @@ -193,7 +203,7 @@ class SearchIndexResource(ProtectedResource): def post(self, args: Dict): """Trigger a reindex.""" require_permissions([PERM_TRIGGER_REINDEX]) - tree = get_tree_from_jwt() + tree = get_tree_from_jwt_or_fail() user_id = get_jwt_identity() if args["full"]: task_func = search_reindex_full diff --git a/gramps_webapi/api/search/indexer.py b/gramps_webapi/api/search/indexer.py index 2606eea7..738cddaa 100644 --- a/gramps_webapi/api/search/indexer.py +++ b/gramps_webapi/api/search/indexer.py @@ -296,7 +296,7 @@ def search( search in private fields. """ search = self.index if include_private else self.index_public - where = {} + where: dict[str, Any] = {} if object_types: where["type"] = {"$in": object_types} if change_op and change_value is not None: diff --git a/gramps_webapi/api/tasks.py b/gramps_webapi/api/tasks.py index ecd9b2db..e2b858ab 100644 --- a/gramps_webapi/api/tasks.py +++ b/gramps_webapi/api/tasks.py @@ -161,7 +161,9 @@ def _search_reindex_incremental( ) -> None: """Run an incremental reindex of the search index.""" if semantic: - indexer = get_semantic_search_indexer(tree) + indexer: SearchIndexer | SemanticSearchIndexer = get_semantic_search_indexer( + tree + ) else: indexer = get_search_indexer(tree) db = get_db_outside_request(