@@ -68,13 +68,14 @@ class CassandraVectorStoreIT {
68
68
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
69
69
.withUserConfiguration (TestApplication .class );
70
70
71
- List <Document > documents = List .of (
72
- new Document ("1" , getText ("classpath:/test/data/spring.ai.txt" ), Map .of ("meta1" , "meta1" )),
73
- new Document ("2" , getText ("classpath:/test/data/time.shelter.txt" ), Map .of ()),
74
- new Document ("3" , getText ("classpath:/test/data/great.depression.txt" ),
75
- Map .of ("meta2" , "meta2" , "something_extra" , "blue" )));
71
+ private static List <Document > documents () {
72
+ return List .of (new Document ("1" , getText ("classpath:/test/data/spring.ai.txt" ), Map .of ("meta1" , "meta1" )),
73
+ new Document ("2" , getText ("classpath:/test/data/time.shelter.txt" ), Map .of ()),
74
+ new Document ("3" , getText ("classpath:/test/data/great.depression.txt" ),
75
+ Map .of ("meta2" , "meta2" , "something_extra" , "blue" )));
76
+ }
76
77
77
- public static String getText (String uri ) {
78
+ private static String getText (String uri ) {
78
79
var resource = new DefaultResourceLoader ().getResource (uri );
79
80
try {
80
81
return resource .getContentAsString (StandardCharsets .UTF_8 );
@@ -99,13 +100,21 @@ void addAndSearch() {
99
100
contextRunner .run (context -> {
100
101
try (CassandraVectorStore store = createTestStore (context , new SchemaColumn ("meta1" , DataTypes .TEXT ),
101
102
new SchemaColumn ("meta2" , DataTypes .TEXT ))) {
103
+
104
+ List <Document > documents = documents ();
102
105
store .add (documents );
106
+ for (Document d : documents ) {
107
+ assertThat (d .getEmbedding ()).satisfiesAnyOf (e -> assertThat (e ).isNotNull (),
108
+ e -> assertThat (e ).isNotEmpty ());
109
+ }
103
110
104
111
List <Document > results = store .similaritySearch (SearchRequest .query ("Spring" ).withTopK (1 ));
105
112
106
113
assertThat (results ).hasSize (1 );
107
114
Document resultDoc = results .get (0 );
108
- assertThat (resultDoc .getId ()).isEqualTo (documents .get (0 ).getId ());
115
+ assertThat (resultDoc .getId ()).isEqualTo (documents ().get (0 ).getId ());
116
+ assertThat (resultDoc .getEmbedding ()).satisfiesAnyOf (e -> assertThat (e ).isNull (),
117
+ e -> assertThat (e ).isEmpty ());
109
118
110
119
assertThat (resultDoc .getContent ()).contains (
111
120
"Spring AI provides abstractions that serve as the foundation for developing AI applications." );
@@ -114,7 +123,43 @@ void addAndSearch() {
114
123
assertThat (resultDoc .getMetadata ()).containsKeys ("meta1" , CassandraVectorStore .SIMILARITY_FIELD_NAME );
115
124
116
125
// Remove all documents from the store
117
- store .delete (documents .stream ().map (doc -> doc .getId ()).toList ());
126
+ store .delete (documents ().stream ().map (doc -> doc .getId ()).toList ());
127
+
128
+ results = store .similaritySearch (SearchRequest .query ("Spring" ).withTopK (1 ));
129
+ assertThat (results ).isEmpty ();
130
+ }
131
+ });
132
+ }
133
+
134
+ @ Test
135
+ void addAndSearchReturnEmbeddings () {
136
+ contextRunner .run (context -> {
137
+ CassandraVectorStoreConfig .Builder builder = storeBuilder (context .getBean (CqlSession .class ))
138
+ .returnEmbeddings ();
139
+
140
+ try (CassandraVectorStore store = createTestStore (context , builder )) {
141
+ List <Document > documents = documents ();
142
+ store .add (documents );
143
+ for (Document d : documents ) {
144
+ assertThat (d .getEmbedding ()).satisfiesAnyOf (e -> assertThat (e ).isNotNull (),
145
+ e -> assertThat (e ).isNotEmpty ());
146
+ }
147
+
148
+ List <Document > results = store .similaritySearch (SearchRequest .query ("Spring" ).withTopK (1 ));
149
+
150
+ assertThat (results ).hasSize (1 );
151
+ Document resultDoc = results .get (0 );
152
+ assertThat (resultDoc .getId ()).isEqualTo (documents ().get (0 ).getId ());
153
+ assertThat (resultDoc .getEmbedding ()).isNotEmpty ();
154
+
155
+ assertThat (resultDoc .getContent ()).contains (
156
+ "Spring AI provides abstractions that serve as the foundation for developing AI applications." );
157
+
158
+ assertThat (resultDoc .getMetadata ()).hasSize (1 );
159
+ assertThat (resultDoc .getMetadata ()).containsKey (CassandraVectorStore .SIMILARITY_FIELD_NAME );
160
+
161
+ // Remove all documents from the store
162
+ store .delete (documents ().stream ().map (doc -> doc .getId ()).toList ());
118
163
119
164
results = store .similaritySearch (SearchRequest .query ("Spring" ).withTopK (1 ));
120
165
assertThat (results ).isEmpty ();
@@ -309,7 +354,7 @@ void documentUpdate() {
309
354
void searchWithThreshold () {
310
355
contextRunner .run (context -> {
311
356
try (CassandraVectorStore store = context .getBean (CassandraVectorStore .class )) {
312
- store .add (documents );
357
+ store .add (documents () );
313
358
314
359
List <Document > fullResult = store
315
360
.similaritySearch (SearchRequest .query ("Spring" ).withTopK (5 ).withSimilarityThresholdAll ());
@@ -327,7 +372,7 @@ void searchWithThreshold() {
327
372
328
373
assertThat (results ).hasSize (1 );
329
374
Document resultDoc = results .get (0 );
330
- assertThat (resultDoc .getId ()).isEqualTo (documents .get (0 ).getId ());
375
+ assertThat (resultDoc .getId ()).isEqualTo (documents () .get (0 ).getId ());
331
376
332
377
assertThat (resultDoc .getContent ()).contains (
333
378
"Spring AI provides abstractions that serve as the foundation for developing AI applications." );
@@ -370,17 +415,21 @@ public CqlSession cqlSession() {
370
415
371
416
}
372
417
373
- static CassandraVectorStoreConfig .Builder storeBuilder (CqlSession cqlSession ) {
418
+ private static CassandraVectorStoreConfig .Builder storeBuilder (CqlSession cqlSession ) {
374
419
return CassandraVectorStoreConfig .builder ()
375
420
.withCqlSession (cqlSession )
376
421
.withKeyspaceName ("test_" + CassandraVectorStoreConfig .DEFAULT_KEYSPACE_NAME );
377
422
}
378
423
379
- private CassandraVectorStore createTestStore (ApplicationContext context , SchemaColumn ... metadataFields ) {
380
-
424
+ private static CassandraVectorStore createTestStore (ApplicationContext context , SchemaColumn ... metadataFields ) {
381
425
CassandraVectorStoreConfig .Builder builder = storeBuilder (context .getBean (CqlSession .class ))
382
426
.addMetadataColumns (metadataFields );
383
427
428
+ return createTestStore (context , builder );
429
+ }
430
+
431
+ private static CassandraVectorStore createTestStore (ApplicationContext context ,
432
+ CassandraVectorStoreConfig .Builder builder ) {
384
433
CassandraVectorStoreConfig conf = builder .build ();
385
434
conf .dropKeyspace ();
386
435
return new CassandraVectorStore (conf , context .getBean (EmbeddingClient .class ));
0 commit comments