Skip to content

Commit

Permalink
datastore: fix MongoDB facet parser, #TASK-7151, #TASK-7134
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Jan 17, 2025
1 parent 421d5ce commit 1184be3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -775,14 +775,18 @@ private static List<Bson> createFacet(Bson query, List<String> facetFields) {
facet = getMongoDBFacet(groupField, accumulator, accumulatorField, boundaries);

// Unwind in any case
String[] split = groupField.split("\\.");
String acc = "";
for (String s : split) {
if (!StringUtils.isEmpty(acc)) {
acc += ".";
}
acc += s;
unwindList.add(Aggregates.unwind("$" + acc));
Set<String> unwindFields = new HashSet<>();
if (StringUtils.isNotEmpty(groupField)) {
unwindFields.addAll(getUnwindFields(groupField));
}
if (StringUtils.isNotEmpty(accumulatorField)) {
unwindFields.addAll(getUnwindFields(accumulatorField));
}
// We must order the "unwind" fields
List<String> unwindFieldList = new ArrayList<>(unwindFields);
unwindFieldList.sort(Comparator.comparingInt(s -> s.length() - s.replace(".", "").length()));
for (String unwindField : unwindFieldList) {
unwindList.add(Aggregates.unwind("$" + unwindField));
}
}

Expand All @@ -807,6 +811,20 @@ private static List<Bson> createFacet(Bson query, List<String> facetFields) {
return result;
}

private static Collection<String> getUnwindFields(String field) {
List<String> unwindFields = new ArrayList<>();
String[] split = field.split("\\.");
String acc = "";
for (String s : split) {
if (!StringUtils.isEmpty(acc)) {
acc += ".";
}
acc += s;
unwindFields.add(acc);
}
return unwindFields;
}

private static Facet getMongoDBFacet(String groupField, Accumulator accumulator, String accumulatorField, List<Double> boundaries) {
String groupFieldId = groupField;
String accumulatorId = "$" + groupField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ public String toString() {

public static class Dog {
public int age;
public List<Integer> years;
public String color;

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Dog{");
sb.append("age=").append(age);
sb.append("years=").append(years);
sb.append("color=").append(color);
sb.append('}');
return sb.toString();
Expand Down Expand Up @@ -161,6 +163,14 @@ private static MongoDBCollection createTestCollection(String test, int size) {
for (int j = 0 ; j < numDogs; j++) {
Document dog = new Document();
dog.put("age", random.nextInt(20));
int numYears = random.nextInt(3);
List<Integer> years = new ArrayList<>();
for (int k = 0 ; k < numYears; k++) {
years.add(random.nextInt(100) + 1900);
}
if (years.size() > 1) {
dog.put("years", years);
}
dog.put("color", COLORS.get(random.nextInt(COLORS.size())));
dogs.add(dog);
}
Expand Down Expand Up @@ -701,6 +711,34 @@ public void testFacetAvgBucketsArray() {
Assert.assertEquals(aggregate.getResults().get(0).get(0).getAggregationValues().get(0), 1.0d * acc / counter, 0.0001);
}

// @Test
// public void testFacetAccumulatorMaxBucketsArray() {
// Document match = new Document("age", new BasicDBObject("$gt", 2));
// DataResult<Document> matchedResults = mongoDBCollection.find(match, null);
//
// String fieldName = "dogs.color:max(dogs.years)";
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, fieldName);
// System.out.println("facets = " + facets);
// MongoDBDocumentToFacetFieldsConverter converter = new MongoDBDocumentToFacetFieldsConverter();
// DataResult<List<FacetField>> aggregate = mongoDBCollection.aggregate(facets, converter, null);
// for (List<FacetField> facetFieldList : aggregate.getResults()) {
// System.out.println("facetFieldList = " + facetFieldList);
// }
//
// int counter = 0;
// int acc = 0;
// for (Document doc : matchedResults.getResults()) {
// List<Document> dogs = (List<Document>) doc.get("dogs");
// for (Document dog : dogs) {
// counter++;
// acc += (int) dog.get("age");
// }
// }
// System.out.println("counter = " + counter);
// System.out.println("(acc/counter) = " + (1.0d * acc / counter));
// Assert.assertEquals(aggregate.getResults().get(0).get(0).getAggregationValues().get(0), 1.0d * acc / counter, 0.0001);
// }
//
@Test
public void testFacetFilterAccumulatorBucketsArray() {
Document match = new Document("age", new BasicDBObject("$gt", 2));
Expand Down

0 comments on commit 1184be3

Please sign in to comment.