From ca511f908b24c1cbccc8a92559598277b1ab6eb0 Mon Sep 17 00:00:00 2001 From: Darrel O'Pry Date: Wed, 8 Mar 2023 14:22:24 -0500 Subject: [PATCH] WIP feat: add search operator and fields arguments this will allow us to implement more specific query functionality. This is a trial balloon to see if it still passes tests, before updating all the queries. --- grapple/utils.py | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/grapple/utils.py b/grapple/utils.py index 51f8ef0b..02a02138 100644 --- a/grapple/utils.py +++ b/grapple/utils.py @@ -3,6 +3,7 @@ from wagtail.models import Site from wagtail.search.index import class_is_indexed from wagtail.search.models import Query +from wagtail.search.utils import parse_query_string from .settings import grapple_settings from .types.structures import BasePaginatedType, PaginationType @@ -61,6 +62,8 @@ def resolve_queryset( id=None, order=None, collection=None, + search_operator="and", + search_fields=None, **kwargs, ): """ @@ -111,7 +114,14 @@ def resolve_queryset( query = Query.get(search_query) query.add_hit() - qs = qs.search(search_query, order_by_relevance=order_by_relevance) + filters, parsed_query = parse_query_string(search_query, search_operator) + + qs = qs.search( + parsed_query, + order_by_relevance=order_by_relevance, + operator=search_operator, + fields=search_fields, + ) return _sliced_queryset(qs, limit, offset) @@ -150,7 +160,16 @@ def get_paginated_result(qs, page, per_page): def resolve_paginated_queryset( - qs, info, page=None, per_page=None, search_query=None, id=None, order=None, **kwargs + qs, + info, + page=None, + per_page=None, + id=None, + order=None, + search_query=None, + search_operator="and", + search_fields=None, + **kwargs, ): """ Add page, per_page and search capabilities to the query. This contains @@ -164,11 +183,16 @@ def resolve_paginated_queryset( :type id: int :param per_page: The maximum number of items to include on a page. :type per_page: int + :param order: Order the query set using the Django QuerySet order_by format. + :type order: str :param search_query: Using Wagtail search, exclude objects that do not match the search query. :type search_query: str - :param order: Order the query set using the Django QuerySet order_by format. - :type order: str + :param search_operator: The operator to use when combining search terms. + Defaults to "and". + :type search_operator: "and" | "or" + :param search_fields: A list of fields to search. Defaults to all fields. + :type search_fields: list """ page = int(page or 1) per_page = min( @@ -196,7 +220,14 @@ def resolve_paginated_queryset( query = Query.get(search_query) query.add_hit() - qs = qs.search(search_query, order_by_relevance=order_by_relevance) + filters, parsed_query = parse_query_string(search_query, search_operator) + + qs = qs.search( + parsed_query, + order_by_relevance=order_by_relevance, + operator=search_operator, + fields=search_fields, + ) return get_paginated_result(qs, page, per_page)