> ## Documentation Index
> Fetch the complete documentation index at: https://javaai.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Neo4j 向量数据库

Spring AI 中的 Neo4j 向量数据库集成了 Neo4j 基于图的向量功能，提供向量存储和搜索能力。

## 概述

Neo4j 向量数据库支持：

* 基于图的向量存储
* 关系感知搜索
* 实时向量操作
* 图分析集成

## 特性

<CardGroup cols={2}>
  <Card title="基于图" icon="project-diagram">
    图向量存储
  </Card>

  <Card title="关系" icon="link">
    关系感知搜索
  </Card>

  <Card title="实时" icon="clock">
    实时操作
  </Card>

  <Card title="分析" icon="chart-line">
    图分析
  </Card>
</CardGroup>

## 实现

### 基本设置

```xml theme={"system"}
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-neo4j-vectordb</artifactId>
    <version>${spring-ai.version}</version>
</dependency>
```

### 配置

```properties theme={"system"}
# 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
```

### 用法

```java theme={"system"}
@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. 向量存储

```java theme={"system"}
@Configuration
public class Neo4jVectorStorageConfig {
    @Bean
    public VectorStorage neo4jVectorStorage(Neo4jVectorProperties properties) {
        return new Neo4jVectorStorage(properties);
    }
}
```

### 2. 相似性搜索

```java theme={"system"}
@Configuration
public class Neo4jSimilaritySearchConfig {
    @Bean
    public SimilaritySearch neo4jSimilaritySearch(Neo4jVectorProperties properties) {
        return new Neo4jSimilaritySearch(properties);
    }
}
```

### 3. 图操作

```java theme={"system"}
@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);
    }
}
```

## 高级特性

### 自定义节点配置

```java theme={"system"}
@Configuration
public class NodeConfig {
    @Bean
    public NodeConfig nodeConfig() {
        return NodeConfig.builder()
            .label("Vector")
            .properties(Arrays.asList("id", "vector", "metadata"))
            .build();
    }
}
```

### 索引管理

```java theme={"system"}
@Configuration
public class IndexConfig {
    @Bean
    public IndexConfig indexConfig() {
        return IndexConfig.builder()
            .name("vector_index")
            .type(IndexType.VECTOR)
            .dimension(1536)
            .build();
    }
}
```

### 监控

```properties theme={"system"}
management.endpoints.web.exposure.include=neo4j-vectordb
management.endpoint.neo4j-vectordb.enabled=true
```

## 最佳实践

<Note>
  使用 Neo4j 向量数据库时，请考虑以下最佳实践：
</Note>

* **图设计**：设计高效的图结构
* **索引管理**：优化索引配置
* **关系策略**：实施有效的关系
* **资源管理**：配置资源分配
* **监控**：设置全面的监控

## 故障排除

常见问题及解决方案：

1. **连接问题**
   * 验证连接 URI
   * 检查身份验证
   * 查看网络设置

2. **性能问题**
   * 优化图结构
   * 调整索引配置
   * 查看查询模式

3. **存储问题**
   * 监控图大小
   * 执行清理
   * 查看保留策略

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/spring-ai/api/vectordbs/neo4j.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>
