Skip to content

Commit

Permalink
services: Add locale to search params via args
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshamarora1 committed Aug 14, 2024
1 parent 6c484cb commit d7fae4c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions invenio_records_resources/resources/records/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

"""Schemas for parameter parsing."""

from invenio_i18n import get_locale

from flask_resources.parsers import MultiDictSchema
from marshmallow import fields, post_load, validate

Expand All @@ -29,3 +31,9 @@ def facets(self, data, original_data=None, **kwargs):
for k in set(original_data.keys()) - set(data.keys()):
data["facets"][k] = original_data.getlist(k)
return data

@post_load
def inject_locale(self, data, **kwargs):
"""Inject locale from request context into the args."""
data["locale"] = str(get_locale())
return data
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def apply(self, identity, search, params):
raise QuerystringValidationError("Invalid 'suggest' parameter.")

if query_str:
query = parser_cls(identity).parse(query_str)
query = parser_cls(identity).parse(query_str, locale=params.get("locale"))
search = search.query(query)

return search
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, identity=None, extra_params=None, tree_transformer_cls=None):
# fields is not removed from extra params since if given it must be
# used in both querystring and multi match
self._fields = self.extra_params.get("fields") or []
self._dynamic_fields = self.extra_params.pop("dynamic_fields", None) or []

@property
def allow_list(self):
Expand Down Expand Up @@ -117,8 +118,12 @@ def factory(cls, tree_transformer_cls=None, **extra_params):
tree_transformer_cls=tree_transformer_cls,
)

def parse(self, query_str):
def parse(self, query_str, locale=None):
"""Parse the query."""
if locale and self.extra_params.get("fields", None):
self.extra_params["fields"] += [
field.format(locale=locale) for field in self._dynamic_fields
]
try:
# We parse the Lucene query syntax in Python, so we know upfront
# if the syntax is correct before executing it in the search engine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def __init__(self, identity=None, extra_params=None, **kwargs):
super().__init__(identity=identity, extra_params=extra_params)
self.extra_params.setdefault("type", "bool_prefix")

def parse(self, query_str):
def parse(self, query_str, locale=None):
"""Parse the query."""
if locale and self.extra_params.get("fields", None):
self.extra_params["fields"] += [
field.format(locale=locale) for field in self._dynamic_fields
]
return dsl.Q("multi_match", query=query_str, **self.extra_params)

0 comments on commit d7fae4c

Please sign in to comment.