Skip to content

Commit 3293a20

Browse files
committed
Neo4jVectorStore updates
- Add integration tests for custom database name and session config Signed-off-by: Ilayaperumal Gopinathan <[email protected]>
1 parent 99127f6 commit 3293a20

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

vector-stores/spring-ai-neo4j-store/src/test/java/org/springframework/ai/vectorstore/neo4j/Neo4jVectorStoreIT.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,104 @@ void getNativeClientTest() {
353353
});
354354
}
355355

356+
@Test
357+
void addWithCustomDatabaseName() {
358+
this.contextRunner.run(context -> {
359+
Driver driver = context.getBean(Driver.class);
360+
EmbeddingModel embeddingModel = context.getBean(EmbeddingModel.class);
361+
362+
// Create VectorStore with custom database name (neo4j is the default
363+
// database)
364+
VectorStore vectorStore = Neo4jVectorStore.builder(driver, embeddingModel)
365+
.databaseName("neo4j")
366+
.initializeSchema(true)
367+
.build();
368+
369+
// Add documents using doAdd (which should respect the sessionConfig)
370+
Document doc = new Document("Test content for custom database", Map.of("testKey", "testValue"));
371+
vectorStore.add(List.of(doc));
372+
373+
// Verify the document was added by querying the specific database directly
374+
// This ensures the sessionConfig was used correctly
375+
try (var session = driver.session(org.neo4j.driver.SessionConfig.forDatabase("neo4j"))) {
376+
var count = session
377+
.run("MATCH (n:Document {id: $id}) RETURN count(n) as count", Map.of("id", doc.getId()))
378+
.single()
379+
.get("count")
380+
.asLong();
381+
assertThat(count).isEqualTo(1);
382+
}
383+
384+
// Verify through the VectorStore API as well
385+
List<Document> results = vectorStore
386+
.similaritySearch(SearchRequest.builder().query("Test content").topK(1).build());
387+
388+
assertThat(results).hasSize(1);
389+
assertThat(results.get(0).getId()).isEqualTo(doc.getId());
390+
assertThat(results.get(0).getText()).isEqualTo("Test content for custom database");
391+
assertThat(results.get(0).getMetadata()).containsEntry("testKey", "testValue");
392+
393+
// Clean up
394+
vectorStore.delete(List.of(doc.getId()));
395+
});
396+
}
397+
398+
@Test
399+
void addWithCustomSessionConfig() {
400+
this.contextRunner.run(context -> {
401+
Driver driver = context.getBean(Driver.class);
402+
EmbeddingModel embeddingModel = context.getBean(EmbeddingModel.class);
403+
404+
// Create VectorStore with custom SessionConfig
405+
var sessionConfig = org.neo4j.driver.SessionConfig.forDatabase("neo4j");
406+
VectorStore vectorStore = Neo4jVectorStore.builder(driver, embeddingModel)
407+
.sessionConfig(sessionConfig)
408+
.initializeSchema(true)
409+
.build();
410+
411+
// Add multiple documents to test batch processing
412+
List<Document> docs = List.of(
413+
new Document("First document with custom session", Map.of("category", "session-test", "index", 1)),
414+
new Document("Second document with custom session", Map.of("category", "session-test", "index", 2)),
415+
new Document("Third document with custom session", Map.of("category", "session-test", "index", 3)));
416+
417+
vectorStore.add(docs);
418+
419+
// Verify documents were added to the correct database by querying directly
420+
try (var session = driver.session(sessionConfig)) {
421+
var count = session
422+
.run("MATCH (n:Document) WHERE n.id IN $ids RETURN count(n) as count",
423+
Map.of("ids", docs.stream().map(Document::getId).toList()))
424+
.single()
425+
.get("count")
426+
.asLong();
427+
assertThat(count).isEqualTo(3);
428+
}
429+
430+
// Verify all documents were added through VectorStore API
431+
List<Document> results = vectorStore.similaritySearch(
432+
SearchRequest.builder().query("document custom session").topK(5).similarityThresholdAll().build());
433+
434+
assertThat(results).hasSize(3);
435+
assertThat(results.stream().map(Document::getId).toList())
436+
.containsExactlyInAnyOrderElementsOf(docs.stream().map(Document::getId).toList());
437+
438+
// Verify we can search with filters
439+
results = vectorStore.similaritySearch(SearchRequest.builder()
440+
.query("document custom session")
441+
.topK(5)
442+
.similarityThresholdAll()
443+
.filterExpression("index == 2")
444+
.build());
445+
446+
assertThat(results).hasSize(1);
447+
assertThat(results.get(0).getMetadata()).containsEntry("index", 2L);
448+
449+
// Clean up
450+
vectorStore.delete(docs.stream().map(Document::getId).toList());
451+
});
452+
}
453+
356454
@Test
357455
void vectorIndexDimensionsDefaultAndOverwriteWorks() {
358456
this.contextRunner.run(context -> {

0 commit comments

Comments
 (0)