|
10 | 10 | import java.util.stream.Stream; |
11 | 11 |
|
12 | 12 | public class QuickStart { |
13 | | - public static void main(String[] args) { |
14 | | - // Initialization |
15 | | - AstraDB myDb = new AstraDB("<token>", "<api_endpoint>"); |
| 13 | + public static void main(String[] args) { |
16 | 14 |
|
17 | | - // Create a collection (if needed) |
18 | | - AstraDBCollection demoCollection = myDb.createCollection("demo",14); |
| 15 | +// Initialization |
| 16 | +AstraDB myDb = new AstraDB("<token>", "<api_endpoint>"); |
19 | 17 |
|
20 | | - // Insert vectors |
21 | | - demoCollection.insertOne(new JsonDocument() |
22 | | - .id("doc1") // generated if not set |
23 | | - .vector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
24 | | - .put("product_name", "HealthyFresh - Beef raw dog food") |
25 | | - .put("product_price", 12.99)); |
26 | | - demoCollection.insertOne(new JsonDocument() |
27 | | - .id("doc2") |
28 | | - .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
29 | | - .data("{" |
30 | | - + " \"product_name\": \"HealthyFresh - Chicken raw dog food\", " |
31 | | - + " \"product_price\": 9.99" |
32 | | - + "}")); |
33 | | - demoCollection.insertOne(new JsonDocument() |
34 | | - .id("doc3") |
35 | | - .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
36 | | - .data(Map.of("product_name", "HealthyFresh - Chicken raw dog food"))); |
37 | | - demoCollection.insertOne(new JsonDocument() |
38 | | - .id("doc4") |
39 | | - .vector(new float[]{1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}) |
40 | | - .put("product_name", "HealthyFresh - Chicken raw dog food") |
41 | | - .put("product_price", 9.99)); |
| 18 | +// Create a collection (if needed) |
| 19 | +AstraDBCollection demoCollection = myDb.createCollection("demo",14); |
42 | 20 |
|
43 | | - // Semantic/Vector Search |
44 | | - float[] embeddings = new float[] {1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}; |
45 | | - Filter metadataFilter = new Filter().where("product_price").isEqualsTo(9.99); |
46 | | - int maxRecord = 10; |
47 | | - Stream<JsonResult> resultsSet = demoCollection.findVector(embeddings, metadataFilter, maxRecord); |
48 | | - } |
| 21 | +// Insert vectors |
| 22 | +demoCollection.insertOne(new JsonDocument() |
| 23 | + .id("doc1") // generated if not set |
| 24 | + .vector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
| 25 | + .put("product_name", "HealthyFresh - Beef raw dog food") |
| 26 | + .put("product_price", 12.99)); |
| 27 | +demoCollection.insertOne(new JsonDocument() |
| 28 | + .id("doc2") |
| 29 | + .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
| 30 | + .data("{" |
| 31 | + + " \"product_name\": \"HealthyFresh - Chicken raw dog food\", " |
| 32 | + + " \"product_price\": 9.99" |
| 33 | + + "}")); |
| 34 | +demoCollection.insertOne(new JsonDocument() |
| 35 | + .id("doc3") |
| 36 | + .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
| 37 | + .data(Map.of("product_name", "HealthyFresh - Chicken raw dog food"))); |
| 38 | +demoCollection.insertOne(new JsonDocument() |
| 39 | + .id("doc4") |
| 40 | + .vector(new float[]{1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}) |
| 41 | + .put("product_name", "HealthyFresh - Chicken raw dog food") |
| 42 | + .put("product_price", 9.99)); |
| 43 | + |
| 44 | +// Semantic/Vector Search |
| 45 | +float[] embeddings = new float[] {1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}; |
| 46 | +Filter metadataFilter = new Filter().where("product_price").isEqualsTo(9.99); |
| 47 | +int maxRecord = 10; |
| 48 | +Stream<JsonResult> resultsSet = demoCollection.findVector(embeddings, metadataFilter, maxRecord); |
| 49 | +} |
49 | 50 | } |
0 commit comments