From b9aaab38e04ac34f9dd7d6d759a951a82ef482f9 Mon Sep 17 00:00:00 2001 From: Nick dos Remedios Date: Thu, 9 Jan 2025 17:24:51 +1100 Subject: [PATCH] #935 Prevent tag|ex appearing in Fix `activeFacetMap` and `activeFacetObj` --- .../ala/biocache/util/QueryFormatUtils.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/au/org/ala/biocache/util/QueryFormatUtils.java b/src/main/java/au/org/ala/biocache/util/QueryFormatUtils.java index 49ef81c1f..5de14af5f 100644 --- a/src/main/java/au/org/ala/biocache/util/QueryFormatUtils.java +++ b/src/main/java/au/org/ala/biocache/util/QueryFormatUtils.java @@ -131,7 +131,7 @@ public Map[] formatSearchQuery(SpatialSearchRequestDTO searchParams, boolean for //Only format the query if it doesn't already supply a formattedQuery. if (forceQueryFormat || StringUtils.isEmpty(searchParams.getFormattedQuery())) { - String [] originalFqs = searchParams.getFq(); + String[] originalFqs = Arrays.copyOf(searchParams.getFq(), searchParams.getFq().length); // copy by value String [] formatted = formatQueryTerm(searchParams.getQ(), searchParams); searchParams.setDisplayString(formatted[0]); @@ -149,9 +149,11 @@ public Map[] formatSearchQuery(SpatialSearchRequestDTO searchParams, boolean for if (searchParams.getFq() != null) { for (int i = 0; i < searchParams.getFq().length; i++) { String fq = searchParams.getFq()[i]; + String fqOriginal = originalFqs[i]; // not altered by `applyFilterTagging()` - if (fq != null && fq.length() > 0) { + if (fq != null && !fq.isEmpty()) { formatted = formatQueryTerm(fq, searchParams); + String[] formattedOriginal = formatQueryTerm(fqOriginal, searchParams); if (StringUtils.isNotEmpty(formatted[1])) { addFormattedFq(new String[]{formatted[1]}, searchParams); @@ -161,21 +163,21 @@ public Map[] formatSearchQuery(SpatialSearchRequestDTO searchParams, boolean for //do not add spatial fields if (originalFqs != null && i < originalFqs.length && !formatted[1].contains(spatialField + ":")) { Facet facet = new Facet(); - facet.setDisplayName(formatted[0]); - String[] fv = fq.split(":"); + facet.setDisplayName(formattedOriginal[0]); + String[] fv = fqOriginal.split(":"); if (fv.length >= 2) { facet.setName(fv[0]); - facet.setValue(fq.substring(fv[0].length() + 1)); + facet.setValue(fqOriginal.substring(fv[0].length() + 1)); } activeFacetMap.put(facet.getName(), facet); // activeFacetMap is based on the assumption that each fq is on different filter so its a [StringKey: Facet] structure // but actually different fqs can use same filter key for example &fq=-month:'11'&fq=-month='12' so we added a new map // activeFacetObj which is [StringKey: List] - String fqKey = parseFQ(fq); + String fqKey = parseFQ(fqOriginal); if (fqKey != null) { - Facet fct = new Facet(fqKey, formatted[0]); // display name is the formatted name, for example '11' to 'November' - fct.setValue(fq); // value in activeFacetMap is the part with key replaced by '', but here is the original fq because front end will need it + Facet fct = new Facet(fqKey, formattedOriginal[0]); // display name is the formatted name, for example '11' to 'November' + fct.setValue(fqOriginal); // value in activeFacetMap is the part with key replaced by '', but here is the original fq because front end will need it List valList = activeFacetObj.getOrDefault(fqKey, new ArrayList<>()); valList.add(fct); activeFacetObj.put(fqKey, valList); @@ -205,7 +207,7 @@ public Map[] formatSearchQuery(SpatialSearchRequestDTO searchParams, boolean for /** * Apply facet tagging and filter exclusions to the search request. * - * Note: due to bug/feature in SOLRJ, the filtered facets are added to + * Note: due to bug/feature in SOLRJ, the excluded facets are added to * the facet pivot list instead of the facet list, otherwise SOLRJ will * ignore the facets with counts greater than totalRecords count, when generating * the facetResults. @@ -219,7 +221,6 @@ private void applyFilterTagging(SpatialSearchRequestDTO searchParams) { List facetList = new ArrayList(); List facetPivotList = new ArrayList(); List fqList = new ArrayList(); - List exList = new ArrayList(); // Get a list of excluded fields List excludedFields = Arrays.stream(searchParams.getFacets()) @@ -236,6 +237,7 @@ private void applyFilterTagging(SpatialSearchRequestDTO searchParams) { } } + // Add the tag syntax to the fqList, if the fq is a facet if (searchParams.getFq() != null) { for (String fq : searchParams.getFq()) { String fqField = org.apache.commons.lang3.StringUtils.substringBefore(fq, ":"); @@ -248,6 +250,7 @@ private void applyFilterTagging(SpatialSearchRequestDTO searchParams) { } } + // Update the searchParams with the new facetPivotList, facetList, and fqList searchParams.setFacetPivots(facetPivotList.toArray(new String[0])); searchParams.setFacets(facetList.toArray(new String[0])); searchParams.setFq(fqList.toArray(new String[0]));