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

# Azure AI 搜索向量数据库

Spring AI 中的 Azure AI 搜索 (以前称为 Azure Cognitive Search) 向量数据库集成了 Azure 的向量数据库服务，提供向量存储和搜索能力。

## 概述

Azure AI 搜索向量数据库支持：

* 向量存储和索引
* 相似性搜索
* 实时向量操作
* 可扩展向量管理

## 特性

<CardGroup cols={2}>
  <Card title="向量存储" icon="database">
    高效的向量存储
  </Card>

  <Card title="相似性搜索" icon="magnifying-glass">
    快速相似性搜索
  </Card>

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

  <Card title="集成" icon="plug">
    轻松集成 Spring
  </Card>
</CardGroup>

## 实现

### 基本设置

```xml theme={"system"}
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-ai-search-store</artifactId>  <!-- 注意 artifactId 更新 -->
    <version>${spring-ai.version}</version>
</dependency>
```

### 配置

```properties theme={"system"}
# Azure AI 搜索向量数据库配置
spring.ai.vectorstore.azure.aisearch.endpoint=YOUR_AZURE_AI_SEARCH_ENDPOINT # 例如 https://your-service-name.search.windows.net
spring.ai.vectorstore.azure.aisearch.api-key=YOUR_AZURE_AI_SEARCH_API_KEY
spring.ai.vectorstore.azure.aisearch.index-name=vector-store-index # 要使用的索引名称
# spring.ai.vectorstore.azure.aisearch.create-index-if-not-exists=true # 如果索引不存在则创建 (可选, 默认为 false)
# spring.ai.vectorstore.azure.aisearch.embedding-dimension=1536 # 嵌入维度 (如果 create-index-if-not-exists=true 则需要)
```

### 用法

```java theme={"system"}
@Service
public class VectorService {
    private final AzureAiSearchVectorStore vectorStore;

    public VectorService(AzureAiSearchVectorStore vectorStore) {
        this.vectorStore = vectorStore;
    }

    public void storeVector(String id, float[] vector, Map<String, Object> metadata) {
        // 注意：Azure AI 搜索通常从嵌入客户端获取嵌入
        // 此处仅为示例，实际使用时通常直接添加 Document 对象
        Document document = new Document(id, "Content related to vector", metadata);
        // 假设嵌入由 AzureAiSearchVectorStore 内部处理或您已配置 EmbeddingClient
        vectorStore.add(List.of(document));
    }

    public List<Document> searchSimilar(String query, int k) {
        return vectorStore.similaritySearch(SearchRequest.query(query).withTopK(k));
    }
}
```

## 向量操作

### 1. 向量存储

`AzureAiSearchVectorStore` 直接与 `EmbeddingClient` 集成以在添加文档时生成嵌入。

```java theme={"system"}
@Configuration
public class VectorStorageConfig {
    // AzureAiSearchVectorStore bean 通常由 Spring Boot 自动配置
    // 如果需要，您可以自定义它
}
```

### 2. 相似性搜索

```java theme={"system"}
@Service
public class SimilaritySearchService {
    private final AzureAiSearchVectorStore vectorStore;

    public SimilaritySearchService(AzureAiSearchVectorStore vectorStore) {
        this.vectorStore = vectorStore;
    }

    public List<Document> findSimilar(String queryText, int topK) {
        return vectorStore.similaritySearch(SearchRequest.query(queryText).withTopK(topK));
    }
}
```

### 3. 批量操作

`AzureAiSearchVectorStore` 支持批量添加文档。

```java theme={"system"}
@Service
public class BatchVectorService {
    private final AzureAiSearchVectorStore vectorStore;

    public BatchVectorService(AzureAiSearchVectorStore vectorStore) {
        this.vectorStore = vectorStore;
    }

    public void batchStore(List<Document> documents) {
        vectorStore.add(documents);
    }
}
```

## 高级特性

### 自定义索引配置

如果 `spring.ai.vectorstore.azure.aisearch.create-index-if-not-exists` 设置为 `true`，则会自动创建索引。
嵌入维度 (`spring.ai.vectorstore.azure.aisearch.embedding-dimension`) 必须与您的 `EmbeddingClient` 生成的维度匹配。
有关更高级的索引自定义，请参阅 [Azure AI 搜索文档](https://docs.microsoft.com/azure/search/search-what-is-azure-search)。

### 监控

Azure AI 搜索提供强大的监控功能，可以通过 Azure 门户访问。

## 最佳实践

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

* **索引设计**：为您的数据和查询模式设计高效的索引。
* **API 密钥管理**：安全地管理您的 API 密钥。
* **批量处理**：尽可能使用批量操作以提高效率。
* **错误处理**：实施稳健的错误处理。
* **监控**：利用 Azure 门户中的监控功能。

## 故障排除

常见问题及解决方案：

1. **连接问题**
   * 验证端点和 API 密钥。
   * 检查网络连接性。
   * 查看防火墙设置。

2. **性能问题**
   * 优化索引配置。
   * 考虑扩展您的 Azure AI 搜索服务。
   * 查看查询复杂性。

3. **存储问题**
   * 监控存储使用情况。
   * 实施清理策略。
   * 查看 Azure AI 搜索服务的限制。

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