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
.
Dependencies
Add the Neo4j Vector Store dependency 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. |
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. |
Configuration
To connect to Neo4j and use the Neo4jVectorStore
, you need to provide access details for your instance.
A simple configuration can either be provided via Spring Boot’s application.properties,
spring.neo4j.uri=<uri_for_your_neo4j_instance>
spring.neo4j.authentication.username=<your_username>
spring.neo4j.authentication.password=<your_password>
# API key if needed, e.g. OpenAI
spring.ai.openai.api.key=<api-key>
environment variables,
export SPRING_NEO4J_URI=<uri_for_your_neo4j_instance>
export SPRING_NEO4J_AUTHENTICATION_USERNAME=<your_username>
export SPRING_NEO4J_AUTHENTICATION_PASSWORD=<your_password>
# API key if needed, e.g. OpenAI
export SPRING_AI_OPENAI_API_KEY=<api-key>
or can be a mix of those. For example, if you want to store your API key as an environment variable but keep the rest in the plain application.properties file.
If you choose to create a shell script for ease in future work, be sure to run it prior to starting your application by "sourcing" the file, i.e. source <your_script_name>.sh .
|
Besides application.properties and environment variables, Spring Boot offers additional configuration options. |
Spring Boot’s auto-configuration feature for the Neo4j Driver will create a bean instance that will be used 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. |
Additionally, you will need a configured EmbeddingModel
bean. Refer to the EmbeddingModel section for more information.
Here is an example of the needed bean:
@Bean
public EmbeddingModel embeddingModel() {
// Can be any other Embeddingmodel implementation.
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
}
In cases where the Spring Boot auto-configured Neo4j Driver
bean is not what you want or need, you can still define your own bean.
Please read the Neo4j Java Driver reference 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>"));
}
Now you can auto-wire the Neo4jVectorStore
as a vector store in your application.
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.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'"));
or programmatically using the Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("author", "john", "jill"),
b.eq("article_type", "blog")).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"
Neo4jVectorStore properties
You can use the following properties in your Spring Boot configuration to customize the Neo4j vector store.
Property | Default value |
---|---|
|
neo4j |
|
false |
|
1536 |
|
cosine |
|
Document |
|
embedding |
|
spring-ai-document-index |