Skip to content

Commit baab1c7

Browse files
committed
review changes
Signed-off-by: Gerrit Meier <[email protected]>
1 parent 8586fb2 commit baab1c7

File tree

8 files changed

+33
-25
lines changed

8 files changed

+33
-25
lines changed

src/main/java/org/springframework/data/neo4j/repository/query/PartTreeNeo4jQuery.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ static RepositoryQuery create(Neo4jOperations neo4jOperations, Neo4jMappingConte
5959
Neo4jQueryMethod queryMethod, ProjectionFactory factory) {
6060
if (queryMethod.hasVectorSearchAnnotation()
6161
&& queryMethod.getVectorSearchAnnotation().get().numberOfNodes() < 1) {
62-
throw new IllegalArgumentException("Number of nodes in a vector search has to be greater than zero.");
62+
throw new IllegalArgumentException("Number of nodes in the vector search %s#%s has to be greater than zero."
63+
.formatted(queryMethod.getRepositoryName(), queryMethod.getMethod().getName()));
6364
}
6465
return new PartTreeNeo4jQuery(neo4jOperations, mappingContext, queryMethod,
6566
new PartTree(queryMethod.getName(), getDomainType(queryMethod)), factory);

src/main/java/org/springframework/data/neo4j/repository/query/QueryFragments.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ public Statement toStatement(VectorSearchFragment vectorSearchFragment) {
201201
throw new IllegalStateException("No pattern to match on");
202202
}
203203
var vectorSearch = Cypher.call("db.index.vector.queryNodes")
204-
.withArgs(org.neo4j.cypherdsl.core.Cypher.literalOf(vectorSearchFragment.indexName()),
205-
org.neo4j.cypherdsl.core.Cypher.literalOf(vectorSearchFragment.numberOfNodes()),
206-
org.neo4j.cypherdsl.core.Cypher.parameter(Constants.VECTOR_SEARCH_VECTOR_PARAMETER))
204+
.withArgs(Cypher.literalOf(vectorSearchFragment.indexName()),
205+
Cypher.literalOf(vectorSearchFragment.numberOfNodes()),
206+
Cypher.parameter(Constants.VECTOR_SEARCH_VECTOR_PARAMETER))
207207
.yield("node", "score")
208208
.with(Cypher.name("node").as(((Node) this.matchOn.get(0)).getRequiredSymbolicName().getValue()),
209209
Cypher.name("score").as(Constants.NAME_OF_SCORE));
@@ -233,16 +233,16 @@ public Statement toStatement(VectorSearchFragment vectorSearchFragment) {
233233
}
234234

235235
StatementBuilder.OngoingReadingAndReturn returnPart = isDistinctReturn()
236-
? matchWithWhere.returningDistinct(getReturnExpressions(vectorSearchFragment))
237-
: matchWithWhere.returning(getReturnExpressions(vectorSearchFragment));
236+
? matchWithWhere.returningDistinct(getReturnExpressionsForVectorSearch())
237+
: matchWithWhere.returning(getReturnExpressionsForVectorSearch());
238238

239239
Statement statement = returnPart.orderBy(getOrderBy()).skip(this.skip).limit(this.limit).build();
240240

241241
statement.setRenderConstantsAsParameters(false);
242242
return statement;
243243
}
244244

245-
private Collection<Expression> getReturnExpressions(VectorSearchFragment vectorSearchFragment) {
245+
private Collection<Expression> getReturnExpressionsForVectorSearch() {
246246
return (this.returnExpressions.isEmpty() && this.returnTuple != null) ? CypherGenerator.INSTANCE
247247
.createReturnStatementForMatch((Neo4jPersistentEntity<?>) this.returnTuple.nodeDescription,
248248
this::includeField, this.returnTuple.additionalExpressions.toArray(Expression[]::new))

src/main/java/org/springframework/data/neo4j/repository/query/ReactivePartTreeNeo4jQuery.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ static RepositoryQuery create(ReactiveNeo4jOperations neo4jOperations, Neo4jMapp
5959
Neo4jQueryMethod queryMethod, ProjectionFactory factory) {
6060
if (queryMethod.hasVectorSearchAnnotation()
6161
&& queryMethod.getVectorSearchAnnotation().get().numberOfNodes() < 1) {
62-
throw new IllegalArgumentException("Number of nodes in a vector search has to be greater than zero.");
62+
throw new IllegalArgumentException("Number of nodes in the vector search %s#%s has to be greater than zero."
63+
.formatted(queryMethod.getRepositoryName(), queryMethod.getMethod().getName()));
6364
}
6465
return new ReactivePartTreeNeo4jQuery(neo4jOperations, mappingContext, queryMethod,
6566
new PartTree(queryMethod.getName(), getDomainType(queryMethod)), factory);

src/main/java/org/springframework/data/neo4j/repository/query/VectorSearch.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Indicates a vector search on a repository.
2626
*
2727
* @author Gerrit Meier
28+
* @since 8.0.0
2829
*/
2930
@Retention(RetentionPolicy.RUNTIME)
3031
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })

src/test/java/org/springframework/data/neo4j/integration/imperative/VectorSearchIT.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void removeIndex(@Autowired BookmarkCapture bookmarkCapture, @Autowired Driver d
8787

8888
@Test
8989
void findAllWithVectorIndex(@Autowired VectorSearchRepository repository) {
90-
var result = repository.findBy("dings", Vector.of(new double[] { 0.1d, 0.1d, 0.1d }));
90+
var result = repository.findBy(Vector.of(new double[] { 0.1d, 0.1d, 0.1d }));
9191
assertThat(result).hasSize(2);
9292
}
9393

@@ -110,6 +110,12 @@ void findSingleAsSearchResultWithVectorIndex(@Autowired VectorSearchRepository r
110110
assertThat(result.getScore().getValue()).isGreaterThanOrEqualTo(0.9d);
111111
}
112112

113+
@Test
114+
void findSearchResultsOfSearchResults(@Autowired VectorSearchRepository repository) {
115+
var result = repository.findDistinctByName("dings", Vector.of(new double[] { 0.1d, 0.1d, 0.1d }));
116+
assertThat(result).isNotNull();
117+
}
118+
113119
@Test
114120
void findByNameWithVectorIndex(@Autowired VectorSearchRepository repository) {
115121
var result = repository.findByName("dings", Vector.of(new double[] { 0.1d, 0.1d, 0.1d }));
@@ -134,14 +140,17 @@ interface VectorSearchRepository extends Neo4jRepository<EntityWithVector, Strin
134140
// end::sdn-vector-search.usage[]
135141
// tag::sdn-vector-search.usage.findall[]
136142
@VectorSearch(indexName = "entityIndex", numberOfNodes = 2)
137-
List<EntityWithVector> findBy(String name, Vector searchVector);
143+
List<EntityWithVector> findBy(Vector searchVector);
138144
// end::sdn-vector-search.usage.findall[]
139145

140146
// tag::sdn-vector-search.usage.findbyproperty[]
141147
@VectorSearch(indexName = "entityIndex", numberOfNodes = 1)
142148
List<EntityWithVector> findByName(String name, Vector searchVector);
143149
// end::sdn-vector-search.usage.findbyproperty[]
144150

151+
@VectorSearch(indexName = "entityIndex", numberOfNodes = 1)
152+
List<EntityWithVector> findDistinctByName(String name, Vector searchVector);
153+
145154
@VectorSearch(indexName = "entityIndex", numberOfNodes = 2)
146155
List<EntityWithVector> findByName(String name, Vector searchVector, Score score);
147156

src/test/java/org/springframework/data/neo4j/integration/shared/common/EntityWithVector.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.neo4j.integration.shared.common;
1717

18-
import org.springframework.data.domain.Vector;
1918
import org.springframework.data.neo4j.core.schema.GeneratedValue;
2019
import org.springframework.data.neo4j.core.schema.Id;
2120
import org.springframework.data.neo4j.core.schema.Node;
@@ -32,8 +31,6 @@ public class EntityWithVector {
3231

3332
String name;
3433

35-
Vector myVector;
36-
3734
String getId() {
3835
return this.id;
3936
}
@@ -42,14 +39,6 @@ void setId(String id) {
4239
this.id = id;
4340
}
4441

45-
Vector getMyVector() {
46-
return this.myVector;
47-
}
48-
49-
void setMyVector(Vector myVector) {
50-
this.myVector = myVector;
51-
}
52-
5342
String getName() {
5443
return this.name;
5544
}

src/test/java/org/springframework/data/neo4j/repository/query/ReactiveRepositoryQueryTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,13 @@ void failOnZeroNodesVectorSearchAnnotation() {
166166
ReactiveRepositoryQueryTests.this.neo4jMappingContext, ValueExpressionDelegate.create(),
167167
Configuration.defaultConfig());
168168

169-
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> lookupStrategy.resolveQuery(
170-
reactiveNeo4jQueryMethod("illegalAnnotatedVectorSearch", Vector.class).getMethod(),
171-
TEST_REPOSITORY_METADATA, PROJECTION_FACTORY, ReactiveRepositoryQueryTests.this.namedQueries));
169+
assertThatExceptionOfType(IllegalArgumentException.class)
170+
.isThrownBy(() -> lookupStrategy.resolveQuery(
171+
reactiveNeo4jQueryMethod("illegalAnnotatedVectorSearch", Vector.class).getMethod(),
172+
TEST_REPOSITORY_METADATA, PROJECTION_FACTORY, ReactiveRepositoryQueryTests.this.namedQueries))
173+
.withMessage("Number of nodes in the vector search "
174+
+ "org.springframework.data.neo4j.repository.query.ReactiveRepositoryQueryTests$TestRepository#illegalAnnotatedVectorSearch "
175+
+ "has to be greater than zero.");
172176
}
173177

174178
}

src/test/java/org/springframework/data/neo4j/repository/query/RepositoryQueryTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ void failOnZeroNodesVectorSearchAnnotation() {
295295

296296
assertThatExceptionOfType(IllegalArgumentException.class)
297297
.isThrownBy(() -> lookupStrategy.resolveQuery(queryMethod("illegalAnnotatedVectorSearch", Vector.class),
298-
TEST_REPOSITORY_METADATA, PROJECTION_FACTORY, RepositoryQueryTests.this.namedQueries));
298+
TEST_REPOSITORY_METADATA, PROJECTION_FACTORY, RepositoryQueryTests.this.namedQueries))
299+
.withMessage("Number of nodes in the vector search "
300+
+ "org.springframework.data.neo4j.repository.query.RepositoryQueryTests$TestRepository#illegalAnnotatedVectorSearch "
301+
+ "has to be greater than zero.");
299302
}
300303

301304
@Test

0 commit comments

Comments
 (0)