Skip to content

Commit

Permalink
datastore: fix MongoDB facets when combining multiple fields, #TASK-7…
Browse files Browse the repository at this point in the history
…151, #TASK-7134
  • Loading branch information
jtarraga committed Jan 31, 2025
1 parent 52ae7ee commit 9243ebb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ public List<FacetField> convertToDataModelType(Document document) {
List<Document> documentValues = (List<Document>) 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<FacetField.Bucket> 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);
Expand All @@ -41,6 +44,9 @@ public List<FacetField> 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<FacetField> bucketFacetFields = null;
Expand All @@ -66,7 +72,6 @@ public List<FacetField> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ private static List<Bson> createFacet(Bson query, List<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Document> matchedResults = mongoDBCollection.find(match, null);

String fieldName = "name;surname";
List<Bson> facets = MongoDBQueryUtils.createFacet(match, fieldName);
MongoDBDocumentToFacetFieldsConverter converter = new MongoDBDocumentToFacetFieldsConverter();
DataResult<List<FacetField>> 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));
Expand Down

0 comments on commit 9243ebb

Please # to comment.