From 5f14a56ed1c8734c6627e78255bbac223f8dd13a Mon Sep 17 00:00:00 2001 From: olaughter Date: Wed, 10 Jan 2024 16:41:44 +0000 Subject: [PATCH] Fix country & region filtering There is currently an issue with keyword filtering with either multiple filters or none. Multiple will create a query looking for all of the items rather than any. Meaning empty queries that should have results. While none will return an error. By way of example. A search parameter with the following keyword filters: ``` {'family_geography': ['AUS', 'BRN' ...]} ``` Produces a yql query like so: ``` and (family_geography contains "AUS") and (family_geography contains "BRN") and ... ``` This functionality is important for the backend region searches. A related issue is the scenario where filters are: ``` {'family_geography': []} ``` This currently results in a query error, because the generated yql would be: ``` and () ``` --- src/cpr_data_access/vespa.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cpr_data_access/vespa.py b/src/cpr_data_access/vespa.py index f1b2474..00d1264 100644 --- a/src/cpr_data_access/vespa.py +++ b/src/cpr_data_access/vespa.py @@ -141,7 +141,8 @@ def build_yql(request: SearchParameters, sensitive: bool = False) -> str: for field_name, values in request.keyword_filters.items(): for value in values: filters.append(f'({field_name} contains "{sanitize(value)}")') - rendered_filters = " and " + " and ".join(filters) + if filters: + rendered_filters = f" and ({' or '.join(filters)})" if request.year_range: start, end = request.year_range