From a40934b16647c74cdc9479f5572a3f6711c2559e Mon Sep 17 00:00:00 2001 From: adam-collins Date: Fri, 9 Aug 2024 16:33:05 +1000 Subject: [PATCH] #911 fix streaming sort (#912) #911 fix streaming sort. --- build.gradle | 2 +- .../ala/biocache/dao/SolrIndexDAOImpl.java | 30 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index e8d5c0921..8e01ec8eb 100644 --- a/build.gradle +++ b/build.gradle @@ -40,7 +40,7 @@ repositories { } group = 'au.org.ala' -version = '3.5.0-SNAPSHOT' +version = '3.5.1-SNAPSHOT' boolean inplace = false diff --git a/src/main/java/au/org/ala/biocache/dao/SolrIndexDAOImpl.java b/src/main/java/au/org/ala/biocache/dao/SolrIndexDAOImpl.java index 1b6dad2bf..1a32c580e 100644 --- a/src/main/java/au/org/ala/biocache/dao/SolrIndexDAOImpl.java +++ b/src/main/java/au/org/ala/biocache/dao/SolrIndexDAOImpl.java @@ -1175,17 +1175,39 @@ private ModifiableSolrParams buildSearchExpr(SolrQuery query) { } } } + if (StringUtils.isNotEmpty(query.getFields())) { solrParams.set("fl", StringUtils.join(fieldMappingUtil.translateFieldArray(query.getFields().split(",")), ",")); } else { solrParams.set("fl", "id"); } - // The sort field does not need to be present in the field list. - if (StringUtils.isEmpty(query.getSortField())) { - solrParams.set("sort", "id asc"); - } else { + // The sort field is required, sometimes. Testing suggests that this depends on the solrClient class. + // 1. use the query specified sort, without checking if it is in the list of fields + // 2. use the first non-multivalue field + // 3. use "id asc" and append "id" to the fl if it is missing + // + // Handle the error for some solrClient classes when the default query sort is "score asc" by excluding it + if (StringUtils.isNotEmpty(query.getSortField()) && !"score asc".equals(query.getSortField())) { solrParams.set("sort", query.getSortField()); + } else { + String [] fl = solrParams.get("fl").split(","); + String nonMultivalueField = null; + for (String field : fl) { + IndexFieldDTO f = indexFieldMap.get(field); + if (f != null && !f.isMultivalue()) { + nonMultivalueField = f.getName(); + break; + } + } + if (nonMultivalueField != null) { + solrParams.set("sort", nonMultivalueField + " asc"); + } else { + solrParams.set("sort", "id asc"); + + // append "id" to field list + solrParams.set("fl", solrParams.get("fl") + ",id"); + } } String qt = "/export";