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!spring-doc.cn

Neo4j

This section walks you through setting up Neo4jVectorStore to store document embeddings and perform similarity searches.spring-doc.cn

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.spring-doc.cn

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.spring-doc.cn

Prerequisites

Dependencies

Add the Neo4j Vector Store dependency to your project:spring-doc.cn

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-neo4j-store</artifactId>
</dependency>

or to your Gradle build.gradle build file.spring-doc.cn

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.spring-doc.cn

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-doc.cn

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,spring-doc.cn

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.spring-doc.cn

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.spring-doc.cn

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:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-neo4j-store-spring-boot-starter</artifactId>
</dependency>

or to your Gradle build.gradle build file.spring-doc.cn

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.spring-doc.cn

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.spring-doc.cn

Here is an example of the needed bean:spring-doc.cn

@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.spring-doc.cn

@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.spring-doc.cn

Metadata filtering

You can leverage the generic, portable metadata filters with Neo4j store as well.spring-doc.cn

For example, you can use either the text expression language:spring-doc.cn

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:spring-doc.cn

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:spring-doc.cn

author in ['john', 'jill'] && 'article_type' == 'blog'

is converted into the proprietary Neo4j filter format:spring-doc.cn

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.spring-doc.cn

Property Default value

spring.ai.vectorstore.neo4j.database-namespring-doc.cn

neo4jspring-doc.cn

spring.ai.vectorstore.neo4j.initialize-schemaspring-doc.cn

falsespring-doc.cn

spring.ai.vectorstore.neo4j.embedding-dimensionspring-doc.cn

1536spring-doc.cn

spring.ai.vectorstore.neo4j.distance-typespring-doc.cn

cosinespring-doc.cn

spring.ai.vectorstore.neo4j.labelspring-doc.cn

Documentspring-doc.cn

spring.ai.vectorstore.neo4j.embedding-propertyspring-doc.cn

embeddingspring-doc.cn

spring.ai.vectorstore.neo4j.index-namespring-doc.cn

spring-ai-document-indexspring-doc.cn