|
3 | 3 | import com.dtsx.astra.sdk.AstraDB; |
4 | 4 | import com.dtsx.astra.sdk.AstraDBCollection; |
5 | 5 | import io.stargate.sdk.json.domain.SelectQuery; |
6 | | - |
7 | 6 | import java.util.ArrayList; |
8 | 7 | import java.util.HashMap; |
9 | 8 | import java.util.List; |
10 | 9 | import java.util.Map; |
11 | 10 |
|
12 | 11 | 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); |
17 | 15 |
|
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); |
20 | 23 |
|
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); |
26 | 31 |
|
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); |
32 | 42 |
|
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); |
38 | 51 |
|
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); |
47 | 60 |
|
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); |
56 | 69 |
|
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 | + } |
81 | 89 | } |
0 commit comments