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

# Qdrant 向量数据库

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

## 概述

Qdrant 向量数据库支持：

* 高性能向量存储
* 实时相似性搜索
* 筛选搜索
* 可扩展架构

## 特性

<CardGroup cols={2}>
  <Card title="性能" icon="tachometer-alt">
    高性能存储
  </Card>

  <Card title="筛选" icon="filter">
    筛选搜索
  </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-qdrant-vectordb</artifactId>
    <version>${spring-ai.version}</version>
</dependency>
```

### 配置

```properties theme={"system"}
# Qdrant 向量数据库配置
spring.ai.qdrant.vectordb.enabled=true
spring.ai.qdrant.vectordb.host=localhost
spring.ai.qdrant.vectordb.port=6333
spring.ai.qdrant.vectordb.collection-name=vectors
```

### 用法

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

    public QdrantVectorService(QdrantVectorClient 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 QdrantVectorStorageConfig {
    @Bean
    public VectorStorage qdrantVectorStorage(QdrantVectorProperties properties) {
        return new QdrantVectorStorage(properties);
    }
}
```

### 2. 相似性搜索

```java theme={"system"}
@Configuration
public class QdrantSimilaritySearchConfig {
    @Bean
    public SimilaritySearch qdrantSimilaritySearch(QdrantVectorProperties properties) {
        return new QdrantSimilaritySearch(properties);
    }
}
```

### 3. 集合管理

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

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

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

## 高级特性

### 自定义集合配置

```java theme={"system"}
@Configuration
public class CollectionConfig {
    @Bean
    public CollectionConfig collectionConfig() {
        return CollectionConfig.builder()
            .name("custom-vectors")
            .dimension(1536)
            .distanceType(DistanceType.COSINE)
            .build();
    }
}
```

### 筛选器配置

```java theme={"system"}
@Configuration
public class FilterConfig {
    @Bean
    public FilterConfig filterConfig() {
        return FilterConfig.builder()
            .must(Arrays.asList("category", "status"))
            .should(Arrays.asList("tags"))
            .build();
    }
}
```

### 监控

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

## 最佳实践

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

* **集合设计**：设计高效的集合结构
* **筛选策略**：优化筛选器配置
* **距离度量**：选择合适的距离度量
* **资源管理**：配置资源分配
* **监控**：设置全面的监控

## 故障排除

常见问题及解决方案：

1. **连接问题**
   * 验证主机和端口
   * 检查网络连接性
   * 查看身份验证

2. **性能问题**
   * 优化集合配置
   * 调整筛选策略
   * 查看查询模式

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

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