|
| 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 ObjectMappingFind { |
| 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 | +} |
0 commit comments