Skip to content

Commit 8efb1a3

Browse files
authored
Merge pull request #98 from djsauble/patch-11
Update Find.java
2 parents 6526620 + 70192fd commit 8efb1a3

File tree

1 file changed

+70
-62
lines changed
  • astra-db-client/src/test/java/com/dtsx/astra/sdk/documentation

1 file changed

+70
-62
lines changed

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

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,87 @@
33
import com.dtsx.astra.sdk.AstraDB;
44
import com.dtsx.astra.sdk.AstraDBCollection;
55
import io.stargate.sdk.json.domain.SelectQuery;
6-
76
import java.util.ArrayList;
87
import java.util.HashMap;
98
import java.util.List;
109
import java.util.Map;
1110

1211
public class Find {
13-
public static void main(String[] args) {
14-
15-
// Accessing existing DB
16-
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
12+
public static void main(String[] args) {
13+
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
14+
AstraDBCollection collection = db.createCollection("collection_vector1", 14);
1715

18-
// Access existing collection
19-
AstraDBCollection collection = db.createCollection("collection_vector1", 14);
16+
// Retrieve the first document with a product_price
17+
collection.find(
18+
SelectQuery.builder()
19+
.where("product_price")
20+
.exists()
21+
.build())
22+
.forEach(System.out::println);
2023

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);
24+
// Retrieve the first document where the product_price is 12.99
25+
collection.find(
26+
SelectQuery.builder()
27+
.where("product_price")
28+
.isEqualsTo(12.99)
29+
.build())
30+
.forEach(System.out::println);
2631

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+
// Retrieve the first document where product_price is 12.99
33+
// and product_name is "HealthyFresh - Beef raw dog food"
34+
collection.find(
35+
SelectQuery.builder()
36+
.where("product_name")
37+
.isEqualsTo("HealthyFresh - Chicken raw dog food")
38+
.andWhere("product_price")
39+
.isEqualsTo(9.99)
40+
.build())
41+
.forEach(System.out::println);
3242

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);
43+
// Only retrieve the product_name and product_price fields
44+
collection.find(
45+
SelectQuery.builder()
46+
.select("product_name", "product_price")
47+
.where("product_price")
48+
.isEqualsTo(9.99)
49+
.build())
50+
.forEach(System.out::println);
3851

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);
52+
// Order the results by similarity
53+
collection.find(
54+
SelectQuery.builder()
55+
.where("product_price")
56+
.isEqualsTo(9.99)
57+
.orderByAnn(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f})
58+
.build())
59+
.forEach(System.out::println);
4760

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);
61+
// Order the results by a specific field
62+
collection.find(
63+
SelectQuery.builder()
64+
.where("product_name")
65+
.isEqualsTo("HealthyFresh - Chicken raw dog food")
66+
.orderBy("product_price", 1)
67+
.build())
68+
.forEach(System.out::println);
5669

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-
}
70+
// Complex query with AND and OR:
71+
// (product_price == 9.99 OR product_name == "HealthyFresh - Beef raw dog food")
72+
// AND (product_price == 12.99 OR product_name == "HealthyFresh - Beef raw dog food")
73+
SelectQuery sq2 = new SelectQuery();
74+
sq2.setFilter(new HashMap<>());
75+
Map<String, List<Map<String, Object>>> or1Criteria = new HashMap<>();
76+
or1Criteria.put("$or", new ArrayList<Map<String, Object>>());
77+
or1Criteria.get("$or").add(Map.of("product_price", 9.99));
78+
or1Criteria.get("$or").add(Map.of("product_name", "HealthyFresh - Beef raw dog food"));
79+
Map<String, List<Map<String, Object>>> or2Criteria = new HashMap<>();
80+
or2Criteria.put("$or", new ArrayList<Map<String, Object>>());
81+
or2Criteria.get("$or").add(Map.of("product_price", 12.99));
82+
or2Criteria.get("$or").add(Map.of("product_name", "HealthyFresh - Beef raw dog food"));
83+
List<Map<String, List<Map<String, Object>>>> andCriteria = new ArrayList<>();
84+
andCriteria.add(or1Criteria);
85+
andCriteria.add(or2Criteria);
86+
sq2.getFilter().put("$and", andCriteria);
87+
collection.find(sq2).forEach(System.out::println);
88+
}
8189
}

0 commit comments

Comments
 (0)