Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidMStraub committed Dec 10, 2024
1 parent 7f094a5 commit 98ff8bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
24 changes: 17 additions & 7 deletions gramps_webapi/api/resources/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gramps_webapi/api/search/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion gramps_webapi/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 98ff8bf

Please sign in to comment.