|
| 1 | +/* |
| 2 | + * Copyright 2011-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.neo4j.integration.reactive; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.AfterEach; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.neo4j.driver.Driver; |
| 22 | +import org.neo4j.driver.Session; |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | +import reactor.test.StepVerifier; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.data.domain.Score; |
| 31 | +import org.springframework.data.domain.SearchResult; |
| 32 | +import org.springframework.data.domain.Vector; |
| 33 | +import org.springframework.data.neo4j.core.ReactiveDatabaseSelectionProvider; |
| 34 | +import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager; |
| 35 | +import org.springframework.data.neo4j.core.transaction.ReactiveNeo4jTransactionManager; |
| 36 | +import org.springframework.data.neo4j.integration.shared.common.EntityWithVector; |
| 37 | +import org.springframework.data.neo4j.repository.ReactiveNeo4jRepository; |
| 38 | +import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories; |
| 39 | +import org.springframework.data.neo4j.repository.query.VectorSearch; |
| 40 | +import org.springframework.data.neo4j.test.BookmarkCapture; |
| 41 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 42 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 43 | +import org.springframework.data.neo4j.test.Neo4jReactiveTestConfiguration; |
| 44 | +import org.springframework.transaction.ReactiveTransactionManager; |
| 45 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 46 | + |
| 47 | +import static org.assertj.core.api.Assertions.assertThat; |
| 48 | + |
| 49 | +/** |
| 50 | + * @author Gerrit Meier |
| 51 | + */ |
| 52 | +@Neo4jIntegrationTest |
| 53 | +class ReactiveVectorSearchIT { |
| 54 | + |
| 55 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void setupData(@Autowired BookmarkCapture bookmarkCapture, @Autowired Driver driver) { |
| 59 | + try (Session session = driver.session(bookmarkCapture.createSessionConfig())) { |
| 60 | + session.run("MATCH (n) detach delete n"); |
| 61 | + session.run(""" |
| 62 | + CREATE VECTOR INDEX dingsIndex IF NOT EXISTS |
| 63 | + FOR (m:EntityWithVector) |
| 64 | + ON m.myVector |
| 65 | + OPTIONS { indexConfig: { |
| 66 | + `vector.dimensions`: 3, |
| 67 | + `vector.similarity_function`: 'cosine' |
| 68 | + }}""").consume(); |
| 69 | + session.run( |
| 70 | + "CREATE (e:EntityWithVector{name:'dings'}) WITH e CALL db.create.setNodeVectorProperty(e, 'myVector', [0.1, 0.1, 0.1])") |
| 71 | + .consume(); |
| 72 | + session.run( |
| 73 | + "CREATE (e:EntityWithVector{name:'dings2'}) WITH e CALL db.create.setNodeVectorProperty(e, 'myVector', [0.7, 0.0, 0.3])") |
| 74 | + .consume(); |
| 75 | + session.run("CALL db.awaitIndexes()").consume(); |
| 76 | + bookmarkCapture.seedWith(session.lastBookmarks()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @AfterEach |
| 81 | + void removeIndex(@Autowired BookmarkCapture bookmarkCapture, @Autowired Driver driver) { |
| 82 | + try (Session session = driver.session(bookmarkCapture.createSessionConfig())) { |
| 83 | + session.run("DROP INDEX `dingsIndex` IF EXISTS"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void findAllWithVectorIndex(@Autowired VectorSearchRepository repository) { |
| 89 | + StepVerifier.create(repository.findBy("dings", Vector.of(new double[] { 0.1d, 0.1d, 0.1d }))) |
| 90 | + .expectNextCount(2) |
| 91 | + .verifyComplete(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void findAllAsSearchResultsWithVectorIndex(@Autowired VectorSearchRepository repository) { |
| 96 | + StepVerifier.create(repository.findAllBy(Vector.of(new double[] { 0.1d, 0.1d, 0.1d }))) |
| 97 | + .assertNext(result -> assertThat(result.getContent()).isNotNull()) |
| 98 | + .assertNext(result -> assertThat(result.getContent()).isNotNull()) |
| 99 | + .verifyComplete(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void findSingleAsSearchResultWithVectorIndex(@Autowired VectorSearchRepository repository) { |
| 104 | + StepVerifier.create(repository.findBy(Vector.of(new double[] { 0.1d, 0.1d, 0.1d }), Score.of(0.9d))) |
| 105 | + .assertNext(result -> { |
| 106 | + assertThat(result).isNotNull(); |
| 107 | + assertThat(result.getContent()).isNotNull(); |
| 108 | + assertThat(result.getScore().getValue()).isGreaterThanOrEqualTo(0.9d); |
| 109 | + }) |
| 110 | + .verifyComplete(); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void findByNameWithVectorIndex(@Autowired VectorSearchRepository repository) { |
| 115 | + StepVerifier.create(repository.findByName("dings", Vector.of(new double[] { 0.1d, 0.1d, 0.1d }))) |
| 116 | + .expectNextCount(1) |
| 117 | + .verifyComplete(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void findByNameWithVectorIndexAndScore(@Autowired VectorSearchRepository repository) { |
| 122 | + StepVerifier |
| 123 | + .create(repository.findByName("dings", Vector.of(new double[] { -0.7d, 0.0d, -0.7d }), Score.of(0.01d))) |
| 124 | + .expectNextCount(1) |
| 125 | + .verifyComplete(); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void dontFindByNameWithVectorIndexAndScore(@Autowired VectorSearchRepository repository) { |
| 130 | + StepVerifier |
| 131 | + .create(repository.findByName("dings", Vector.of(new double[] { -0.7d, 0.0d, -0.7d }), Score.of(0.8d))) |
| 132 | + .verifyComplete(); |
| 133 | + } |
| 134 | + |
| 135 | + interface VectorSearchRepository extends ReactiveNeo4jRepository<EntityWithVector, String> { |
| 136 | + |
| 137 | + @VectorSearch(indexName = "dingsIndex", numberOfNodes = 2) |
| 138 | + Flux<EntityWithVector> findBy(String name, Vector searchVector); |
| 139 | + |
| 140 | + @VectorSearch(indexName = "dingsIndex", numberOfNodes = 1) |
| 141 | + Flux<EntityWithVector> findByName(String name, Vector searchVector); |
| 142 | + |
| 143 | + @VectorSearch(indexName = "dingsIndex", numberOfNodes = 2) |
| 144 | + Flux<EntityWithVector> findByName(String name, Vector searchVector, Score score); |
| 145 | + |
| 146 | + @VectorSearch(indexName = "dingsIndex", numberOfNodes = 2) |
| 147 | + Flux<SearchResult<EntityWithVector>> findAllBy(Vector searchVector); |
| 148 | + |
| 149 | + @VectorSearch(indexName = "dingsIndex", numberOfNodes = 2) |
| 150 | + Mono<SearchResult<EntityWithVector>> findBy(Vector searchVector, Score score); |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + @Configuration |
| 155 | + @EnableTransactionManagement |
| 156 | + @EnableReactiveNeo4jRepositories(considerNestedRepositories = true) |
| 157 | + static class Config extends Neo4jReactiveTestConfiguration { |
| 158 | + |
| 159 | + @Bean |
| 160 | + @Override |
| 161 | + public Driver driver() { |
| 162 | + return neo4jConnectionSupport.getDriver(); |
| 163 | + } |
| 164 | + |
| 165 | + @Bean |
| 166 | + BookmarkCapture bookmarkCapture() { |
| 167 | + return new BookmarkCapture(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public ReactiveTransactionManager reactiveTransactionManager(Driver driver, |
| 172 | + ReactiveDatabaseSelectionProvider databaseNameProvider) { |
| 173 | + BookmarkCapture bookmarkCapture = bookmarkCapture(); |
| 174 | + return new ReactiveNeo4jTransactionManager(driver, databaseNameProvider, |
| 175 | + Neo4jBookmarkManager.create(bookmarkCapture)); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public boolean isCypher5Compatible() { |
| 180 | + return neo4jConnectionSupport.isCypher5SyntaxCompatible(); |
| 181 | + } |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments