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

# Couchbase 向量数据库

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

## 概述

Couchbase 向量数据库支持：

* 分布式向量存储
* 全文搜索集成
* 实时向量操作
* 多维扩展

## 特性

<CardGroup cols={2}>
  <Card title="分布式存储" icon="network-wired">
    分布式向量存储
  </Card>

  <Card title="搜索集成" icon="magnifying-glass">
    全文搜索
  </Card>

  <Card title="可扩展性" icon="arrows-up-down-left-right">
    多维扩展
  </Card>

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

## 实现

### 基本设置

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

### 配置

```properties theme={"system"}
# Couchbase 向量数据库配置
spring.ai.couchbase.vectordb.enabled=true
spring.ai.couchbase.vectordb.connection-string=couchbase://localhost
spring.ai.couchbase.vectordb.bucket-name=vectordb
spring.ai.couchbase.vectordb.scope-name=vectors
spring.ai.couchbase.vectordb.collection-name=vectors
```

### 用法

```java theme={"system"}
@Service
public class CouchbaseVectorService {
    private final CouchbaseVectorClient vectorClient;

    public CouchbaseVectorService(CouchbaseVectorClient 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 CouchbaseVectorStorageConfig {
    @Bean
    public VectorStorage couchbaseVectorStorage(CouchbaseVectorProperties properties) {
        return new CouchbaseVectorStorage(properties);
    }
}
```

### 2. 相似性搜索

```java theme={"system"}
@Configuration
public class CouchbaseSimilaritySearchConfig {
    @Bean
    public SimilaritySearch couchbaseSimilaritySearch(CouchbaseVectorProperties properties) {
        return new CouchbaseSimilaritySearch(properties);
    }
}
```

### 3. 全文搜索集成

```java theme={"system"}
@Service
public class FullTextSearchService {
    private final CouchbaseVectorClient vectorClient;

    public List<SearchResult> searchWithText(String text, float[] queryVector, int k) {
        return vectorClient.searchWithText(text, queryVector, k);
    }
}
```

## 高级特性

### 自定义索引配置

```java theme={"system"}
@Configuration
public class IndexConfig {
    @Bean
    public IndexConfig indexConfig() {
        return IndexConfig.builder()
            .name("vector-index")
            .dimension(1536)
            .metric(MetricType.COSINE)
            .build();
    }
}
```

### Bucket 配置

```java theme={"system"}
@Configuration
public class BucketConfig {
    @Bean
    public BucketConfig bucketConfig() {
        return BucketConfig.builder()
            .name("vectordb")
            .memoryQuota(1024)
            .build();
    }
}
```

### 监控

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

## 最佳实践

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

* **集群设计**：设计高效的集群拓扑
* **索引管理**：优化索引配置
* **内存管理**：配置适当的内存配额
* **复制**：设置适当的复制
* **监控**：实施全面的监控

## 故障排除

常见问题及解决方案：

1. **连接问题**
   * 验证连接字符串
   * 检查集群状态
   * 查看身份验证

2. **性能问题**
   * 优化索引配置
   * 调整内存配额
   * 查看查询模式

3. **存储问题**
   * 监控存储桶使用情况
   * 配置清理
   * 查看保留策略

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