Skip to content

Commit

Permalink
oaiserver: add rebuild index method
Browse files Browse the repository at this point in the history
  • Loading branch information
slint committed Oct 11, 2023
1 parent dfad3ee commit 8641d3b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions invenio_rdm_records/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ def rebuild_index():
rec_service = current_rdm_records.records_service
rec_service.rebuild_index(identity=system_identity)

click.secho("Reindexing OAI sets...", fg="green")
oaipmh_service = current_rdm_records.oaipmh_server_service
oaipmh_service.rebuild_index(identity=system_identity)

click.secho("Reindexed records and vocabularies!", fg="green")


Expand Down
10 changes: 10 additions & 0 deletions invenio_rdm_records/oaiserver/services/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

from flask import current_app
from flask_sqlalchemy import Pagination
from invenio_db import db
from invenio_i18n import lazy_gettext as _
from invenio_oaiserver.models import OAISet
from invenio_oaiserver.percolator import _new_percolator
from invenio_records_resources.services import Service
from invenio_records_resources.services.base import LinksTemplate
from invenio_records_resources.services.base.utils import map_search_params
Expand Down Expand Up @@ -234,3 +236,11 @@ def read_all_formats(self, identity):
self, schema=self.config.metadata_format_schema
),
)

def rebuild_index(self, identity):
"""Rebuild OAI sets percolator index."""
entries = db.session.query(OAISet.spec, OAISet.search_pattern).yield_per(1000)
for spec, search_pattern in entries:
# Creates or updates the OAI set
_new_percolator(spec, search_pattern)
return True
45 changes: 44 additions & 1 deletion tests/services/test_oai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import pytest
from invenio_db import db
from invenio_oaiserver.models import OAISet
from invenio_search import current_search_client
from marshmallow import ValidationError

from invenio_rdm_records.oaiserver.services.config import OAIPMHServerServiceConfig
from invenio_rdm_records.oaiserver.services.errors import OAIPMHSetNotEditable
from invenio_rdm_records.oaiserver.services.services import OAIPMHServerService
from invenio_rdm_records.proxies import current_oaipmh_server_service
from invenio_rdm_records.proxies import (
current_oaipmh_server_service,
current_rdm_records_service,
)


def test_minimal_set_creation_and_edit(running_app, search_clear, minimal_oai_set):
Expand Down Expand Up @@ -74,3 +78,42 @@ def test_reserved_prefixes(running_app, search_clear, minimal_oai_set):
# Must fail as "cds-" is a reserved prefix
with pytest.raises(ValidationError):
service.create(superuser_identity, minimal_oai_set)


def test_rebuild_index(running_app, search_clear, minimal_oai_set):
superuser_identity = running_app.superuser_identity
service = current_oaipmh_server_service

oai_record_index = current_rdm_records_service.record_cls.index._name
percolators_index = f"{oai_record_index}-percolators"
current_search_client.indices.delete(index=percolators_index, ignore=[404])
current_search_client.indices.refresh()

oai_item = service.create(superuser_identity, minimal_oai_set)
oai_dict = oai_item.to_dict()
assert oai_dict["name"] == "name"

current_search_client.indices.refresh()
res = current_search_client.search(index=percolators_index)
assert res["hits"]["total"]["value"] == 1
oai_hit = res["hits"]["hits"][0]
assert oai_hit["_id"] == "oaiset-spec"
assert oai_hit["_source"] == {
"query": {"query_string": {"query": "is_published:true"}}
}

# Delete the percolator index
current_search_client.indices.delete(index=percolators_index)
current_search_client.indices.refresh()
assert current_search_client.indices.exists(index=percolators_index) is False

# Rebuild the OAI sets percolator index
service.rebuild_index(superuser_identity)
current_search_client.indices.refresh(index=percolators_index)

res = current_search_client.search(index=percolators_index)
assert res["hits"]["total"]["value"] == 1
assert oai_hit["_id"] == "oaiset-spec"
assert oai_hit["_source"] == {
"query": {"query_string": {"query": "is_published:true"}}
}

0 comments on commit 8641d3b

Please sign in to comment.