From 9243ebb6fc8bf18b244038e365c13482bcc2d3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20T=C3=A1rraga=20Gim=C3=A9nez?= Date: Fri, 31 Jan 2025 21:41:28 +0100 Subject: [PATCH] datastore: fix MongoDB facets when combining multiple fields, #TASK-7151, #TASK-7134 --- .../MongoDBDocumentToFacetFieldsConverter.java | 7 ++++++- .../datastore/mongodb/MongoDBQueryUtils.java | 2 +- .../datastore/mongodb/MongoDBCollectionTest.java | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBDocumentToFacetFieldsConverter.java b/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBDocumentToFacetFieldsConverter.java index e4783ebc..b71e6c13 100644 --- a/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBDocumentToFacetFieldsConverter.java +++ b/commons-datastore/commons-datastore-mongodb/src/main/java/org/opencb/commons/datastore/mongodb/MongoDBDocumentToFacetFieldsConverter.java @@ -26,9 +26,12 @@ public List convertToDataModelType(Document document) { List documentValues = (List) entry.getValue(); if (key.endsWith(COUNTS_SUFFIX) || key.endsWith(FACET_ACC_SUFFIX) || key.endsWith(YEAR_SUFFIX) || key.endsWith(MONTH_SUFFIX) || key.endsWith(DAY_SUFFIX)) { + facetFieldName = key.split(SEPARATOR)[0].replace(TO_REPLACE_DOTS, "."); + List buckets = new ArrayList<>(documentValues.size()); long total = 0; for (Document documentValue : documentValues) { + long counter = documentValue.getInteger(count.name()); String bucketValue = ""; Object internalIdValue = documentValue.get(INTERNAL_ID); @@ -41,6 +44,9 @@ public List convertToDataModelType(Document document) { bucketValue = internalIdValue.toString(); } else if (internalIdValue instanceof Document) { bucketValue = StringUtils.join(((Document) internalIdValue).values(), SEPARATOR); + if (key.endsWith(COUNTS_SUFFIX)) { + facetFieldName = key.substring(0, key.indexOf(COUNTS_SUFFIX)); + } } List bucketFacetFields = null; @@ -66,7 +72,6 @@ public List convertToDataModelType(Document document) { buckets.add(new FacetField.Bucket(bucketValue, counter, bucketFacetFields)); total += counter; } - facetFieldName = key.split(SEPARATOR)[0].replace(TO_REPLACE_DOTS, "."); FacetField facetField = new FacetField(facetFieldName, total, buckets); facetField.setAggregationName(count.name()); if (key.endsWith(YEAR_SUFFIX) || key.endsWith(MONTH_SUFFIX) || key.endsWith(DAY_SUFFIX)) { 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 c4b726a2..c60dee0c 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 @@ -714,7 +714,7 @@ private static List createFacet(Bson query, List facetFields) { includeFields.add(field); } facet = new Facet( - facetField.replace(",", SEPARATOR), // + COUNTS_SUFFIX, + facetField.replace(",", SEPARATOR) + COUNTS_SUFFIX, group(fields, Accumulators.sum(count.name(), 1)) ); } else { 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 2f54143b..c8d440f4 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 @@ -1093,6 +1093,20 @@ public void testFacetInvalidAccumulator() { mongoDBCollection.aggregate(facets, converter, null); } + @Test + public void testFacetMultiple() { + Document match = new Document("age", new BasicDBObject("$gt", 2)); + DataResult matchedResults = mongoDBCollection.find(match, null); + + String fieldName = "name;surname"; + List facets = MongoDBQueryUtils.createFacet(match, fieldName); + MongoDBDocumentToFacetFieldsConverter converter = new MongoDBDocumentToFacetFieldsConverter(); + DataResult> aggregate = mongoDBCollection.aggregate(facets, converter, null); + System.out.println("aggregate = " + aggregate); + + Assert.assertEquals(2, aggregate.first().size()); + } + @Test public void testFacetCombine() { Document match = new Document("age", new BasicDBObject("$gt", 2));