Skip to content

Commit 278e690

Browse files
authored
feat: support filter after aggregation (#105)
1 parent 5578bb6 commit 278e690

13 files changed

Lines changed: 338 additions & 149 deletions

File tree

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ java-cosmos is a client for Azure CosmosDB 's SQL API (also called documentdb fo
2323
<dependency>
2424
<groupId>com.github.thunderz99</groupId>
2525
<artifactId>java-cosmos</artifactId>
26-
<version>0.6.8</version>
26+
<version>0.6.9</version>
2727
</dependency>
2828
```
2929

@@ -293,14 +293,19 @@ The main difference between Partial update and Patch is that:
293293

294294
```java
295295
// support aggregate function: COUNT, AVG, SUM, MAX, MIN
296-
// see https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-aggregate-functions
297-
var aggregate = Aggregate.function("COUNT(1) AS facetCount").groupBy("location", "gender");
296+
// see https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-aggregate-functions
297+
var aggregate=Aggregate.function("COUNT(1) AS facetCount").groupBy("location","gender");
298298

299-
var result = db.aggregate("Collection1", aggregate, Condition.filter("age >=", 20));
299+
var result=db.aggregate("Collection1",aggregate,Condition.filter("age >=",20));
300300

301301
// will generate a sql like this:
302302
/* SELECT COUNT(1) as facetCount, c.location, c.gender WHERE age >= 20 GROUP BY c.location, c.gender
303-
*/
303+
*/
304+
305+
// Also supports filtering the aggregation result
306+
var aggregate=Aggregate.function("COUNT(1) AS facetCount").groupBy("location","gender")
307+
.conditionAfterAggregate(Condition.filter("facetCount >",1)) // filter after aggregation
308+
;
304309
```
305310

306311
### Increment

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.github.thunderz99</groupId>
55
<artifactId>java-cosmos</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.6.8</version>
7+
<version>0.6.9</version>
88
<name>${project.groupId}:${project.artifactId}$</name>
99
<description>A lightweight Azure CosmosDB client for Java</description>
1010
<url>https://github.com/thunderz99/java-cosmos</url>

src/main/java/io/github/thunderz99/cosmos/CosmosDatabase.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,9 @@ CosmosDocumentList find(String coll, Aggregate aggregate, Condition cond, String
877877
queryRequestOptions.setPartitionKey(new com.azure.cosmos.models.PartitionKey(partition));
878878
}
879879

880-
var querySpec = cond.toQuerySpec(aggregate);
880+
var querySpec = aggregate == null ?
881+
cond.toQuerySpec() : // normal query
882+
cond.toQuerySpecForAggregate(aggregate); // aggregate query//
881883

882884
var container = this.clientV4.getDatabase(db).getContainer(coll);
883885

src/main/java/io/github/thunderz99/cosmos/condition/Aggregate.java

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,62 @@
99
*/
1010
public class Aggregate {
1111

12-
/**
13-
* Default constructor
14-
*/
15-
public Aggregate() {
16-
}
17-
18-
public String function = "";
19-
20-
public Set<String> groupBy = new LinkedHashSet<>();
21-
22-
/**
23-
* set function
24-
*
25-
* <p>
26-
* aggregate functions like: COUNT, AVG, SUM, MAX, MIN <br>
27-
* see <a href="https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-aggregate-functions">cosmosdb aggregate functions</a>
28-
* </p>
29-
*
30-
*
31-
* @param function aggregate functions like:
32-
* @return Aggregate
33-
*/
34-
public static Aggregate function(String function) {
35-
36-
Aggregate ret = new Aggregate();
37-
ret.function = function;
38-
39-
return ret;
40-
}
41-
42-
43-
public Aggregate groupBy(String... fields) {
44-
45-
if (fields == null || fields.length == 0) {
46-
return this;
47-
}
48-
49-
this.groupBy = new LinkedHashSet<>(List.of(fields));
50-
return this;
51-
}
12+
/**
13+
* Default constructor
14+
*/
15+
public Aggregate() {
16+
}
17+
18+
public String function = "";
19+
20+
public Set<String> groupBy = new LinkedHashSet<>();
21+
22+
23+
/**
24+
* condition for the result of aggregation
25+
*/
26+
public Condition condAfterAggregate = null;
27+
28+
/**
29+
* set function
30+
*
31+
* <p>
32+
* aggregate functions like: COUNT, AVG, SUM, MAX, MIN <br>
33+
* see <a href="https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-aggregate-functions">cosmosdb aggregate functions</a>
34+
* </p>
35+
*
36+
* @param function aggregate functions like:
37+
* @return Aggregate
38+
*/
39+
public static Aggregate function(String function) {
40+
41+
Aggregate ret = new Aggregate();
42+
ret.function = function;
43+
44+
return ret;
45+
}
46+
47+
48+
public Aggregate groupBy(String... fields) {
49+
50+
if (fields == null || fields.length == 0) {
51+
return this;
52+
}
53+
54+
this.groupBy = new LinkedHashSet<>(List.of(fields));
55+
return this;
56+
}
57+
58+
/**
59+
* Add filter / sort / limit that applies to the result of aggregation
60+
*
61+
* @param condAfterAggregate
62+
* @return aggregate obj self
63+
*/
64+
public Aggregate conditionAfterAggregate(Condition condAfterAggregate) {
65+
this.condAfterAggregate = condAfterAggregate;
66+
return this;
67+
}
68+
5269

5370
}

0 commit comments

Comments
 (0)