Class Neo4jVectorStore
java.lang.Object
org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
org.springframework.ai.vectorstore.neo4j.Neo4jVectorStore
- All Implemented Interfaces:
Consumer<List<Document>>,DocumentWriter,VectorStore,VectorStoreRetriever,org.springframework.beans.factory.InitializingBean
public class Neo4jVectorStore
extends AbstractObservationVectorStore
implements org.springframework.beans.factory.InitializingBean
Neo4j-based vector store implementation using Neo4j's vector search capabilities.
The store uses Neo4j's vector search functionality to persist and query vector embeddings along with their associated document content and metadata. The implementation leverages Neo4j's HNSW (Hierarchical Navigable Small World) algorithm for efficient k-NN search operations.
Features:
- Automatic schema initialization with configurable index creation
- Support for multiple distance functions: Cosine and Euclidean
- Metadata filtering using Neo4j's WHERE clause expressions
- Configurable similarity thresholds for search results
- Batch processing support with configurable strategies
- Observation and metrics support through Micrometer
Basic usage example:
Neo4jVectorStore vectorStore = Neo4jVectorStore.builder(driver, embeddingModel)
.initializeSchema(true)
.build();
// Add documents
vectorStore.add(List.of(
new Document("content1", Map.of("key1", "value1")),
new Document("content2", Map.of("key2", "value2"))
));
// Search with filters
List<Document> results = vectorStore.similaritySearch(
SearchRequest.query("search text")
.withTopK(5)
.withSimilarityThreshold(0.7)
.withFilterExpression("key1 == 'value1'")
);
Advanced configuration example:
Neo4jVectorStore vectorStore = Neo4jVectorStore.builder(driver, embeddingModel)
.databaseName("neo4j")
.distanceType(Neo4jDistanceType.COSINE)
.dimensions(1536)
.label("CustomDocument")
.embeddingProperty("vector")
.indexName("custom-vectors")
.initializeSchema(true)
.batchingStrategy(new TokenCountBatchingStrategy())
.build();
Requirements:
- Neo4j 5.15 or later
- Node schema with id (string), text (string), metadata (object), and embedding (vector) properties
Distance Functions:
- cosine: Default, suitable for most use cases. Measures cosine similarity between vectors.
- euclidean: Euclidean distance between vectors. Lower values indicate higher similarity.
- Since:
- 1.0.0
- Author:
- Gerrit Meier, Michael Simons, Christian Tzolov, Thomas Vitale, Soby Chacko, Jihoon Kim
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enumAn enum to configure the distance function used in the Neo4j vector index. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final intFields inherited from class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
batchingStrategy, embeddingModel -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidstatic Neo4jVectorStore.Builderbuilder(org.neo4j.driver.Driver driver, EmbeddingModel embeddingModel) createObservationContextBuilder(String operationName) Create a newVectorStoreObservationContext.Builderinstance.voidPerform the actual add operation.voidPerform the actual delete operation.protected voiddoDelete(Filter.Expression filterExpression) Template method for concrete implementations to provide filter-based deletion logic.doSimilaritySearch(SearchRequest request) Perform the actual similarity search operation.<T> Optional<T> Returns the native client if available in this vector store implementation.Methods inherited from class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
add, delete, delete, similaritySearchMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.document.DocumentWriter
writeMethods inherited from interface org.springframework.ai.vectorstore.VectorStore
accept, delete, getNameMethods inherited from interface org.springframework.ai.vectorstore.VectorStoreRetriever
similaritySearch
-
Field Details
-
DEFAULT_TRANSACTION_SIZE
public static final int DEFAULT_TRANSACTION_SIZE- See Also:
-
DEFAULT_LABEL
- See Also:
-
DEFAULT_INDEX_NAME
- See Also:
-
DEFAULT_EMBEDDING_PROPERTY
- See Also:
-
DEFAULT_ID_PROPERTY
- See Also:
-
DEFAULT_TEXT_PROPERTY
- See Also:
-
DEFAULT_CONSTRAINT_NAME
- See Also:
-
-
Constructor Details
-
Neo4jVectorStore
-
-
Method Details
-
doAdd
Description copied from class:AbstractObservationVectorStorePerform the actual add operation.- Specified by:
doAddin classAbstractObservationVectorStore- Parameters:
documents- the documents to add
-
doDelete
Description copied from class:AbstractObservationVectorStorePerform the actual delete operation.- Specified by:
doDeletein classAbstractObservationVectorStore- Parameters:
idList- the list of document IDs to delete
-
doDelete
Description copied from class:AbstractObservationVectorStoreTemplate method for concrete implementations to provide filter-based deletion logic.- Overrides:
doDeletein classAbstractObservationVectorStore- Parameters:
filterExpression- Filter expression to identify documents to delete
-
doSimilaritySearch
Description copied from class:AbstractObservationVectorStorePerform the actual similarity search operation.- Specified by:
doSimilaritySearchin classAbstractObservationVectorStore- Parameters:
request- the search request- Returns:
- the list of documents that match the query request conditions
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
createObservationContextBuilder
Description copied from class:AbstractObservationVectorStoreCreate a newVectorStoreObservationContext.Builderinstance.- Specified by:
createObservationContextBuilderin classAbstractObservationVectorStore- Parameters:
operationName- the operation name- Returns:
- the observation context builder
-
getNativeClient
Description copied from interface:VectorStoreReturns the native client if available in this vector store implementation. Note on usage: 1. Returns empty Optional when no native client is available 2. Due to Java type erasure, runtime type checking is not possible Example usage: When working with implementation with known native client: Optionalclient = vectorStore.getNativeClient(); Note: Using Optionalinvalid input: '<'?> will return the native client if one exists, rather than an empty Optional. For type safety, prefer using the specific client type. - Specified by:
getNativeClientin interfaceVectorStore- Type Parameters:
T- The type of the native client- Returns:
- Optional containing native client if available, empty Optional otherwise
-
builder
public static Neo4jVectorStore.Builder builder(org.neo4j.driver.Driver driver, EmbeddingModel embeddingModel)
-