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

# OpenSearch 向量数据库

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

## 概述

OpenSearch 向量数据库支持：

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

## 特性

<CardGroup cols={2}>
  <Card title="分布式" icon="network-wired">
    分布式架构
  </Card>

  <Card title="搜索" icon="search">
    全文搜索
  </Card>

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

  <Card title="可扩展" icon="arrows-up-down-left-right">
    水平扩展
  </Card>
</CardGroup>

## 实现

### 基本设置

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

### 配置

```properties theme={"system"}
# OpenSearch 向量数据库配置
spring.ai.opensearch.vectordb.enabled=true
spring.ai.opensearch.vectordb.uri=http://localhost:9200
spring.ai.opensearch.vectordb.username=admin
spring.ai.opensearch.vectordb.password=admin
spring.ai.opensearch.vectordb.index-name=vectors
```

### 用法

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

    public OpenSearchVectorService(OpenSearchVectorClient 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 OpenSearchVectorStorageConfig {
    @Bean
    public VectorStorage openSearchVectorStorage(OpenSearchVectorProperties properties) {
        return new OpenSearchVectorStorage(properties);
    }
}
```

### 2. 相似性搜索

```java theme={"system"}
@Configuration
public class OpenSearchSimilaritySearchConfig {
    @Bean
    public SimilaritySearch openSearchSimilaritySearch(OpenSearchVectorProperties properties) {
        return new OpenSearchSimilaritySearch(properties);
    }
}
```

### 3. 索引管理

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

    public void createIndex(String name, int dimension) {
        vectorClient.createIndex(name, dimension);
    }

    public void deleteIndex(String name) {
        vectorClient.deleteIndex(name);
    }
}
```

## 高级特性

### 自定义索引配置

```java theme={"system"}
@Configuration
public class IndexConfig {
    @Bean
    public IndexConfig indexConfig() {
        return IndexConfig.builder()
            .name("custom-vectors")
            .dimension(1536)
            .indexType(IndexType.HNSW)
            .metricType(MetricType.COSINE)
            .build();
    }
}
```

### 映射配置

```java theme={"system"}
@Configuration
public class MappingConfig {
    @Bean
    public MappingConfig mappingConfig() {
        return MappingConfig.builder()
            .vectorField("vector")
            .metadataFields(Arrays.asList("title", "content"))
            .build();
    }
}
```

### 监控

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

## 最佳实践

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

* **索引设计**：设计高效的索引结构
* **映射策略**：优化字段映射
* **分片策略**：实施有效的分片
* **资源管理**：配置资源分配
* **监控**：设置全面的监控

## 故障排除

常见问题及解决方案：

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

2. **性能问题**
   * 优化索引配置
   * 调整分片策略
   * 查看查询模式

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

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