Skip to content

Commit fbd2114

Browse files
authored
New endpoint to trigger search reindex (#339)
1 parent 9f8b7b9 commit fbd2114

File tree

5 files changed

+106
-5
lines changed

5 files changed

+106
-5
lines changed

gramps_webapi/api/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
from .resources.relations import RelationResource, RelationsResource
6262
from .resources.reports import ReportFileResource, ReportResource, ReportsResource
6363
from .resources.repositories import RepositoriesResource, RepositoryResource
64-
from .resources.search import SearchResource
64+
from .resources.search import SearchIndexResource, SearchResource
6565
from .resources.sources import SourceResource, SourcesResource
6666
from .resources.tags import TagResource, TagsResource
6767
from .resources.tasks import TaskResource
@@ -268,6 +268,7 @@ def register_endpt(resource: Type[Resource], url: str, name: str):
268268
)
269269
# Search
270270
register_endpt(SearchResource, "/search/", "search")
271+
register_endpt(SearchIndexResource, "/search/index/", "search_index")
271272

272273
# Config
273274
register_endpt(

gramps_webapi/api/resources/search.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@
2121

2222
from typing import Dict
2323

24-
from flask import current_app
24+
from flask import current_app, Response
2525
from gramps.gen.db.base import DbReadBase
2626
from gramps.gen.errors import HandleError
2727
from gramps.gen.lib.primaryobj import BasicPrimaryObject as GrampsObject
2828
from gramps.gen.utils.grampslocale import GrampsLocale
2929
from webargs import fields, validate
3030

31-
from ...auth.const import PERM_VIEW_PRIVATE
32-
from ..auth import has_permissions
31+
from ...auth.const import PERM_TRIGGER_REINDEX, PERM_VIEW_PRIVATE
32+
from ..auth import has_permissions, require_permissions
33+
from ..tasks import (
34+
make_task_response,
35+
run_task,
36+
search_reindex_incremental,
37+
search_reindex_full,
38+
)
3339
from ..util import get_db_handle, get_locale_for_language, use_args
3440
from . import ProtectedResource
3541
from .emit import GrampsJSONEncoder
@@ -130,3 +136,25 @@ def get(self, args: Dict):
130136
# filter out hits without object (i.e. if handle failed)
131137
hits = [hit for hit in hits if "object" in hit]
132138
return self.response(200, payload=hits or [], args=args, total_items=total)
139+
140+
141+
class SearchIndexResource(ProtectedResource):
142+
"""Resource to trigger a search reindex."""
143+
144+
@use_args(
145+
{
146+
"full": fields.Boolean(load_default=False),
147+
},
148+
location="query",
149+
)
150+
def post(self, args: Dict):
151+
"""Trigger a reindex."""
152+
require_permissions([PERM_TRIGGER_REINDEX])
153+
if args["full"]:
154+
task_func = search_reindex_full
155+
else:
156+
task_func = search_reindex_incremental
157+
task = run_task(task_func)
158+
if task:
159+
return make_task_response(task)
160+
return Response(status=201)

gramps_webapi/api/tasks.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ def search_reindex_full() -> None:
9797
return _search_reindex_full()
9898

9999

100+
@shared_task()
101+
def search_reindex_incremental() -> None:
102+
"""Run an incremental reindex of the search index."""
103+
indexer = current_app.config["SEARCH_INDEXER"]
104+
db = current_app.config["DB_MANAGER"].get_db().db
105+
try:
106+
indexer.search_reindex_incremental(db)
107+
finally:
108+
db.close()
109+
110+
100111
@shared_task()
101112
def import_file(file_name: str, extension: str, delete: bool = True):
102113
"""Import a file."""

gramps_webapi/auth/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
PERM_IMPORT_FILE = "ImportFile"
4444
PERM_VIEW_SETTINGS = "ViewSettings"
4545
PERM_EDIT_SETTINGS = "EditSettings"
46+
PERM_TRIGGER_REINDEX = "TriggerReindex"
4647

4748
PERMISSIONS = {
4849
ROLE_OWNER: {
@@ -59,6 +60,7 @@
5960
PERM_IMPORT_FILE,
6061
PERM_VIEW_SETTINGS,
6162
PERM_EDIT_SETTINGS,
63+
PERM_TRIGGER_REINDEX,
6264
},
6365
ROLE_EDITOR: {
6466
PERM_EDIT_OWN_USER,

gramps_webapi/data/apispec.yaml

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,11 @@ paths:
418418
description: "OK: e-mail successfully sent."
419419
202:
420420
description: "Accepted: e-mail will be sent in the background."
421+
schema:
422+
type: object
423+
properties:
424+
task:
425+
$ref: "#/definitions/TaskReference"
421426
404:
422427
description: "Not found: user does not exist or has no e-mail address."
423428
422:
@@ -5004,7 +5009,7 @@ paths:
50045009
tags:
50055010
- translations
50065011
summary: "Get a translation in a specific language."
5007-
operationId: getTranslation
5012+
operationId: postTranslation
50085013
security:
50095014
- Bearer: []
50105015
parameters:
@@ -5648,6 +5653,38 @@ paths:
56485653
422:
56495654
description: "Unprocessable Entity: Invalid or bad parameter provided."
56505655

5656+
/search/index/:
5657+
post:
5658+
tags:
5659+
- search
5660+
summary: "Trigger a reindex of the search index."
5661+
operationId: reindexSearch
5662+
security:
5663+
- Bearer: []
5664+
parameters:
5665+
- name: full
5666+
in: query
5667+
required: false
5668+
type: boolean
5669+
default: false
5670+
description: "Perform a full or incremental (default) reindex."
5671+
responses:
5672+
201:
5673+
description: "OK: Successful operation."
5674+
202:
5675+
description: "Accepted: reindex will be performed in the background."
5676+
schema:
5677+
type: object
5678+
properties:
5679+
task:
5680+
$ref: "#/definitions/TaskReference"
5681+
401:
5682+
description: "Unauthorized: Missing authorization header."
5683+
403:
5684+
description: "Unauthorized: insufficient permissions."
5685+
422:
5686+
description: "Unprocessable Entity: Invalid or bad parameter provided."
5687+
56515688

56525689
##############################################################################
56535690
# Endpoint - Reports
@@ -6120,6 +6157,11 @@ paths:
61206157
description: "OK: resource created."
61216158
202:
61226159
description: "Accepted: file will be imported in the background."
6160+
schema:
6161+
type: object
6162+
properties:
6163+
task:
6164+
$ref: "#/definitions/TaskReference"
61236165
401:
61246166
description: "Unauthorized: Missing authorization header."
61256167
404:
@@ -9519,3 +9561,20 @@ definitions:
95199561
description: "The object value supporting the fact."
95209562
type: string
95219563
example: "21 years, 8 months, 11 days"
9564+
9565+
9566+
##############################################################################
9567+
# Model - TaskReference
9568+
##############################################################################
9569+
9570+
TaskReference:
9571+
type: object
9572+
properties:
9573+
href:
9574+
description: "The URL of the task detail endpoint."
9575+
type: string
9576+
example: "https://grampsweb.example.com/api/tasks/b9ed86ea-1c6b-400a-bb70-af9cb11bdf13"
9577+
id:
9578+
description: "The unique identifier for a task."
9579+
type: string
9580+
example: "b9ed86ea-1c6b-400a-bb70-af9cb11bdf13"

0 commit comments

Comments
 (0)