From 5d68e827b09a1c758b05fccaf3e2b003f99a9691 Mon Sep 17 00:00:00 2001 From: Pablo Saiz Date: Fri, 1 Dec 2023 15:09:52 +0100 Subject: [PATCH] search: possibility to specify a different query_parser --- invenio_records_rest/facets.py | 19 +++++++++++++++---- invenio_records_rest/views.py | 7 ++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/invenio_records_rest/facets.py b/invenio_records_rest/facets.py index 3102996..b27dbb0 100644 --- a/invenio_records_rest/facets.py +++ b/invenio_records_rest/facets.py @@ -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] diff --git a/invenio_records_rest/views.py b/invenio_records_rest/views.py index d422309..9735788 100644 --- a/invenio_records_rest/views.py +++ b/invenio_records_rest/views.py @@ -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. @@ -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``. @@ -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), @@ -591,6 +594,7 @@ def __init__( item_links_factory=None, record_class=None, indexer_class=None, + search_query_parser=None, **kwargs ): """Constructor.""" @@ -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( @@ -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