diff --git a/invenio_utilities_tuw/cli/cli.py b/invenio_utilities_tuw/cli/cli.py index 987771ef6433692d22fd14558f20d24a406d2f58..162395a5f88ac3e2bbe613ad8aa1ec6a63670860 100644 --- a/invenio_utilities_tuw/cli/cli.py +++ b/invenio_utilities_tuw/cli/cli.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2020 - 2021 TU Wien. +# Copyright (C) 2020 - 2022 TU Wien. # # Invenio-Utilities-TUW is free software; you can redistribute it and/or # modify it under the terms of the MIT License; see LICENSE file for more @@ -13,6 +13,7 @@ import click from .drafts import drafts from .files import files from .records import records +from .search import search from .users import users @@ -24,4 +25,5 @@ def utilities(): utilities.add_command(drafts) utilities.add_command(files) utilities.add_command(records) +utilities.add_command(search) utilities.add_command(users) diff --git a/invenio_utilities_tuw/cli/search.py b/invenio_utilities_tuw/cli/search.py new file mode 100644 index 0000000000000000000000000000000000000000..9ff24909bbfc242cb471e44820bdb7879fa810d4 --- /dev/null +++ b/invenio_utilities_tuw/cli/search.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2020 - 2022 TU Wien. +# +# Invenio-Utilities-TUW is free software; you can redistribute it and/or +# modify it under the terms of the MIT License; see LICENSE file for more +# details. + +"""Management commands for search indices.""" + +import click +from flask import current_app +from flask.cli import with_appcontext +from invenio_access.permissions import system_identity +from invenio_communities.proxies import current_communities +from invenio_rdm_records.proxies import current_rdm_records +from invenio_requests.proxies import current_events_service, current_requests_service +from invenio_users_resources.proxies import ( + current_groups_service, + current_users_service, +) + + +@click.group() +def search(): + """Management commands for search indices.""" + + +@search.command("reindex") +@with_appcontext +def reindex_everything(): + """Use all known services to reindex their objects in the search engine. + + (Re-)index all relevant entities for the currently known services. + Note that this only performs indexing operations, and no indices are cleared + or torn down and recreated beforehand. + + Current state (regarding known services): App-RDM v9.1.3 + """ + # TODO maybe we could use the 'invenio_records_resources.proxies.service_registry' + + # rebuilding records and drafts + click.echo("indexing records and drafts... ", nl=False) + current_rdm_records.records_service.rebuild_index(system_identity) + click.echo("done.") + + # rebuilding communities and community members + click.echo("indexing communities and community members... ", nl=False) + current_communities.service.rebuild_index(system_identity) + current_communities.service.members.rebuild_index(system_identity) + click.echo("done.") + + # rebuilding users and groups + click.echo("indexing users and groups... ", nl=False) + current_users_service.rebuild_index(system_identity) + current_groups_service.rebuild_index(system_identity) + click.echo("done.") + + # rebuilding requests and request events + click.echo("indexing requests and request events... ", nl=False) + current_requests_service.rebuild_index(system_identity) + current_events_service.rebuild_index(system_identity) + click.echo("done.") + + # rebuilding vocabularies + click.echo("indexing vocabularies...") + current_vocabularies = current_app.extensions["invenio-vocabularies"] + svc_names = (n for n in vars(current_vocabularies) if n.endswith("service")) + services = ((name, getattr(current_vocabularies, name)) for name in svc_names) + for name, service in services: + click.echo(f"> {name}... ", nl=False) + service.rebuild_index(system_identity) + click.echo("done.")