Skip to content

Commit dbe5b99

Browse files
authored
feat: support RegexMatch for cosmosdb (#103)
1 parent dc47b34 commit dbe5b99

4 files changed

Lines changed: 53 additions & 33 deletions

File tree

README.md

Lines changed: 32 additions & 31 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.6</version>
26+
<version>0.6.7</version>
2727
</dependency>
2828
```
2929

@@ -243,36 +243,37 @@ The main difference between Partial update and Patch is that:
243243
### Complex queries
244244

245245
```java
246-
var cond = Condition.filter(
247-
"id", "id010", // id equal to 'id010'
248-
"lastName", "Banks", // last name equal to Banks
249-
"firstName !=", "Andy", // not equal
250-
"firstName LIKE", "%dy%", // see cosmosdb LIKE
251-
"location", List.of("New York", "Paris"), // location is 'New York' or 'Paris'. see cosmosdb IN
252-
"skills =", List.of("Java", "React"), // skills equals array ["Java", "React"] exactly
253-
"age >=", 20, // see cosmosdb compare operators
254-
"middleName OR firstName STARTSWITH", "H", // see cosmosdb STARTSWITH
255-
"desciption CONTAINS", "Project manager",// see cosmosdb CONTAINS
256-
"certificates IS_DEFINED", true, // see cosmosdb IS_DEFINED
257-
"families.villa IS_DEFINED", false, // see cosmosdb IS_DEFINED
258-
"age IS_NUMBER", true, // see cosmosdb type check functions
259-
"tagIds ARRAY_CONTAINS", "T001", // see cosmosdb ARRAY_CONTAINS
260-
"tagIds ARRAY_CONTAINS_ANY", List.of("T001", "T002"), // see cosmosdb EXISTS
261-
"tags ARRAY_CONTAINS_ALL name", List.of("Java", "React"), // see cosmosdb EXISTS
262-
"$OR", List.of( // add an OR sub condition
263-
Condition.filter("position", "leader"), // subquery's fields/order/offset/limit will be ignored
264-
Condition.filter("organization.id", "executive_committee")
265-
),
266-
"$OR 2", List.of( // add another OR sub condition (name it $OR xxx in order to avoid the same key to a previous $OR )
267-
Condition.filter("position", "leader"), // subquery's fields/order/offset/limit will be ignored
268-
Condition.filter("organization.id", "executive_committee")
269-
),
270-
"$AND", List.of(
271-
Condition.filter("tagIds ARRAY_CONTAINS_ALL", List.of("T001", "T002")).not() // A negative condition. see cosmosdb NOT
272-
Condition.filter("city", "Tokyo")
273-
),
274-
"$NOT", Map.of("lastName CONTAINS", "Willington"), // A negative query using $NOT
275-
"$NOT 2", Map.of("$OR 3", // A nested filter using $NOT and $OR
246+
var cond=Condition.filter(
247+
"id","id010", // id equal to 'id010'
248+
"lastName","Banks", // last name equal to Banks
249+
"firstName !=","Andy", // not equal
250+
"firstName LIKE","%dy%", // see cosmosdb LIKE
251+
"location",List.of("New York","Paris"), // location is 'New York' or 'Paris'. see cosmosdb IN
252+
"skills =",List.of("Java","React"), // skills equals array ["Java", "React"] exactly
253+
"age >=",20, // see cosmosdb compare operators
254+
"firstName STARTSWITH","H", // see cosmosdb STARTSWITH
255+
"desciption CONTAINS","Project manager",// see cosmosdb CONTAINS
256+
"fullName.last RegexMatch","[A-Z]{1}ank\\w+$", // see cosmosdb RegexMatch
257+
"certificates IS_DEFINED",true, // see cosmosdb IS_DEFINED
258+
"families.villa IS_DEFINED",false, // see cosmosdb IS_DEFINED
259+
"age IS_NUMBER",true, // see cosmosdb type check functions
260+
"tagIds ARRAY_CONTAINS","T001", // see cosmosdb ARRAY_CONTAINS
261+
"tagIds ARRAY_CONTAINS_ANY",List.of("T001","T002"), // see cosmosdb EXISTS
262+
"tags ARRAY_CONTAINS_ALL name",List.of("Java","React"), // see cosmosdb EXISTS
263+
"$OR",List.of( // add an OR sub condition
264+
Condition.filter("position","leader"), // subquery's fields/order/offset/limit will be ignored
265+
Condition.filter("organization.id","executive_committee")
266+
),
267+
"$OR 2",List.of( // add another OR sub condition (name it $OR xxx in order to avoid the same key to a previous $OR )
268+
Condition.filter("position","leader"), // subquery's fields/order/offset/limit will be ignored
269+
Condition.filter("organization.id","executive_committee")
270+
),
271+
"$AND",List.of(
272+
Condition.filter("tagIds ARRAY_CONTAINS_ALL",List.of("T001","T002")).not() // A negative condition. see cosmosdb NOT
273+
Condition.filter("city","Tokyo")
274+
),
275+
"$NOT",Map.of("lastName CONTAINS","Willington"), // A negative query using $NOT
276+
"$NOT 2",Map.of("$OR 3", // A nested filter using $NOT and $OR
276277
List.of(
277278
Map.of("lastName", ""), // note they will do the same thing using Condition.filter or Map.of
278279
Map.of("age >=", 20)

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.6</version>
7+
<version>0.6.7</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/condition/Condition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public enum OperatorType {
9797

9898

9999
public static final Pattern simpleExpressionPattern = Pattern
100-
.compile("(.+)\\s(STARTSWITH|ENDSWITH|CONTAINS|ARRAY_CONTAINS|LIKE|IN|" + TYPE_CHECK_FUNCTIONS + "|=|!=|<|<=|>|>=)\\s*$");
100+
.compile("(.+)\\s(STARTSWITH|ENDSWITH|CONTAINS|ARRAY_CONTAINS|LIKE|IN|RegexMatch|" + TYPE_CHECK_FUNCTIONS + "|=|!=|<|<=|>|>=)\\s*$");
101101

102102
public static final Pattern typeCheckFunctionPattern = Pattern
103103
.compile(TYPE_CHECK_FUNCTIONS);

src/test/java/io/github/thunderz99/cosmos/CosmosDatabaseTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,25 @@ public void find_should_work_with_filter() throws Exception {
820820

821821
}
822822

823+
@Test
824+
public void regex_should_work_with_filter() throws Exception {
825+
826+
// test regex match
827+
{
828+
var cond = Condition.filter("fullName.last RegexMatch", "[A-Z]{1}ank\\w+$", //
829+
"fullName.first", "Elise" //
830+
).sort("id", "ASC") //
831+
.limit(10) //
832+
.offset(0);
833+
834+
var users = db.find(coll, cond, "Users").toList(FullNameUser.class);
835+
836+
assertThat(users.size()).isEqualTo(1);
837+
assertThat(users.get(0)).hasToString(user1.toString());
838+
}
839+
840+
}
841+
823842
@Test
824843
void aggregate_should_work() throws Exception {
825844
// test aggregate(simple)

0 commit comments

Comments
 (0)