Skip to content

Commit

Permalink
mongodb: fix aggregation parse
Browse files Browse the repository at this point in the history
  • Loading branch information
imedina committed Dec 13, 2024
1 parent 3f9386f commit 13b3e59
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public List<FacetField> convertToDataModelType(Document document) {
bucketValue = (String) internalIdValue;
} else if (internalIdValue instanceof Boolean
|| internalIdValue instanceof Integer
|| internalIdValue instanceof Long
|| internalIdValue instanceof Double) {
bucketValue = internalIdValue.toString();
} else if (internalIdValue instanceof Document) {
Expand All @@ -43,65 +44,68 @@ public List<FacetField> convertToDataModelType(Document document) {
}
key = key.substring(0, key.length() - COUNTS_SUFFIX.length());
facets.add(new FacetField(key, total, buckets));
} else if (key.endsWith(RANGES_SUFFIX)) {
List<Double> facetFieldValues = new ArrayList<>();
Number start = null;
Number end = null;
Number step = null;
Double other = null;
for (Document value : documentValues) {
if (value.get(INTERNAL_ID) instanceof String && OTHER.equals(value.getString(INTERNAL_ID))) {
other = 1.0d * value.getInteger(count.name());
} else {
Double range = value.getDouble(INTERNAL_ID);
Integer counter = value.getInteger(count.name());
facetFieldValues.add(1.0d * counter);
if (start == null) {
start = range;
}
end = range;
if (step == null && start != end) {
step = end.doubleValue() - start.doubleValue();
}
}
}
key = key.substring(0, key.length() - RANGES_SUFFIX.length()).replace(GenericDocumentComplexConverter.TO_REPLACE_DOTS, ".");
if (other != null) {
key += " (counts out of range: " + other + ")";
}
FacetField facetField = new FacetField(key, "range", facetFieldValues)
.setStart(start)
.setEnd(end)
.setStep(step);
facets.add(facetField);
} else {
Document documentValue = ((List<Document>) entry.getValue()).get(0);
MongoDBQueryUtils.Accumulator accumulator = getAccumulator(documentValue);
switch (accumulator) {
case sum:
case avg:
case max:
case min:
case stdDevPop:
case stdDevSamp: {
List<Double> fieldValues = new ArrayList<>();
if (documentValue.get(accumulator.name()) instanceof Integer) {
fieldValues.add(1.0d * documentValue.getInteger(accumulator.name()));
} else if (documentValue.get(accumulator.name()) instanceof Long) {
fieldValues.add(1.0d * documentValue.getLong(accumulator.name()));
} else if (documentValue.get(accumulator.name()) instanceof List) {
List<Number> list = (List<Number>) documentValue.get(accumulator.name());
for (Number number : list) {
fieldValues.add(number.doubleValue());
}
if (key.endsWith(RANGES_SUFFIX)) {
List<Double> facetFieldValues = new ArrayList<>();
Number start = null;
Number end = null;
Number step = null;
Double other = null;
for (Document value : documentValues) {
if (value.get(INTERNAL_ID) instanceof String && OTHER.equals(value.getString(INTERNAL_ID))) {
other = 1.0d * value.getInteger(count.name());
} else {
fieldValues.add(documentValue.getDouble(accumulator.name()));
Double range = value.getDouble(INTERNAL_ID);
Integer counter = value.getInteger(count.name());
facetFieldValues.add(1.0d * counter);
if (start == null) {
start = range;
}
end = range;
if (step == null && start != end) {
step = end.doubleValue() - start.doubleValue();
}
}
facets.add(new FacetField(documentValue.getString(INTERNAL_ID), accumulator.name(), fieldValues));
break;
}
default: {
// Do nothing, exception is raised
key = key.substring(0,
key.length() - RANGES_SUFFIX.length()).replace(GenericDocumentComplexConverter.TO_REPLACE_DOTS, ".");
if (other != null) {
key += " (counts out of range: " + other + ")";
}
FacetField facetField = new FacetField(key, "range", facetFieldValues)
.setStart(start)
.setEnd(end)
.setStep(step);
facets.add(facetField);
} else {
Document documentValue = ((List<Document>) entry.getValue()).get(0);
MongoDBQueryUtils.Accumulator accumulator = getAccumulator(documentValue);
switch (accumulator) {
case sum:
case avg:
case max:
case min:
case stdDevPop:
case stdDevSamp: {
List<Double> fieldValues = new ArrayList<>();
if (documentValue.get(accumulator.name()) instanceof Integer) {
fieldValues.add(1.0d * documentValue.getInteger(accumulator.name()));
} else if (documentValue.get(accumulator.name()) instanceof Long) {
fieldValues.add(1.0d * documentValue.getLong(accumulator.name()));
} else if (documentValue.get(accumulator.name()) instanceof List) {
List<Number> list = (List<Number>) documentValue.get(accumulator.name());
for (Number number : list) {
fieldValues.add(number.doubleValue());
}
} else {
fieldValues.add(documentValue.getDouble(accumulator.name()));
}
facets.add(new FacetField(documentValue.getString(INTERNAL_ID), accumulator.name(), fieldValues));
break;
}
default: {
// Do nothing, exception is raised
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ private static List<Bson> createFacet(Bson query, List<String> facetFields) {
String accumulatorField = split[2];

facet = new Facet(groupField + COUNTS_SUFFIX,
Aggregates.group("$" + groupField, Accumulators.sum(accumulator.name(), "$" + accumulatorField)));
Aggregates.group("$" + groupField, Accumulators.sum(count.name(), "$" + accumulatorField)));
} else {
groupField = facetField;
accumulator = count;
Expand Down

0 comments on commit 13b3e59

Please sign in to comment.