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

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

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

Now you can auto-wire the Neo4jVectorStore as a vector store in your application.spring-doc.cn

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

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

Property Description Default Value

spring.neo4j.urispring-doc.cn

URI for connecting to the Neo4j instancespring-doc.cn

neo4j://localhost:7687spring-doc.cn

spring.neo4j.authentication.usernamespring-doc.cn

Username for authentication with Neo4jspring-doc.cn

neo4jspring-doc.cn

spring.neo4j.authentication.passwordspring-doc.cn

Password for authentication with Neo4jspring-doc.cn

-spring-doc.cn

Properties starting with spring.ai.vectorstore.neo4j.* are used to configure the Neo4jVectorStore:spring-doc.cn

Property Description Default Value

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

Whether to initialize the required schemaspring-doc.cn

falsespring-doc.cn

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

The name of the Neo4j database to usespring-doc.cn

neo4jspring-doc.cn

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

The name of the index to store the vectorsspring-doc.cn

spring-ai-document-indexspring-doc.cn

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

The number of dimensions in the vectorspring-doc.cn

1536spring-doc.cn

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

The distance function to usespring-doc.cn

cosinespring-doc.cn

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

The label used for document nodesspring-doc.cn

Documentspring-doc.cn

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

The property name used to store embeddingsspring-doc.cn

embeddingspring-doc.cn

spring.ai.vectorstore.neo4j.batching-strategyspring-doc.cn

Strategy for batching documents when calculating embeddings. Options are TOKEN_COUNT or FIXED_SIZEspring-doc.cn

TOKEN_COUNTspring-doc.cn

The following distance functions are available:spring-doc.cn

  • cosine - Default, suitable for most use cases. Measures cosine similarity between vectors.spring-doc.cn

  • euclidean - Euclidean distance between vectors. Lower values indicate higher similarity.spring-doc.cn

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: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.

Create a Neo4j Driver bean. Read the Neo4j Documentation 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>"));
}

Then create the Neo4jVectorStore bean using the builder pattern:spring-doc.cn

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

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

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

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: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"

Accessing the Native Client

The Neo4j Vector Store implementation provides access to the underlying native Neo4j client (Driver) through the getNativeClient() method:spring-doc.cn

Neo4jVectorStore vectorStore = context.getBean(Neo4jVectorStore.class);
Optional<Driver> nativeClient = vectorStore.getNativeClient();

if (nativeClient.isPresent()) {
    Driver driver = nativeClient.get();
    // Use the native client for Neo4j-specific operations
}

The native client gives you access to Neo4j-specific features and operations that might not be exposed through the VectorStore interface.spring-doc.cn