This version is still in development and is not considered stable yet. For the latest snapshot version, please use Spring AI 1.0.0-SNAPSHOT! |
Neo4j
This section walks you through setting up Neo4jVectorStore
to store document embeddings and perform similarity searches.
Neo4j is an open-source NoSQL graph database. It is a fully transactional database (ACID) that stores data structured as graphs consisting of nodes, connected by relationships. Inspired by the structure of the real world, it allows for high query performance on complex data while remaining intuitive and simple for the developer.
The Neo4j’s Vector Search allows users to query vector embeddings from large datasets.
An embedding is a numerical representation of a data object, such as text, image, audio, or document.
Embeddings can be stored on Node properties and can be queried with the db.index.vector.queryNodes()
function.
Those indexes are powered by Lucene using a Hierarchical Navigable Small World Graph (HNSW) to perform a k approximate nearest neighbors (k-ANN) query over the vector fields.
Prerequisites
-
A running Neo4j (5.15+) instance. The following options are available:
-
Docker image
-
Neo4j Server instance
-
-
If required, an API key for the EmbeddingModel to generate the embeddings stored by the
Neo4jVectorStore
.
Auto-configuration
Spring AI provides Spring Boot auto-configuration for the Neo4j Vector Store.
To enable it, add the following dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-neo4j-store-spring-boot-starter</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-neo4j-store-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Please have a look at the list of configuration parameters for the vector store to learn about the default values and configuration options.
Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file. |
The vector store implementation can initialize the requisite schema for you, but you must opt-in by specifying the initializeSchema
boolean in the appropriate constructor or by setting …initialize-schema=true
in the application.properties
file.
this is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default. |
Additionally, you will need a configured EmbeddingModel
bean. Refer to the EmbeddingModel section for more information.
Now you can auto-wire the Neo4jVectorStore
as a vector store in your application.
@Autowired VectorStore vectorStore;
// ...
List<Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
new Document("The World is Big and Salvation Lurks Around the Corner"),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
// Add the documents to Neo4j
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
Configuration Properties
To connect to Neo4j and use the Neo4jVectorStore
, you need to provide access details for your instance.
A simple configuration can be provided via Spring Boot’s application.yml
:
spring:
neo4j:
uri: <neo4j instance URI>
authentication:
username: <neo4j username>
password: <neo4j password>
ai:
vectorstore:
neo4j:
initialize-schema: true
database-name: neo4j
index-name: custom-index
dimensions: 1536
distance-type: cosine
batching-strategy: TOKEN_COUNT # Optional: Controls how documents are batched for embedding
The Spring Boot properties starting with spring.neo4j.*
are used to configure the Neo4j client:
Property | Description | Default Value |
---|---|---|
|
URI for connecting to the Neo4j instance |
|
|
Username for authentication with Neo4j |
|
|
Password for authentication with Neo4j |
- |
Properties starting with spring.ai.vectorstore.neo4j.*
are used to configure the Neo4jVectorStore
:
Property | Description | Default Value |
---|---|---|
|
Whether to initialize the required schema |
|
|
The name of the Neo4j database to use |
|
|
The name of the index to store the vectors |
|
|
The number of dimensions in the vector |
|
|
The distance function to use |
|
|
The label used for document nodes |
|
|
The property name used to store embeddings |
|
|
Strategy for batching documents when calculating embeddings. Options are |
|
The following distance functions are available:
-
cosine
- Default, suitable for most use cases. Measures cosine similarity between vectors. -
euclidean
- Euclidean distance between vectors. Lower values indicate higher similarity.
Manual Configuration
Instead of using the Spring Boot auto-configuration, you can manually configure the Neo4j vector store. For this you need to add the spring-ai-neo4j-store
to your project:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-neo4j-store</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-neo4j-store'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Create a Neo4j Driver
bean.
Read the Neo4j Documentation for more in-depth information about the configuration of a custom driver.
@Bean
public Driver driver() {
return GraphDatabase.driver("neo4j://<host>:<bolt-port>",
AuthTokens.basic("<username>", "<password>"));
}
Then create the Neo4jVectorStore
bean using the builder pattern:
@Bean
public VectorStore vectorStore(Driver driver, EmbeddingModel embeddingModel) {
return Neo4jVectorStore.builder(driver, embeddingModel)
.databaseName("neo4j") // Optional: defaults to "neo4j"
.distanceType(Neo4jDistanceType.COSINE) // Optional: defaults to COSINE
.dimensions(1536) // Optional: defaults to 1536
.label("Document") // Optional: defaults to "Document"
.embeddingProperty("embedding") // Optional: defaults to "embedding"
.indexName("custom-index") // Optional: defaults to "spring-ai-document-index"
.initializeSchema(true) // Optional: defaults to false
.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
.build();
}
// This can be any EmbeddingModel implementation
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
Metadata Filtering
You can leverage the generic, portable metadata filters with Neo4j store as well.
For example, you can use either the text expression language:
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'").build());
or programmatically using the Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("author", "john", "jill"),
b.eq("article_type", "blog")).build()).build());
Those (portable) filter expressions get automatically converted into the proprietary Neo4j WHERE filter expressions.
|
For example, this portable filter expression:
author in ['john', 'jill'] && 'article_type' == 'blog'
is converted into the proprietary Neo4j filter format:
node.`metadata.author` IN ["john","jill"] AND node.`metadata.'article_type'` = "blog"