Skip to content

Commit 207fe91

Browse files
committed
Documentation Support
1 parent 4c20c39 commit 207fe91

File tree

6 files changed

+228
-12
lines changed

6 files changed

+228
-12
lines changed

astra-db-client/src/main/java/com/dtsx/astra/sdk/AstraDBCollection.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ public boolean isDocumentExists(String id) {
144144
return collectionClient.isDocumentExists(id);
145145
}
146146

147+
/**
148+
* Find one document matching the query.
149+
*
150+
* @param rawJsonQuery
151+
* query documents and vector
152+
* @return
153+
* result if exists
154+
*/
155+
public Optional<JsonResult> findOne(String rawJsonQuery) {
156+
return collectionClient.findOne(rawJsonQuery);
157+
}
158+
147159
/**
148160
* Find one document matching the query.
149161
*
@@ -172,6 +184,22 @@ public <DOC> Optional<Result<DOC>> findOne(SelectQuery query, Class<DOC> clazz)
172184
return findOne(query).map(r -> new Result<>(r, clazz));
173185
}
174186

187+
/**
188+
* Find one document matching the query.
189+
*
190+
* @param query
191+
* query documents and vector
192+
* @param clazz
193+
* class of the document
194+
* @return
195+
* result if exists
196+
* @param <DOC>
197+
* class to be marshalled
198+
*/
199+
public <DOC> Optional<Result<DOC>> findOne(String query, Class<DOC> clazz) {
200+
return findOne(query).map(r -> new Result<>(r, clazz));
201+
}
202+
175203
/**
176204
* Find one document matching the query.
177205
*
@@ -188,6 +216,23 @@ public <DOC> Optional<Result<DOC>> findOne(SelectQuery query, ResultMapper<DOC>
188216
return findOne(query).map(mapper::map);
189217
}
190218

219+
220+
/**
221+
* Find one document matching the query.
222+
*
223+
* @param query
224+
* query documents and vector
225+
* @param mapper
226+
* convert a json into expected pojo
227+
* @return
228+
* result if exists
229+
* @param <DOC>
230+
* class to be marshalled
231+
*/
232+
public <DOC> Optional<Result<DOC>> findOne(String query, ResultMapper<DOC> mapper) {
233+
return findOne(query).map(mapper::map);
234+
}
235+
191236
// --------------------------
192237
// --- Find By Id ----
193238
// --------------------------
@@ -312,6 +357,18 @@ public Page<JsonResult> findPage(SelectQuery pagedQuery) {
312357
return collectionClient.findPage(pagedQuery);
313358
}
314359

360+
/**
361+
* Find documents matching the pagedQuery.
362+
*
363+
* @param pagedQuery
364+
* current pagedQuery
365+
* @return
366+
* page of results
367+
*/
368+
public Page<JsonResult> findPage(String pagedQuery) {
369+
return collectionClient.findPage(pagedQuery);
370+
}
371+
315372
/**
316373
* Search records with a filter
317374
*

astra-db-client/src/test/java/com/dtsx/astra/sdk/AbstractAstraDBTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ public abstract class AbstractAstraDBTest {
1717

1818
@SuppressWarnings("unchecked")
1919
protected LinkedHashMap<String, List<?>> loadQuotes(String filePath) throws IOException {
20-
SelectQuery.builder()
21-
.where("metadata")
22-
.isEqualsTo("value")
23-
.withLimit(5)
24-
.build()
2520
File inputFile = new File(MetadataVectorTableTest.class.getClassLoader().getResource(filePath).getFile());
2621
LinkedHashMap<String, Object> sampleQuotes = new ObjectMapper().readValue(inputFile, LinkedHashMap.class);
2722
System.out.println("Quotes by Author:");

astra-db-client/src/test/java/com/dtsx/astra/sdk/documentation/CreateCollection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.dtsx.astra.sdk.documentation;
2+
23
import com.dtsx.astra.sdk.AstraDB;
34
import com.dtsx.astra.sdk.AstraDBCollection;
45
import io.stargate.sdk.json.domain.CollectionDefinition;
56
import io.stargate.sdk.json.domain.SimilarityMetric;
6-
import io.stargate.sdk.json.exception.ApiException;
7-
import io.stargate.sdk.json.exception.InvalidJsonApiArgumentException;
7+
import io.stargate.sdk.json.exception.JsonApiException;
88

99
public class CreateCollection {
1010
public static void main(String[] args) {
@@ -34,7 +34,7 @@ public static void main(String[] args) {
3434
*/
3535
try {
3636
db.createCollection("invalid.name");
37-
} catch(ApiException e) {
37+
} catch(JsonApiException e) {
3838
// will fail
3939
}
4040
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.dtsx.astra.sdk.documentation;
2+
3+
import com.dtsx.astra.sdk.AstraDB;
4+
import com.dtsx.astra.sdk.AstraDBCollection;
5+
import io.stargate.sdk.json.domain.SelectQuery;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class Find {
13+
public static void main(String[] args) {
14+
15+
// Accessing existing DB
16+
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
17+
18+
// Access existing collection
19+
AstraDBCollection collection = db.createCollection("collection_vector1", 14);
20+
21+
// Retrieve first document where product_price exists
22+
collection.find(SelectQuery.builder()
23+
.where("product_price")
24+
.exists().build())
25+
.forEach(System.out::println);
26+
27+
// Retrieve first document where product_price is 12.99
28+
collection.find(SelectQuery.builder()
29+
.where("product_price")
30+
.isEqualsTo(12.99).build())
31+
.forEach(System.out::println);
32+
33+
// Retrieve first document where product_price is 12.99 and product_name is "HealthyFresh - Beef raw dog food"
34+
collection.find(SelectQuery.builder()
35+
.where("product_name").isEqualsTo("HealthyFresh - Chicken raw dog food")
36+
.andWhere("product_price").isEqualsTo(9.99).build())
37+
.forEach(System.out::println);
38+
39+
// Limit retrieved fields to product_name and product_price
40+
collection
41+
.find(SelectQuery.builder()
42+
.select("product_name", "product_price")
43+
.where("product_price")
44+
.isEqualsTo(9.99)
45+
.build())
46+
.forEach(System.out::println);
47+
48+
// Add an Ann Search
49+
collection.find(SelectQuery
50+
.builder()
51+
.where("product_price")
52+
.isEqualsTo(9.99)
53+
.orderByAnn(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f})
54+
.build())
55+
.forEach(System.out::println);
56+
57+
collection.find(SelectQuery.builder()
58+
.where("product_name").isEqualsTo("HealthyFresh - Chicken raw dog food")
59+
.orderBy("product_price", 1).build())
60+
.forEach(System.out::println);
61+
62+
// Complex query with AND and OR:
63+
// (product_price == 9.99 OR product_name == "HealthyFresh - Beef raw dog food")
64+
// AND (product_price == 12.99 OR product_name == "HealthyFresh - Beef raw dog food")
65+
SelectQuery sq2 = new SelectQuery();
66+
sq2.setFilter(new HashMap<>());
67+
Map<String, List<Map<String, Object>>> or1Criteria = new HashMap<>();
68+
or1Criteria.put("$or", new ArrayList<Map<String, Object>>());
69+
or1Criteria.get("$or").add(Map.of("product_price", 9.99));
70+
or1Criteria.get("$or").add(Map.of("product_name", "HealthyFresh - Beef raw dog food"));
71+
Map<String, List<Map<String, Object>>> or2Criteria = new HashMap<>();
72+
or2Criteria.put("$or", new ArrayList<Map<String, Object>>());
73+
or2Criteria.get("$or").add(Map.of("product_price", 12.99));
74+
or2Criteria.get("$or").add(Map.of("product_name", "HealthyFresh - Beef raw dog food"));
75+
List<Map<String, List<Map<String, Object>>>> andCriteria = new ArrayList<>();
76+
andCriteria.add(or1Criteria);
77+
andCriteria.add(or2Criteria);
78+
sq2.getFilter().put("$and", andCriteria);
79+
collection.find(sq2).forEach(System.out::println);
80+
}
81+
}

astra-db-client/src/test/java/com/dtsx/astra/sdk/documentation/FindOne.java

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,97 @@
22

33
import com.dtsx.astra.sdk.AstraDB;
44
import com.dtsx.astra.sdk.AstraDBCollection;
5+
import io.stargate.sdk.json.domain.JsonDocument;
6+
import io.stargate.sdk.json.domain.SelectQuery;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
512

613
public class FindOne {
7-
public static void main(String[] args) {
14+
public static void main(String[] args) {
15+
816
// Accessing existing DB
9-
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
17+
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
1018

1119
// Access existing collection
12-
AstraDBCollection collection = db.collection("collection_vector1");
20+
AstraDBCollection collection = db.createCollection("collection_vector1", 14);
21+
22+
// Retrieve first document where product_price exists
23+
collection.findOne(SelectQuery.builder()
24+
.where("product_price")
25+
.exists().build())
26+
.ifPresent(System.out::println);
27+
28+
// Retrieve first document where product_price is 12.99
29+
collection.findOne(SelectQuery.builder()
30+
.where("product_price")
31+
.isEqualsTo(12.99).build())
32+
.ifPresent(System.out::println);
33+
34+
// Retrieve first document where product_price is 12.99 and product_name is "HealthyFresh - Beef raw dog food"
35+
collection.findOne(SelectQuery.builder()
36+
.where("product_name").isEqualsTo("HealthyFresh - Chicken raw dog food")
37+
.andWhere("product_price").isEqualsTo(9.99).build())
38+
.ifPresent(System.out::println);
1339

40+
// Send the request as a JSON String
41+
collection.findOne("{" +
42+
"\"filter\":{" +
43+
"\"product_price\":9.99,\"product_name\":\"HealthyFresh - Chicken raw dog food\"}" +
44+
"}")
45+
.ifPresent(System.out::println);
46+
47+
// Limit retrieved fields to product_name and product_price
48+
collection
49+
.findOne(SelectQuery.builder()
50+
.select("product_name", "product_price")
51+
.where("product_price")
52+
.isEqualsTo(9.99)
53+
.build())
54+
.ifPresent(System.out::println);
55+
56+
// Add an Ann Search
57+
collection.findOne(SelectQuery
58+
.builder()
59+
.where("product_price")
60+
.isEqualsTo(9.99)
61+
.orderByAnn(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f})
62+
.build());
63+
64+
// Complex query with AND and OR
65+
SelectQuery sq2 = new SelectQuery();
66+
sq2.setFilter(new HashMap<>());
67+
Map<String, List<Map<String, Object>>> or1Criteria = new HashMap<>();
68+
or1Criteria.put("$or", new ArrayList<Map<String, Object>>());
69+
or1Criteria.get("$or").add(Map.of("product_price", 9.99));
70+
or1Criteria.get("$or").add(Map.of("product_name", "HealthyFresh - Beef raw dog food"));
71+
Map<String, List<Map<String, Object>>> or2Criteria = new HashMap<>();
72+
or2Criteria.put("$or", new ArrayList<Map<String, Object>>());
73+
or2Criteria.get("$or").add(Map.of("product_price", 12.99));
74+
or2Criteria.get("$or").add(Map.of("product_name", "HealthyFresh - Beef raw dog food"));
75+
List<Map<String, List<Map<String, Object>>>> andCriteria = new ArrayList<>();
76+
andCriteria.add(or1Criteria);
77+
andCriteria.add(or2Criteria);
78+
sq2.getFilter().put("$and", andCriteria);
79+
collection.findOne(sq2).ifPresent(System.out::println);
80+
81+
// Complex query with AND and OR as String
82+
collection.findOne("{\"filter\":{" +
83+
"\"$and\":[" +
84+
"{\"$or\":[" +
85+
" {\"product_price\":9.99}," +
86+
" {\"product_name\":\"HealthyFresh - Beef raw dog food\"}" +
87+
" ]" +
88+
"}," +
89+
"{\"$or\":[" +
90+
" {\"product_price\":12.99}," +
91+
" {\"product_name\":\"HealthyFresh - Beef raw dog food\"}" +
92+
" ]" +
93+
"}" +
94+
"]" +
95+
"}}").ifPresent(System.out::println);
1496
}
97+
1598
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<lombok.version>1.18.30</lombok.version>
3939

4040
<!-- Stargate -->
41-
<stargate-sdk.version>2.1.5-SNAPSHOT</stargate-sdk.version>
41+
<stargate-sdk.version>2.2</stargate-sdk.version>
4242
<stargate-grpc.version>2.0.17</stargate-grpc.version>
4343
<grpc-netty.version>1.56.1</grpc-netty.version>
4444

0 commit comments

Comments
 (0)