Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 103 additions & 56 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,18 @@ from invenio_rdm_records.checks import requests as checks_requests
from invenio_rdm_records.config import (always_valid, RDM_RECORDS_PERSONORG_SCHEMES,
RDM_RECORDS_IDENTIFIERS_SCHEMES as RDM_RECORDS_RELATED_IDENTIFIERS_SCHEMES
)
from invenio_records_resources.services.records.queryparser import (
QueryParser,
SearchFieldTransformer,
)
from invenio_records_resources.services.records.queryparser import QueryParser
from invenio_rdm_records.proxies import current_rdm_records_service as record_service
from invenio_rdm_records.services.components import DefaultRecordsComponents
from invenio_rdm_records.services.components.vcs import VCSComponent
from invenio_rdm_records.services.vcs.release import RDMVCSRelease
from invenio_rdm_records.config import (
RDM_PERSISTENT_IDENTIFIERS,
RDM_PARENT_PERSISTENT_IDENTIFIERS,
RDM_SEARCH
RDM_SEARCH,
RDM_SEARCH_DRAFTS,
)
from cds_rdm.search import DEFAULT_SEARCH_FIELDS, CDSSearchFieldTransformer
from invenio_rdm_records.notifications.vcs import (
RepositoryReleaseCommunityRequiredNotificationBuilder,
RepositoryReleaseCommunitySubmittedNotificationBuilder,
Expand Down Expand Up @@ -212,7 +211,6 @@ APP_RDM_ROUTES["index"] = ("/", frontpage_view_function)
COMMUNITIES_PERMISSION_POLICY = CDSCommunitiesPermissionPolicy
COMMUNITIES_ADMINISTRATION_DISABLED = False
COMMUNITIES_ALLOW_RESTRICTED = True
COMMUNITIES_RECORDS_SEARCH = deepcopy(RDM_SEARCH)
COMMUNITIES_COLLECTIONS_ENABLED = True

# Invenio-Records-Resources
Expand Down Expand Up @@ -638,59 +636,108 @@ CDS_CERN_SCIENTIFIC_RESOURCE_TYPES = {
Resource types that should be included in the CERN Scientific community.
"""

CDS_SEARCH_ALIASES = {
"accelerator": "custom_fields.cern\\:accelerators.title.en",
"accelerator.id": "custom_fields.cern\\:accelerators.id",
"accelerator.title": "custom_fields.cern\\:accelerators.title.en",
"accelerators": "custom_fields.cern\\:accelerators.title.en",
"accelerators.id": "custom_fields.cern\\:accelerators.id",
"accelerators.title": "custom_fields.cern\\:accelerators.title.en",
"experiment": "custom_fields.cern\\:experiments.title.en",
"experiment.id": "custom_fields.cern\\:experiments.id",
"experiment.title": "custom_fields.cern\\:experiments.title.en",
"experiments": "custom_fields.cern\\:experiments.title.en",
"experiments.id": "custom_fields.cern\\:experiments.id",
"experiments.title": "custom_fields.cern\\:experiments.title.en",
"department": "custom_fields.cern\\:departments.title.en",
"department.id": "custom_fields.cern\\:departments.id",
"department.title": "custom_fields.cern\\:departments.title.en",
"departments": "custom_fields.cern\\:departments.title.en",
"departments.id": "custom_fields.cern\\:departments.id",
"departments.title": "custom_fields.cern\\:departments.title.en",
"programme": "custom_fields.cern\\:programmes.title.en",
"programme.id": "custom_fields.cern\\:programmes.id",
"programme.title": "custom_fields.cern\\:programmes.title.en",
"programmes": "custom_fields.cern\\:programmes.title.en",
"programmes.id": "custom_fields.cern\\:programmes.id",
"programmes.title": "custom_fields.cern\\:programmes.title.en",
"project": "custom_fields.cern\\:projects",
"projects": "custom_fields.cern\\:projects",
"study": "custom_fields.cern\\:studies",
"studies": "custom_fields.cern\\:studies",
"identifier": "metadata.identifiers.identifier",
"cdsrn": "metadata.identifiers.identifier",
"report_number": "metadata.identifiers.identifier",
"inspire": "metadata.related_identifiers.identifier",
"inis": "metadata.related_identifiers.identifier",
"indico": "metadata.related_identifiers.identifier",
"cds": "metadata.identifiers.identifier",
"aleph": "metadata.identifiers.identifier",
"doi": "pids.doi.identifier",
"language": "metadata.languages.id",
"languages": "metadata.languages.id",
"title": "metadata.title",
"publisher": "metadata.publisher",
"description": "metadata.description",
"publication_date": "metadata.publication_date",
"creator": "metadata.creators.person_or_org.name",
"author": "metadata.creators.person_or_org.name",
"creators": "metadata.creators.person_or_org.name",
"authors": "metadata.creators.person_or_org.name",
"contributor": "metadata.contributors.person_or_org.name",
"contributors": "metadata.contributors.person_or_org.name",
"affiliation": "metadata.creators.affiliations.name",
"affiliations": "metadata.creators.affiliations.name",
"subject": "metadata.subjects.subject",
"subjects": "metadata.subjects.subject",
"type": "metadata.resource_type.title.en",
"resource_type": "metadata.resource_type.title.en",
"resource_type.id": "metadata.resource_type.id",
"community": "parent.communities.entries.metadata.title",
"communities": "parent.communities.entries.metadata.title",
"community.id": "parent.communities.entries.slug",
"communities.id": "parent.communities.entries.slug",
"apprn": "metadata.identifiers.identifier",
"edms": "metadata.related_identifiers.identifier",
"hal": "metadata.related_identifiers.identifier",
"archive": "metadata.related_identifiers.identifier",
"beam": "custom_fields.cern\\:beams.title.en",
"beams": "custom_fields.cern\\:beams.title.en",
"committee": "custom_fields.cern\\:committees.title.en",
"committees": "custom_fields.cern\\:committees.title.en",
"facility": "custom_fields.cern\\:facilities",
"facilities": "custom_fields.cern\\:facilities",
"meeting": "custom_fields.meeting\\:meeting.title",
}
"""Search shortcuts (``alias:value``) available in the record search bar.

Documented for users in docs-cds-rdm, docs/search/search.md: keep both in sync.
"""

_CDS_SEARCH_PARSER = QueryParser.factory(
fields=DEFAULT_SEARCH_FIELDS,
# Skip fields a word can't be parsed for (e.g. dates) instead of failing.
lenient=True,
# Keep the upstream restricted terms (internal_notes.*, _exists_), which only
# system users may search, and add the CDS aliases on top.
mapping={
**RDM_SEARCH["query_parser_cls"].keywords["extra_params"]["mapping"],
**CDS_SEARCH_ALIASES,
},
tree_transformer_cls=CDSSearchFieldTransformer,
)

RDM_SEARCH = {
**deepcopy(RDM_SEARCH),
"query_parser_cls": QueryParser.factory(
mapping={
"accelerator": "custom_fields.cern\:accelerators.title.en",
"accelerator.id": "custom_fields.cern\:accelerators.id",
"accelerator.title": "custom_fields.cern\:accelerators.title.en",
"accelerators": "custom_fields.cern\:accelerators.title.en",
"accelerators.id": "custom_fields.cern\:accelerators.id",
"accelerators.title": "custom_fields.cern\:accelerators.title.en",
"experiment": "custom_fields.cern\:experiments.title.en",
"experiment.id": "custom_fields.cern\:experiments.id",
"experiment.title": "custom_fields.cern\:experiments.title.en",
"experiments": "custom_fields.cern\:experiments.title.en",
"experiments.id": "custom_fields.cern\:experiments.id",
"experiments.title": "custom_fields.cern\:experiments.title.en",
"department": "custom_fields.cern\:departments.title.en",
"department.id": "custom_fields.cern\:departments.id",
"department.title": "custom_fields.cern\:departments.title.en",
"departments": "custom_fields.cern\:departments.title.en",
"departments.id": "custom_fields.cern\:departments.id",
"departments.title": "custom_fields.cern\:departments.title.en",
"programme": "custom_fields.cern\:programmes.title.en",
"programme.id": "custom_fields.cern\:programmes.id",
"programme.title": "custom_fields.cern\:programmes.title.en",
"programmes": "custom_fields.cern\:programmes.title.en",
"programmes.id": "custom_fields.cern\:programmes.id",
"programmes.title": "custom_fields.cern\:programmes.title.en",
"project": "custom_fields.cern\:projects",
"projects": "custom_fields.cern\:projects",
"study": "custom_fields.cern\:studies",
"studies": "custom_fields.cern\:studies",
"identifier": "metadata.identifiers.identifier",
"cdsrn": "metadata.identifiers.identifier",
"report_number": "metadata.identifiers.identifier",
"inspire": "metadata.related_identifiers.identifier",
"inis": "metadata.related_identifiers.identifier",
"indico": "metadata.related_identifiers.identifier",
"cds": "metadata.identifiers.identifier",
"aleph": "metadata.identifiers.identifier",
"doi": "pids.doi.identifier",
"language": "metadata.languages.id",
"languages": "metadata.languages.id",
"title": "metadata.title",
"publisher": "metadata.publisher",
"description": "metadata.description",
"publication_date": "metadata.publication_date",
"creator": "metadata.creators.person_or_org.name",
"creators": "metadata.creators.person_or_org.name",
"query_parser_cls": _CDS_SEARCH_PARSER,
}

},
tree_transformer_cls=SearchFieldTransformer,
),
# Defined after RDM_SEARCH so community record pages search the same way.
COMMUNITIES_RECORDS_SEARCH = deepcopy(RDM_SEARCH)

RDM_SEARCH_DRAFTS = {
**deepcopy(RDM_SEARCH_DRAFTS),
"query_parser_cls": _CDS_SEARCH_PARSER,
}

RDM_PERMISSION_POLICY = CDSRDMRecordPermissionPolicy
Expand Down
12 changes: 12 additions & 0 deletions site/cds_rdm/search/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2026 CERN.
#
# CDS-RDM is free software; you can redistribute it and/or modify it under
# the terms of the GPL-2.0 License; see LICENSE file for more details.

"""CDS-RDM record search customisations."""

from .queryparser import DEFAULT_SEARCH_FIELDS, CDSSearchFieldTransformer

__all__ = ("CDSSearchFieldTransformer", "DEFAULT_SEARCH_FIELDS")
128 changes: 128 additions & 0 deletions site/cds_rdm/search/queryparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2026 CERN.
#
# CDS-RDM is free software; you can redistribute it and/or modify it under
# the terms of the GPL-2.0 License; see LICENSE file for more details.

"""Search configuration for the CDS record search bar.

Used with the upstream ``QueryParser`` (see ``invenio.cfg``)::

QueryParser.factory(
fields=DEFAULT_SEARCH_FIELDS,
lenient=True,
mapping={...},
tree_transformer_cls=CDSSearchFieldTransformer,
)
"""

import re

from invenio_records_resources.services.records.queryparser import (
SearchFieldTransformer,
)
from luqum.tree import (
AndOperation,
Group,
OrOperation,
Phrase,
Plus,
Prohibit,
Word,
)

DEFAULT_SEARCH_FIELDS = [
# Mirrors ``index.query.default_field`` of the upstream record mapping
# (invenio-rdm-records, rdmrecords/records/record-v7.0.0.json), with boosts.
"id^10",
"metadata.title^10",
"metadata.title.original^10",
"metadata.additional_titles.title^5",
"metadata.creators.person_or_org.name^5",
"metadata.creators.person_or_org.family_name^5",
"metadata.creators.person_or_org.given_name^2",
"metadata.creators.affiliations.name",
"metadata.contributors.person_or_org.name^2",
"metadata.contributors.person_or_org.family_name^2",
"metadata.contributors.person_or_org.given_name",
"metadata.contributors.affiliations.name",
"metadata.identifiers.identifier^10",
"metadata.subjects.subject^3",
"metadata.description",
"metadata.additional_descriptions.description",
"metadata.contact",
"metadata.formats",
"metadata.funding.award.identifiers.identifier",
"metadata.funding.award.acronym.text",
"metadata.funding.award.number",
"metadata.funding.funder.name",
"metadata.locations.features.place",
"metadata.locations.features.description",
"metadata.publication_date",
"metadata.publisher",
"metadata.version",
"metadata.dates.description",
"metadata.references.reference",
# Not in the upstream default fields.
"pids.doi.identifier^10",
"metadata.related_identifiers.identifier^3",
"metadata.resource_type.title.en^3",
"parent.communities.entries.metadata.title^2",
"custom_fields.cern:experiments.title.en^2",
"custom_fields.cern:accelerators.title.en^2",
"custom_fields.cern:departments.title.en",
"custom_fields.cern:programmes.title.en",
"custom_fields.cern:beams.title.en",
"custom_fields.cern:committees.title.en",
"custom_fields.cern:projects",
"custom_fields.cern:studies",
"custom_fields.cern:facilities",
"custom_fields.meeting:meeting.title",
"custom_fields.journal:journal.title",
"custom_fields.thesis:thesis.university",
]
"""Fields searched by a query without field qualifiers, with boosts.

Never use wildcards here: ``*`` would also search fields restricted to
administrators, such as ``internal_notes``.
"""

# A word the analyzer splits into several tokens: report numbers, DOIs,
# hyphenated words. Not words with wildcards or escapes.
_COMPOUND_WORD_RE = re.compile(r"^[^*?\\\"]+[-/][^*?\\\"]+$")


class CDSSearchFieldTransformer(SearchFieldTransformer):
"""Field aliases (upstream), plus: every word must match.

OpenSearch's ``default_operator: AND`` is applied per field when the fields
use different analyzers: ``thesis Mobius`` would need both words in the
same field and match nothing. An explicit ``AND`` lets each word match in
any field.
"""

def visit_unknown_operation(self, node, context):
"""Turn ``a b`` into ``a AND b``, unless ``+``/``-`` say what is required."""
children = list(self.clone_children(node, node.clone_item(), context))
if any(isinstance(child, (Plus, Prohibit)) for child in children):
new_node = node.clone_item()
new_node.children = children
yield new_node
return
# "a b OR c" is "a (b OR c)": keep the group, OpenSearch would read
# "a AND b OR c" as "+a +b c".
yield AndOperation(
*(Group(c) if isinstance(c, OrOperation) else c for c in children)
)

def visit_word(self, node, context):
"""Search a word made of several tokens as a phrase.

``CERN-THESIS-2021-078`` must match as a whole, not any of its parts,
and the ``/`` of a DOI would otherwise start a regular expression.
"""
for new_node in super().visit_word(node, context):
if isinstance(new_node, Word) and _COMPOUND_WORD_RE.match(new_node.value):
new_node = Phrase(f'"{new_node.value}"')
yield new_node
Loading
Loading