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 10, 2023
1 parent ac008d7 commit 1061866
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
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
14 changes: 14 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,15 @@ 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(1_000)
.yield_per(1000)
)
for spec, search_pattern in entries:
# Creates or updates the OAI set
_new_percolator(spec, search_pattern)
return True
40 changes: 40 additions & 0 deletions tests/services/test_oai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import pytest
from invenio_db import db
from invenio_oaiserver.models import OAISet
from invenio_oaiserver.percolator import _delete_percolator
from invenio_search import current_search_client
from marshmallow import ValidationError

from invenio_rdm_records.oaiserver.services.config import OAIPMHServerServiceConfig
Expand Down Expand Up @@ -74,3 +76,41 @@ 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
percolators_index = "rdmrecords-records-record-v6.0.0-percolators"

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(
index="rdmrecords-records-record-v6.0.0-percolators"
)
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
_delete_percolator(oai_dict["spec"], oai_dict["search_pattern"])
current_search_client.indices.refresh(index=percolators_index)

res = current_search_client.search(index=percolators_index)
assert res["hits"]["total"]["value"] == 0

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 1061866

Please sign in to comment.