10
10
import lombok .AllArgsConstructor ;
11
11
import lombok .Data ;
12
12
import lombok .NoArgsConstructor ;
13
+ import org .junit .jupiter .api .Assertions ;
14
+ import org .junit .jupiter .api .Test ;
13
15
14
16
import java .util .Map ;
15
17
import java .util .UUID ;
16
18
17
19
public class AstraVectorQuickStart {
18
20
21
+ @ Test
22
+ public void theCountLoop () {
23
+ String databaseName = "vector_client_test" ;
24
+ String vectorStoreName = "collection_count" ;
25
+ String astraToken = System .getenv ("ASTRA_DB_APPLICATION_TOKEN" );
26
+
27
+ AstraVectorClient vectorClient = new AstraVectorClient (astraToken );
28
+ AstraVectorDatabaseClient vectorDb = vectorClient .database (databaseName );
29
+ //vectorDb.createVectorStore(vectorStoreName, 14);
30
+ JsonVectorStore vectorStore = vectorDb .vectorStore (vectorStoreName );
31
+
32
+ for (int i =vectorStore .count ();i <1000 ;i ++) {
33
+ vectorStore .insert (new JsonDocument ()
34
+ .vector (new float []{(float ) Math .random (), (float ) Math .random (), (float ) Math .random (), (float ) Math .random (), (float ) Math .random (), (float ) Math .random (),(float ) Math .random (), (float ) Math .random (),(float ) Math .random (), (float ) Math .random (), (float ) Math .random (), (float ) Math .random (), (float ) Math .random (), (float ) Math .random ()})
35
+ .put ("product_name" , "HealthyFresh - Beef raw dog food" )
36
+ .put ("product_price" , 12.99 ));
37
+
38
+ if (i %20 ==0 ) {
39
+ System .out .println ("Inserted " + i + " documents and count " + vectorStore .count ());
40
+ }
41
+ }
42
+
43
+
44
+ }
45
+
46
+
47
+ @ Test
19
48
public void quickStartTest () {
20
- String databaseName = "vector_client_test" ;
21
- String astraToken = System .getenv ("ASTRA_DB_APPLICATION_TOKEN" );
49
+ String databaseName = "vector_client_test" ;
50
+ String vectorStoreName = "demo_store" ;
51
+ String astraToken = System .getenv ("ASTRA_DB_APPLICATION_TOKEN" );
22
52
53
+ // 1a. Initialization with a client
23
54
AstraVectorClient vectorClient = new AstraVectorClient (astraToken );
55
+
56
+ // 1b. Create DB (Skip if you already have a database running)
24
57
if (!vectorClient .isDatabaseExists (databaseName )) {
25
58
vectorClient .createDatabase (databaseName );
26
59
}
27
60
28
- // Without ODM Accessing the Vector DB with JSON-ISH
61
+ // 2. Create a store (delete if exist)
29
62
AstraVectorDatabaseClient vectorDb = vectorClient .database (databaseName );
30
- JsonVectorStore jsonVectorStore =
31
- vectorDb .createVectorStore ("demo_product" , 14 );
32
-
33
- // ======== INSERTIONS =========
34
-
35
- jsonVectorStore .insert (new JsonDocument ("doc1" )
63
+ vectorDb .deleteStore (vectorStoreName );
64
+ vectorDb .createVectorStore (vectorStoreName , 14 );
65
+
66
+ // 3. Insert data in the store
67
+ JsonVectorStore vectorStore = vectorDb .vectorStore (vectorStoreName );
68
+ // 3a. Insert One (attributes as key/value)
69
+ vectorStore .insert (new JsonDocument ()
70
+ .id ("doc1" ) // generated if not set
71
+ .vector (new float []{1f , 0f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f })
36
72
.put ("product_name" , "HealthyFresh - Beef raw dog food" )
37
- .put ("product_price" , 12.99 )
38
- .vector (new float []{1f , 0f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f }));
39
-
40
- jsonVectorStore .insert (new JsonDocument ("doc2" )
73
+ .put ("product_price" , 12.99 ));
74
+ // 3b. Insert One (attributes as JSON)
75
+ vectorStore .insert (new JsonDocument ()
76
+ .id ("doc2" )
77
+ .vector (new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f })
78
+ .data ("{"
79
+ +" \" product_name\" : \" HealthyFresh - Chicken raw dog food\" , "
80
+ + " \" product_price\" : 9.99"
81
+ + "}" )
82
+ );
83
+ // 3c. Insert One (attributes as a MAP)
84
+ vectorStore .insert (new JsonDocument ()
85
+ .id ("doc3" )
86
+ .vector (new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f })
41
87
.data (Map .of ("product_name" , "HealthyFresh - Chicken raw dog food" ))
42
- .vector (new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f }));
43
-
44
- jsonVectorStore .insert (new JsonDocument ("doc3" )
45
- .data ("{"
46
- +" \" product_name\" : \" HealthyFresh - Chicken raw dog food\" , "
47
- + " \" product_price\" : 9.99, "
48
- + "}" )
49
- .vector (new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f }));
50
-
51
- //jsonVectorStore.insert("{"
52
- // + " \"_id\":\"doc4\","
53
- // + " \"$vector\":[1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f],"
54
- // + " \"product_name\": \"HealthyFresh - Chicken raw dog food\", "
55
- // + " \"product_price\": 9.99, "
56
- // + "}");
88
+ );
89
+ // 3d. Insert as a single Big JSON
90
+ vectorStore .insert ("{"
91
+ + " \" _id\" :\" doc4\" ,"
92
+ + " \" $vector\" :[1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],"
93
+ + " \" product_name\" : \" HealthyFresh - Chicken raw dog food\" , "
94
+ + " \" product_price\" : 9.99"
95
+ + "}" );
57
96
58
97
// With ODM
59
- VectorStore <Product > productVectorStore =
60
- vectorDb .createVectorStore ("demo_product" , 14 , Product .class );
98
+ VectorStore <Product > productVectorStore = vectorDb .vectorStore (vectorStoreName , Product .class );
61
99
62
100
// 3 fields: id, payload, vector
63
101
productVectorStore .insert ("doc5" ,
@@ -70,6 +108,7 @@ public void quickStartTest() {
70
108
new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f });
71
109
productVectorStore .insert (doc6 );
72
110
111
+ Assertions .assertEquals (6 , productVectorStore .count ());
73
112
}
74
113
75
114
0 commit comments