Spring AI 中的 Neo4j 向量数据库集成了 Neo4j 基于图的向量功能,提供向量存储和搜索能力。
Neo4j 向量数据库支持:
基于图的向量存储
关系感知搜索
实时向量操作
图分析集成
基本设置
< dependency >
< groupId > org.springframework.ai </ groupId >
< artifactId > spring-ai-neo4j-vectordb </ artifactId >
< version > ${spring-ai.version} </ version >
</ dependency >
# Neo4j 向量数据库配置
spring.ai.neo4j.vectordb.enabled =true
spring.ai.neo4j.vectordb.uri =bolt://localhost:7687
spring.ai.neo4j.vectordb.username =neo4j
spring.ai.neo4j.vectordb.password =password
spring.ai.neo4j.vectordb.database =vectordb
@ Service
public class Neo4jVectorService {
private final Neo4jVectorClient vectorClient ;
public Neo4jVectorService ( Neo4jVectorClient vectorClient ) {
this . vectorClient = vectorClient;
}
public void storeVector ( String id , float [] vector , Map < String , Object > metadata ) {
vectorClient . store (id, vector, metadata);
}
public List < SearchResult > searchSimilar ( float [] queryVector , int k ) {
return vectorClient . search (queryVector, k);
}
}
向量操作
1. 向量存储
@ Configuration
public class Neo4jVectorStorageConfig {
@ Bean
public VectorStorage neo4jVectorStorage ( Neo4jVectorProperties properties ) {
return new Neo4jVectorStorage (properties);
}
}
2. 相似性搜索
@ Configuration
public class Neo4jSimilaritySearchConfig {
@ Bean
public SimilaritySearch neo4jSimilaritySearch ( Neo4jVectorProperties properties ) {
return new Neo4jSimilaritySearch (properties);
}
}
3. 图操作
@ Service
public class GraphService {
private final Neo4jVectorClient vectorClient ;
public void createVectorNode ( String id , float [] vector ) {
vectorClient . createNode (id, vector);
}
public void createRelationship ( String fromId , String toId , String type ) {
vectorClient . createRelationship (fromId, toId, type);
}
}
高级特性
自定义节点配置
@ Configuration
public class NodeConfig {
@ Bean
public NodeConfig nodeConfig () {
return NodeConfig . builder ()
. label ( "Vector" )
. properties ( Arrays . asList ( "id" , "vector" , "metadata" ))
. build ();
}
}
索引管理
@ Configuration
public class IndexConfig {
@ Bean
public IndexConfig indexConfig () {
return IndexConfig . builder ()
. name ( "vector_index" )
. type ( IndexType . VECTOR )
. dimension ( 1536 )
. build ();
}
}
management.endpoints.web.exposure.include =neo4j-vectordb
management.endpoint.neo4j-vectordb.enabled =true
最佳实践
使用 Neo4j 向量数据库时,请考虑以下最佳实践:
图设计 :设计高效的图结构
索引管理 :优化索引配置
关系策略 :实施有效的关系
资源管理 :配置资源分配
监控 :设置全面的监控
故障排除
常见问题及解决方案:
连接问题
性能问题
存储问题
文档有误?请协助编辑 发现文档问题?点击此处直接在 GitHub 上编辑并提交 PR,帮助我们改进文档!