Skip to content

Commit

Permalink
added custom query parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Oct 23, 2024
1 parent dc31e5c commit 9bb883e
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ For the full list of settings and their values, see
https://inveniordm.docs.cern.ch/reference/configuration/.
"""

from copy import deepcopy
from datetime import datetime
import idutils
from marshmallow import validate
Expand All @@ -18,6 +19,14 @@ from invenio_rdm_records.contrib.journal import (
JOURNAL_NAMESPACE,
)
from invenio_records_resources.services.custom_fields import TextCF
from invenio_records_resources.services.records.queryparser import (
FieldValueMapper,
QueryParser,
SearchFieldTransformer,
)
from invenio_communities.communities.records.models import CommunityMetadata
from invenio_db import db
from luqum.tree import Phrase

def _(x): # needed to avoid start time failure with lazy strings
return x
Expand Down Expand Up @@ -398,6 +407,22 @@ RDM_FACETS = {
},
}

# from https://github.com/zenodo/zenodo-rdm/blob/master/site/zenodo_rdm/queryparser.py
def word_doi(node):
"""Quote DOIs."""
if not node.value.startswith("10."):
return node
return Phrase(f'"{node.value}"')

def word_communities(node):
"""Resolve community slugs to IDs."""
slug = node.value
uuid = (
db.session.query(CommunityMetadata.id)
.filter(CommunityMetadata.slug == slug)
.scalar()
)
return Phrase(f'"{uuid}"')

RDM_SEARCH = {
# Supported values from RDM_FACETS
Expand All @@ -411,18 +436,21 @@ RDM_SEARCH = {
"updated-desc",
"updated-asc",
],
"query_parser_cls": QueryParser.factory(
mapping={
"content": "custom_fields.rs\:content_text",
"issn": "custom_fields.journal\:journal.issn",
"doi": FieldValueMapper("pids.doi.identifier", word=word_doi),
"orcid": "metadata.creators.person_or_org.identifiers.identifier",
"ror": "metadata.creators.affiliations.id",
"communities": FieldValueMapper("parent.communities.ids", word=word_communities),
},
tree_transformer_cls=SearchFieldTransformer,
),
}

COMMUNITIES_RECORDS_SEARCH = {
"facets": ["language", "subject"],
"sort": [
"bestmatch",
"newest",
"oldest",
"updated-desc",
"updated-asc",
],
}
COMMUNITIES_RECORDS_SEARCH = deepcopy(RDM_SEARCH)
"""Communities record search config is the same as the main record search."""

# Toggle to show or hide the 'Browse' menu entry for communities.
COMMUNITIES_SHOW_BROWSE_MENU_ENTRY = True
Expand Down

0 comments on commit 9bb883e

Please sign in to comment.