Skip to content

Commit

Permalink
search: possibility to specify a different query_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
psaiz committed Dec 4, 2023
1 parent 945a870 commit 5d68e82
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 15 additions & 4 deletions invenio_records_rest/facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,32 @@


def nested_filter(field, subfield):
"""Create a nested filter. Similar to the https://github.com/inveniosoftware/invenio-records-resources/blob/master/invenio_records_resources/services/records/facets/facets.py#L94"""
"""Create a nested filter.
Similar to the https://github.com/inveniosoftware/invenio-records-resources/blob/master/invenio_records_resources/services/records/facets/facets.py#L94
"""

def inner(values):
top_level = []
queries = []
for value in values:
subvalues = value.split("::")
if len(subvalues)>1:
queries.append(dsl.Q("bool", must=[dsl.Q("term", **{field: subvalues[0]}), dsl.Q("term", **{subfield: subvalues[1]})]))
if len(subvalues) > 1:
queries.append(
dsl.Q(
"bool",
must=[
dsl.Q("term", **{field: subvalues[0]}),
dsl.Q("term", **{subfield: subvalues[1]}),
],
)
)
else:
top_level.append(value)
if len(top_level):
queries.append(dsl.Q("terms", **{field: top_level}))

if len(queries)>1:
if len(queries) > 1:
return dsl.Q("bool", should=queries)
return queries[0]

Expand Down
7 changes: 6 additions & 1 deletion invenio_records_rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def create_url_rules(
links_factory_imp=None,
suggesters=None,
default_endpoint_prefix=None,
search_query_parser=None,
):
"""Create Werkzeug URL rules.
Expand Down Expand Up @@ -254,6 +255,7 @@ def create_url_rules(
:param search_factory_imp: Factory to parse queries.
:param links_factory_imp: Factory for record links generation.
:param suggesters: Suggester fields configuration.
:param search_query_parser: Function that implements the query parser
:returns: a list of dictionaries with can each be passed as keywords
arguments to ``Blueprint.add_url_rule``.
Expand Down Expand Up @@ -325,6 +327,7 @@ def links_factory(pid, record=None, **kwargs):
),
item_links_factory=links_factory,
record_class=record_class,
search_query_parser=search_query_parser,
)
item_view = RecordResource.as_view(
RecordResource.view_name.format(endpoint),
Expand Down Expand Up @@ -591,6 +594,7 @@ def __init__(
item_links_factory=None,
record_class=None,
indexer_class=None,
search_query_parser=None,
**kwargs
):
"""Constructor."""
Expand Down Expand Up @@ -623,6 +627,7 @@ def __init__(
self.loaders = record_loaders or current_records_rest.loaders
self.record_class = record_class or Record
self.indexer_class = indexer_class
self.search_query_parser = search_query_parser

@need_record_permission("list_permission_factory")
@use_paginate_args(
Expand All @@ -647,7 +652,7 @@ def get(self, pagination=None, **kwargs):
search = search[pagination["from_idx"] : pagination["to_idx"]]
search = search.extra(track_total_hits=True)

search, qs_kwargs = self.search_factory(search)
search, qs_kwargs = self.search_factory(search, self.search_query_parser)
urlkwargs.update(qs_kwargs)

# Execute search
Expand Down

0 comments on commit 5d68e82

Please sign in to comment.