|
| 1 | +/* |
| 2 | + * Copyright 2016-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.cassandra.repository; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import reactor.core.publisher.Flux; |
| 21 | +import reactor.test.StepVerifier; |
| 22 | + |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.UUID; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.context.annotation.ComponentScan; |
| 33 | +import org.springframework.context.annotation.Configuration; |
| 34 | +import org.springframework.context.annotation.FilterType; |
| 35 | +import org.springframework.data.annotation.Id; |
| 36 | +import org.springframework.data.annotation.PersistenceCreator; |
| 37 | +import org.springframework.data.cassandra.config.SchemaAction; |
| 38 | +import org.springframework.data.cassandra.core.mapping.SaiIndexed; |
| 39 | +import org.springframework.data.cassandra.core.mapping.Table; |
| 40 | +import org.springframework.data.cassandra.core.mapping.VectorType; |
| 41 | +import org.springframework.data.cassandra.repository.config.EnableReactiveCassandraRepositories; |
| 42 | +import org.springframework.data.cassandra.repository.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; |
| 43 | +import org.springframework.data.cassandra.repository.support.IntegrationTestConfig; |
| 44 | +import org.springframework.data.domain.Limit; |
| 45 | +import org.springframework.data.domain.ScoringFunction; |
| 46 | +import org.springframework.data.domain.SearchResult; |
| 47 | +import org.springframework.data.domain.Similarity; |
| 48 | +import org.springframework.data.domain.Vector; |
| 49 | +import org.springframework.data.domain.VectorScoringFunctions; |
| 50 | +import org.springframework.data.repository.reactive.ReactiveCrudRepository; |
| 51 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 52 | + |
| 53 | +/** |
| 54 | + * Integration tests for Vector Search using reactive repositories. |
| 55 | + * |
| 56 | + * @author Mark Paluch |
| 57 | + */ |
| 58 | +@SpringJUnitConfig |
| 59 | +class ReactiveVectorSearchIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { |
| 60 | + |
| 61 | + Vector VECTOR = Vector.of(0.2001f, 0.32345f, 0.43456f, 0.54567f, 0.65678f); |
| 62 | + |
| 63 | + @Configuration |
| 64 | + @EnableReactiveCassandraRepositories(basePackageClasses = ReactiveVectorSearchRepository.class, |
| 65 | + considerNestedRepositories = true, |
| 66 | + includeFilters = @ComponentScan.Filter(classes = ReactiveVectorSearchRepository.class, |
| 67 | + type = FilterType.ASSIGNABLE_TYPE)) |
| 68 | + public static class Config extends IntegrationTestConfig { |
| 69 | + |
| 70 | + @Override |
| 71 | + protected Set<Class<?>> getInitialEntitySet() { |
| 72 | + return Collections.singleton(WithVectorFields.class); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public SchemaAction getSchemaAction() { |
| 77 | + return SchemaAction.RECREATE_DROP_UNUSED; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Autowired ReactiveVectorSearchRepository repository; |
| 82 | + |
| 83 | + @BeforeEach |
| 84 | + void setUp() { |
| 85 | + |
| 86 | + repository.deleteAll().as(StepVerifier::create).verifyComplete(); |
| 87 | + |
| 88 | + WithVectorFields w1 = new WithVectorFields("de", "one", Vector.of(0.1001f, 0.22345f, 0.33456f, 0.44567f, 0.55678f)); |
| 89 | + WithVectorFields w2 = new WithVectorFields("de", "two", Vector.of(0.2001f, 0.32345f, 0.43456f, 0.54567f, 0.65678f)); |
| 90 | + WithVectorFields w3 = new WithVectorFields("en", "three", |
| 91 | + Vector.of(0.9001f, 0.82345f, 0.73456f, 0.64567f, 0.55678f)); |
| 92 | + WithVectorFields w4 = new WithVectorFields("de", "four", |
| 93 | + Vector.of(0.9001f, 0.92345f, 0.93456f, 0.94567f, 0.95678f)); |
| 94 | + |
| 95 | + repository.saveAll(List.of(w1, w2, w3, w4)).as(StepVerifier::create).expectNextCount(4).verifyComplete(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test // GH- |
| 99 | + void shouldConsiderScoringFunction() { |
| 100 | + |
| 101 | + Vector vector = Vector.of(0.9f, 0.54f, 0.12f, 0.1f, 0.95f); |
| 102 | + |
| 103 | + List<SearchResult<WithVectorFields>> results = repository |
| 104 | + .searchByEmbeddingNear(vector, VectorScoringFunctions.COSINE, Limit.of(100)).collectList().block(); |
| 105 | + |
| 106 | + assertThat(results).hasSize(4); |
| 107 | + for (SearchResult<WithVectorFields> result : results) { |
| 108 | + assertThat(result.getScore()).isInstanceOf(Similarity.class); |
| 109 | + assertThat(result.getScore().getValue()).isNotCloseTo(0d, offset(0.1d)); |
| 110 | + } |
| 111 | + |
| 112 | + results = repository.searchByEmbeddingNear(VECTOR, VectorScoringFunctions.EUCLIDEAN, Limit.of(100)).collectList() |
| 113 | + .block(); |
| 114 | + |
| 115 | + assertThat(results).hasSize(4); |
| 116 | + for (SearchResult<WithVectorFields> result : results) { |
| 117 | + assertThat(result.getScore()).isInstanceOf(Similarity.class); |
| 118 | + assertThat(result.getScore().getValue()).isNotCloseTo(0.3d, offset(0.1d)); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Test // GH- |
| 123 | + void shouldRunAnnotatedSearchByVector() { |
| 124 | + |
| 125 | + List<SearchResult<WithVectorFields>> results = repository.searchAnnotatedByEmbeddingNear(VECTOR, Limit.of(100)) |
| 126 | + .collectList().block(); |
| 127 | + |
| 128 | + assertThat(results).hasSize(4); |
| 129 | + for (SearchResult<WithVectorFields> result : results) { |
| 130 | + assertThat(result.getScore()).isInstanceOf(Similarity.class); |
| 131 | + assertThat(result.getScore().getValue()).isNotCloseTo(0d, offset(0.1d)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Test // GH- |
| 136 | + void shouldFindByVector() { |
| 137 | + |
| 138 | + List<WithVectorFields> result = repository.findByEmbeddingNear(VECTOR, Limit.of(100)).collectList().block(); |
| 139 | + |
| 140 | + assertThat(result).hasSize(4); |
| 141 | + } |
| 142 | + |
| 143 | + interface ReactiveVectorSearchRepository extends ReactiveCrudRepository<WithVectorFields, UUID> { |
| 144 | + |
| 145 | + Flux<SearchResult<WithVectorFields>> searchByEmbeddingNear(Vector embedding, ScoringFunction function, Limit limit); |
| 146 | + |
| 147 | + Flux<WithVectorFields> findByEmbeddingNear(Vector embedding, Limit limit); |
| 148 | + |
| 149 | + @Query("SELECT id,description,country,similarity_cosine(embedding,:embedding) AS score FROM withvectorfields ORDER BY embedding ANN OF :embedding LIMIT :limit") |
| 150 | + Flux<SearchResult<WithVectorFields>> searchAnnotatedByEmbeddingNear(Vector embedding, Limit limit); |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + @Table |
| 155 | + static class WithVectorFields { |
| 156 | + |
| 157 | + @Id String id; |
| 158 | + String country; |
| 159 | + String description; |
| 160 | + |
| 161 | + @VectorType(dimensions = 5) |
| 162 | + @SaiIndexed Vector embedding; |
| 163 | + |
| 164 | + @PersistenceCreator |
| 165 | + public WithVectorFields(String id, String country, String description, Vector embedding) { |
| 166 | + this.id = id; |
| 167 | + this.country = country; |
| 168 | + this.description = description; |
| 169 | + this.embedding = embedding; |
| 170 | + } |
| 171 | + |
| 172 | + public WithVectorFields(String country, String description, Vector embedding) { |
| 173 | + this.id = UUID.randomUUID().toString(); |
| 174 | + this.country = country; |
| 175 | + this.description = description; |
| 176 | + this.embedding = embedding; |
| 177 | + } |
| 178 | + |
| 179 | + public String getId() { |
| 180 | + return id; |
| 181 | + } |
| 182 | + |
| 183 | + public String getCountry() { |
| 184 | + return country; |
| 185 | + } |
| 186 | + |
| 187 | + public String getDescription() { |
| 188 | + return description; |
| 189 | + } |
| 190 | + |
| 191 | + public Vector getEmbedding() { |
| 192 | + return embedding; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public String toString() { |
| 197 | + return "WithVectorFields{" + "id='" + id + '\'' + ", country='" + country + '\'' + ", description='" + description |
| 198 | + + '\'' + '}'; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments