From 3a6e5a95560cd747cef23fe034a499f9fd9008bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20T=C3=A1rraga=20Gim=C3=A9nez?= Date: Thu, 5 Dec 2024 10:41:06 +0100 Subject: [PATCH 1/2] solr: implement ORDER (asc and desc) for Solr facets, #TASK-7260, #TASK-5610 --- .../commons/datastore/core/QueryOptions.java | 2 ++ .../datastore/mongodb/MongoDBNativeQuery.java | 13 ++++----- .../datastore/mongodb/MongoDBQueryUtils.java | 9 ++---- .../mongodb/MongoDBCollectionTest.java | 7 +++-- .../datastore/solr/FacetQueryParser.java | 28 +++++++++++++++++++ 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/commons-datastore/commons-datastore-core/src/main/java/org/opencb/commons/datastore/core/QueryOptions.java b/commons-datastore/commons-datastore-core/src/main/java/org/opencb/commons/datastore/core/QueryOptions.java index 3f2b720d..cada68e5 100644 --- a/commons-datastore/commons-datastore-core/src/main/java/org/opencb/commons/datastore/core/QueryOptions.java +++ b/commons-datastore/commons-datastore-core/src/main/java/org/opencb/commons/datastore/core/QueryOptions.java @@ -33,7 +33,9 @@ public class QueryOptions extends ObjectMap { public static final String SORT = "sort"; public static final String ORDER = "order"; + public static final String ASC = "asc"; public static final String ASCENDING = "ascending"; + public static final String DESC = "desc"; public static final String DESCENDING = "descending"; public static final String TIMEOUT = "timeout"; diff --git a/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBNativeQuery.java b/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBNativeQuery.java index b926cb7a..0876bacd 100644 --- a/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBNativeQuery.java +++ b/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBNativeQuery.java @@ -28,10 +28,7 @@ import org.opencb.commons.datastore.core.ComplexTypeConverter; import org.opencb.commons.datastore.core.QueryOptions; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; +import java.util.*; import java.util.concurrent.*; import static org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.getProjection; @@ -180,8 +177,8 @@ public FindIterable nativeFind(ClientSession clientSession, Bson query if (sortObject instanceof Bson) { findIterable.sort(((Bson) sortObject)); } else if (sortObject instanceof String) { - String order = options.getString(QueryOptions.ORDER, "DESC"); - if (order.equalsIgnoreCase(QueryOptions.ASCENDING) || order.equalsIgnoreCase("ASC") + String order = options.getString(QueryOptions.ORDER, QueryOptions.DESC.toUpperCase(Locale.ROOT)); + if (order.equalsIgnoreCase(QueryOptions.ASCENDING) || order.equalsIgnoreCase(QueryOptions.ASC) || order.equals("1")) { findIterable.sort(Sorts.ascending(((String) sortObject))); } else { @@ -199,9 +196,9 @@ public FindIterable nativeFind(ClientSession clientSession, Bson query order = fieldArray[1]; } else if (fieldArray.length == 1) { sortField = field; - order = options.getString(QueryOptions.ORDER, "DESC"); + order = options.getString(QueryOptions.ORDER, QueryOptions.DESC.toUpperCase(Locale.ROOT)); } - if (QueryOptions.ASCENDING.equalsIgnoreCase(order) || "ASC".equalsIgnoreCase(order) + if (QueryOptions.ASCENDING.equalsIgnoreCase(order) || QueryOptions.ASC.equalsIgnoreCase(order) || "1".equals(order)) { sortedList.add(Sorts.ascending(sortField)); } else { diff --git a/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBQueryUtils.java b/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBQueryUtils.java index e33ced35..97f2136e 100644 --- a/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBQueryUtils.java +++ b/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBQueryUtils.java @@ -27,10 +27,7 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -668,8 +665,8 @@ public static Bson getSort(QueryOptions options) { if (sortObject instanceof Bson) { return Aggregates.sort((Bson) sortObject); } else if (sortObject instanceof String) { - String order = options.getString(QueryOptions.ORDER, "DESC"); - if (order.equalsIgnoreCase(QueryOptions.ASCENDING) || order.equalsIgnoreCase("ASC") || order.equals("1")) { + String order = options.getString(QueryOptions.ORDER, QueryOptions.DESC.toUpperCase(Locale.ROOT)); + if (order.equalsIgnoreCase(QueryOptions.ASCENDING) || order.equalsIgnoreCase(QueryOptions.ASC) || order.equals("1")) { return Aggregates.sort(Sorts.ascending((String) sortObject)); } else { return Aggregates.sort(Sorts.descending((String) sortObject)); diff --git a/commons-datastore/commons-datastore-mongodb/src/test/java/org/opencb/commons/datastore/mongodb/MongoDBCollectionTest.java b/commons-datastore/commons-datastore-mongodb/src/test/java/org/opencb/commons/datastore/mongodb/MongoDBCollectionTest.java index 377d610c..cbe96ac5 100644 --- a/commons-datastore/commons-datastore-mongodb/src/test/java/org/opencb/commons/datastore/mongodb/MongoDBCollectionTest.java +++ b/commons-datastore/commons-datastore-mongodb/src/test/java/org/opencb/commons/datastore/mongodb/MongoDBCollectionTest.java @@ -164,7 +164,7 @@ public void testDistinct() throws Exception { public void testSortOrder() throws Exception { Document query = new Document(); QueryOptions queryOptions = new QueryOptions(QueryOptions.LIMIT, 10).append(QueryOptions.SORT, "number") - .append(QueryOptions.ORDER, "asc"); + .append(QueryOptions.ORDER, QueryOptions.ASC); List result = mongoDBCollection.find(query, queryOptions).getResults(); assertEquals(0L, result.get(0).get("number")); } @@ -173,8 +173,9 @@ public void testSortOrder() throws Exception { public void testMultipleSortOrder() throws Exception { Document query = new Document(); QueryOptions queryOptions = new QueryOptions(QueryOptions.LIMIT, 500) - .append(QueryOptions.SORT, Arrays.asList("age:ASC", "number:DESC")) - .append(QueryOptions.ORDER, "asc"); + .append(QueryOptions.SORT, Arrays.asList("age:" + QueryOptions.ASC.toUpperCase(Locale.ROOT), + "number:" + QueryOptions.DESC.toUpperCase(Locale.ROOT))) + .append(QueryOptions.ORDER, QueryOptions.ASC); int age = 0; long number = Long.MAX_VALUE; List result = mongoDBCollection.find(query, queryOptions).getResults(); diff --git a/commons-datastore/commons-datastore-solr/src/main/java/org/opencb/commons/datastore/solr/FacetQueryParser.java b/commons-datastore/commons-datastore-solr/src/main/java/org/opencb/commons/datastore/solr/FacetQueryParser.java index 3ae38b44..075e167b 100644 --- a/commons-datastore/commons-datastore-solr/src/main/java/org/opencb/commons/datastore/solr/FacetQueryParser.java +++ b/commons-datastore/commons-datastore-solr/src/main/java/org/opencb/commons/datastore/solr/FacetQueryParser.java @@ -25,6 +25,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.opencb.commons.datastore.core.QueryOptions.*; + public class FacetQueryParser { public static final String FACET_SEPARATOR = ";"; @@ -252,6 +254,7 @@ private Map parseFacet(String facet, QueryOptions options) throw auxMap.put("type", "terms"); setTermLimit(matcher.group(3), options, auxMap); setTermSkip(options, auxMap); + setTermOrder(options, auxMap); Map tmpMap = new HashMap<>(); tmpMap.put(matcher.group(1), auxMap); @@ -261,6 +264,7 @@ private Map parseFacet(String facet, QueryOptions options) throw outputMap.put("type", "terms"); setTermLimit(matcher.group(3), options, outputMap); setTermSkip(options, outputMap); + setTermOrder(options, outputMap); } } else { throw new Exception("Invalid categorical facet: " + facet); @@ -290,6 +294,30 @@ private void setTermSkip(QueryOptions options, Map map) { } } + private void setTermOrder(QueryOptions options, Map map) throws Exception { + if (options.containsKey(QueryOptions.ORDER)) { + String order = options.getString(QueryOptions.ORDER); + switch (order.toUpperCase(Locale.ROOT)) { + case ASC: + case ASCENDING: { + map.put("sort", "count asc"); + break; + } + case DESC: + case DESCENDING: { + map.put("sort", "count desc"); + break; + } + default: { + throw new Exception("Invalid order value: '" + order + "'. Valid values are: " + + StringUtils.join(Arrays.asList(ASC, ASCENDING, DESC, DESCENDING))); + } + } + } else { + map.put("sort", "count desc"); + } + } + private void parseNestedFacet(String nestedFacet, QueryOptions options, Map jsonFacet) throws Exception { String[] split = nestedFacet.split(NESTED_FACET_SEPARATOR); From 6f74a1f46916573770c9bc399241bd323cfc2bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20T=C3=A1rraga=20Gim=C3=A9nez?= Date: Tue, 14 Jan 2025 11:26:57 +0100 Subject: [PATCH 2/2] datastore: fix order option in the facet query parser, #TASK-7260, #TASK-5610 --- .../org/opencb/commons/datastore/solr/FacetQueryParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commons-datastore/commons-datastore-solr/src/main/java/org/opencb/commons/datastore/solr/FacetQueryParser.java b/commons-datastore/commons-datastore-solr/src/main/java/org/opencb/commons/datastore/solr/FacetQueryParser.java index 075e167b..f366ffac 100644 --- a/commons-datastore/commons-datastore-solr/src/main/java/org/opencb/commons/datastore/solr/FacetQueryParser.java +++ b/commons-datastore/commons-datastore-solr/src/main/java/org/opencb/commons/datastore/solr/FacetQueryParser.java @@ -297,7 +297,7 @@ private void setTermSkip(QueryOptions options, Map map) { private void setTermOrder(QueryOptions options, Map map) throws Exception { if (options.containsKey(QueryOptions.ORDER)) { String order = options.getString(QueryOptions.ORDER); - switch (order.toUpperCase(Locale.ROOT)) { + switch (order.toLowerCase(Locale.ROOT)) { case ASC: case ASCENDING: { map.put("sort", "count asc");