# 带有 Qdrant 的自封矢量数据库: 深海底

<datetime class="hidden">2025-11-23T13:00</datetime>

<!-- category -- ASP.NET, Semantic Search, Vector Databases, Qdrant, RAG, AI-Article -->
# 一. 导言 导言 导言 导言 导言 导言 一,导言 导言 导言 导言 导言 导言

**与RAG系列有关:** 本文章提供对Qdrant的深度下潜,Qdrant是用于下列用途的矢量数据库:

- [第4部分:ONNX和Qdrant 执行](/blog/semantic-search-with-onnx-and-qdrant) - 建立语义搜索
- [第5部分:混合搜索和自动插入](/blog/rag-hybrid-search-and-indexing) - 生产一体化

[解冻](https://qdrant.tech/) 本文包含核心概念、 C# 客户端、 性能调控、 生产模式等。

[TOC]

# 什么是 Qdrant ?

A A A [矢量数据库](https://qdrant.tech/documentation/overview/) 与找到精确匹配点的传统数据库不同, Qdrant 发现 *字义相似* 项目。

```mermaid
flowchart LR
    A[Text: 'Docker deployment'] --> B[Embedding Model]
    B --> C["Vector: [0.12, -0.34, 0.56, ...]"]
    C --> D[Qdrant]
    E[Query: 'container setup'] --> F[Embedding Model]
    F --> G["Vector: [0.11, -0.32, 0.58, ...]"]
    G --> H[Similarity Search]
    D --> H
    H --> I[Similar Results]

    style B stroke:#6366f1,stroke-width:3px
    style D stroke:#ef4444,stroke-width:3px
    style F stroke:#6366f1,stroke-width:3px
    style H stroke:#10b981,stroke-width:2px
```

**密钥 Qdrant 特性 :**

- [HNSW 指数化](https://qdrant.tech/documentation/concepts/indexing/) - 亚线性搜索时间
- [过滤过滤](https://qdrant.tech/documentation/concepts/filtering/) - 将相似搜索与元数据过滤器相结合
- [gRPC 和REST APPs 税号](https://qdrant.tech/documentation/interfaces/) - 高绩效准入
- [分配部署部署](https://qdrant.tech/documentation/guides/distributed_deployment/) - 水平水平缩放
- [抓图](https://qdrant.tech/documentation/concepts/snapshots/) - 备份和恢复

# 核心概念核心概念

## 实收款

A A A [收藏收藏](https://qdrant.tech/documentation/concepts/collections/) 象一张表格它持有具有固定维度和距离度的矢量。

```mermaid
flowchart TB
    subgraph Collection["Collection: blog_posts"]
        A[Vector Size: 384]
        B[Distance: Cosine]
        C[HNSW Index]
    end

    subgraph Points
        D[Point 1: slug=docker-intro]
        E[Point 2: slug=kubernetes-basics]
        F[Point N...]
    end

    Collection --> Points

    style A stroke:#6366f1,stroke-width:2px
    style B stroke:#6366f1,stroke-width:2px
    style C stroke:#f59e0b,stroke-width:2px
    style D stroke:#10b981,stroke-width:2px
    style E stroke:#10b981,stroke-width:2px
```

```csharp
// Create collection - see https://qdrant.tech/documentation/concepts/collections/#create-a-collection
await client.CreateCollectionAsync(
    collectionName: "blog_posts",
    vectorsConfig: new VectorParams
    {
        Size = 384,              // Must match your embedding model
        Distance = Distance.Cosine  // Best for text embeddings
    }
);
```

**远程度量** ([医生数](https://qdrant.tech/documentation/concepts/collections/#distance-metrics)):

- **余弦** - 衡量矢量之间的角度(文字最好)
- **点点** - 原始内部产品(用于预先正常化的病媒)
- **欧元** - 几何距离(空间数据)

## 点点

A A A [点点](https://qdrant.tech/documentation/concepts/points/) 是包含以下内容的单一记录:

```mermaid
flowchart LR
    subgraph Point
        A[ID: uuid/int]
        B["Vector: float[384]"]
        C[Payload: JSON metadata]
    end

    style A stroke:#8b5cf6,stroke-width:2px
    style B stroke:#f59e0b,stroke-width:2px
    style C stroke:#10b981,stroke-width:2px
```

```csharp
// Upsert points - see https://qdrant.tech/documentation/concepts/points/#upload-points
var point = new PointStruct
{
    Id = new PointId { Uuid = Guid.NewGuid().ToString() },
    Vectors = embedding,  // float[384]
    Payload =
    {
        ["slug"] = "my-post",
        ["title"] = "Vector Databases",
        ["language"] = "en",
        ["categories"] = new[] { "AI", "Databases" },
        ["published"] = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
    }
};

await client.UpsertAsync("blog_posts", points: new[] { point });
```

## 过滤过滤

[过滤过滤](https://qdrant.tech/documentation/concepts/filtering/) 运行中 *之前* 相似性搜索 -- -- 效率极高。

```mermaid
flowchart TB
    A[Search Query] --> B{Apply Filters First}
    B --> C[Language = 'en']
    B --> D[Year >= 2024]
    C --> E[Filtered Subset]
    D --> E
    E --> F[Vector Similarity Search]
    F --> G[Ranked Results]

    style B stroke:#ec4899,stroke-width:3px
    style E stroke:#f59e0b,stroke-width:2px
    style F stroke:#6366f1,stroke-width:2px
    style G stroke:#10b981,stroke-width:2px
```

```csharp
// Filter conditions - see https://qdrant.tech/documentation/concepts/filtering/#filtering-conditions
var filter = new Filter
{
    Must =  // AND conditions
    {
        new Condition { Field = new FieldCondition
        {
            Key = "language",
            Match = new Match { Keyword = "en" }
        }},
        new Condition { Field = new FieldCondition
        {
            Key = "published",
            Range = new Range { Gte = 1704067200 }  // 2024-01-01
        }}
    },
    MustNot =  // Exclude conditions
    {
        new Condition { Field = new FieldCondition
        {
            Key = "slug",
            Match = new Match { Keyword = "draft-post" }
        }}
    }
};
```

**过滤器类型** ([医生数](https://qdrant.tech/documentation/concepts/filtering/#match)):

- `Match.Keyword` - 精确的字符串匹配
- `Match.Text` - 全文匹配
- `Match.Any` - 在数组中匹配任意
- `Range` - 数值范围(Gte、Lte、Gt、Lt)
- `GeoBoundingBox` / `GeoRadius` - 地理过滤

# C# 客户端

安装官方 [Qdrant. 流利](https://www.nuget.org/packages/Qdrant.Client) 软件包( 软件包)[吉特胡布](https://github.com/qdrant/qdrant-dotnet)):

```bash
dotnet add package Qdrant.Client
```

## 连接设置

```csharp
using Qdrant.Client;
using Qdrant.Client.Grpc;

// gRPC client (recommended) - see https://qdrant.tech/documentation/interfaces/#grpc-interface
var client = new QdrantClient(
    host: "localhost",
    port: 6334,  // gRPC port (6333 is REST)
    https: false
);

// With API key - see https://qdrant.tech/documentation/guides/security/
var secureClient = new QdrantClient(
    host: "your-qdrant.cloud",
    port: 6334,
    https: true,
    apiKey: "your-api-key"
);
```

> **总是使用 gRPC** 生产(第6334号港) - 3-5x比废弃能源省快。

## Windows HTTP/2 修补

在 Windows 上, 启用未加密 HTTP/2 **之前** 创建客户端 :

```csharp
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
```

## 关键关键业务

### 搜索搜索

```csharp
// Vector search - see https://qdrant.tech/documentation/concepts/search/
var results = await client.SearchAsync(
    collectionName: "blog_posts",
    vector: queryEmbedding,
    limit: 10,
    filter: filter,
    scoreThreshold: 0.5f,  // Minimum similarity
    searchParams: new SearchParams
    {
        HnswEf = 128,  // Search accuracy (higher = better recall)
        Exact = false  // Use approximate search
    },
    withPayload: true
);

foreach (var result in results)
{
    Console.WriteLine($"{result.Payload["title"].StringValue}: {result.Score}");
}
```

### 批次发件夹

```csharp
// Batch operations - see https://qdrant.tech/documentation/concepts/points/#batch-update
var points = documents.Select(doc => new PointStruct
{
    Id = new PointId { Uuid = doc.Id },
    Vectors = doc.Embedding,
    Payload = { ["slug"] = doc.Slug, ["title"] = doc.Title }
}).ToList();

await client.UpsertAsync(
    collectionName: "blog_posts",
    points: points,
    wait: true  // Wait for indexing
);
```

### 删除删除删除

```csharp
// Delete by filter - see https://qdrant.tech/documentation/concepts/points/#delete-points
await client.DeleteAsync(
    collectionName: "blog_posts",
    filter: new Filter
    {
        Must = { new Condition { Field = new FieldCondition
        {
            Key = "slug",
            Match = new Match { Keyword = "old-post" }
        }}}
    }
);
```

# HNSW 指数图

[HNSW 香港新南威尔士州](https://qdrant.tech/documentation/concepts/indexing/#vector-index) (史无前例的可控小型世界) 是Qdrant的指数算法。

```mermaid
flowchart TB
    subgraph "HNSW Graph Layers"
        L2[Layer 2 - Sparse]
        L1[Layer 1 - Medium]
        L0[Layer 0 - Dense]
    end

    Q[Query] --> L2
    L2 --> L1
    L1 --> L0
    L0 --> R[Nearest Neighbors]

    style L2 stroke:#8b5cf6,stroke-width:2px
    style L1 stroke:#6366f1,stroke-width:2px
    style L0 stroke:#3b82f6,stroke-width:2px
    style Q stroke:#10b981,stroke-width:2px
    style R stroke:#ef4444,stroke-width:2px
```

## 索引参数

```csharp
// HNSW config - see https://qdrant.tech/documentation/concepts/indexing/#hnsw-index
var hnswConfig = new HnswConfigDiff
{
    M = 16,              // Edges per node (16-32 recommended)
    EfConstruct = 100,   // Build-time accuracy (100-200)
    FullScanThreshold = 10000  // Brute force threshold
};

await client.UpdateCollectionAsync(
    collectionName: "blog_posts",
    hnswConfig: hnswConfig
);
```

**搜索时间精确度 :**

```csharp
var searchParams = new SearchParams
{
    HnswEf = 128  // Higher = better recall, slower (64-256)
};
```

**计票指南 :**
使用 case  M  EfConstruct  HnswEf
|----------|---|-------------|--------|
快速,低召回 86432
-=YTET -伊甸园字幕组=- 翻译:
* 高举回想起 * * * 32 * 200 * 256 * * * 高举回想起 * * 32 * 200 * 256 *

# 有效载荷指数

创建创建 [有效有效有效有效有效指数](https://qdrant.tech/documentation/concepts/indexing/#payload-index) 用于经常过滤的字段:

```csharp
// Keyword index - see https://qdrant.tech/documentation/concepts/indexing/#payload-index
await client.CreatePayloadIndexAsync(
    collectionName: "blog_posts",
    fieldName: "language",
    schemaType: PayloadSchemaType.Keyword
);

// Integer index for ranges
await client.CreatePayloadIndexAsync(
    collectionName: "blog_posts",
    fieldName: "published",
    schemaType: PayloadSchemaType.Integer
);
```

**影响:** 10-100x 快速过滤大型收藏 。

# 量化

[量化](https://qdrant.tech/documentation/guides/quantization/) 减少内存使用量 :

```csharp
// Scalar quantization - see https://qdrant.tech/documentation/guides/quantization/#scalar-quantization
await client.UpdateCollectionAsync(
    collectionName: "blog_posts",
    quantizationConfig: new ScalarQuantization
    {
        Scalar = new ScalarQuantizationConfig
        {
            Type = ScalarType.Int8,  // float32 -> int8
            Quantile = 0.99f,
            AlwaysRam = true
        }
    }
);
```

**权衡:** 减少 4x 内存, ~ 2% 回想损失, 1. 5x 快速搜索 。

# Doccker 部署

```yaml
# docker-compose.yml - see https://qdrant.tech/documentation/guides/installation/
services:
  qdrant:
    image: qdrant/qdrant:v1.12.1  # Pin version!
    ports:
      - "6333:6333"  # REST
      - "6334:6334"  # gRPC
    volumes:
      - qdrant_data:/qdrant/storage
    environment:
      - QDRANT__SERVICE__GRPC_PORT=6334
      - QDRANT__SERVICE__HTTP_PORT=6333
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:6333/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  qdrant_data:
```

## 安全安全安全安全安全安全安全安全

启用启用 [API IPI 键密钥认证](https://qdrant.tech/documentation/guides/security/):

```yaml
environment:
  - QDRANT__SERVICE__API_KEY=your-secret-key
```

# 监测监测监测

## 计量数

Qdrant 曝光 [普罗米修斯指标](https://qdrant.tech/documentation/guides/monitoring/) 年 月 时 `/metrics`:

```bash
curl http://localhost:6333/metrics
```

关键指标:

- `qdrant_collections_vector_count` - 总矢量
- `qdrant_rest_responses_duration_seconds` - 询问时的延缓
- `qdrant_memory_usage_bytes` - 内存消耗

## 抓图

创建创建 [备份](https://qdrant.tech/documentation/concepts/snapshots/):

```bash
# Create snapshot
curl -X POST http://localhost:6333/collections/blog_posts/snapshots

# List snapshots
curl http://localhost:6333/collections/blog_posts/snapshots

# Restore (copy snapshot to storage/collections/blog_posts/snapshots/)
```

# 共犯组织

## 1. 港口混乱

- **6333** = STEST API = STEST API = STE = STE API = STE = STE API = STEST API = STE = STEST API = STEST API = STEST API = RET = STEST API = STEST API
- **6334** GRPC API(使用这个! )

## 2. 矢量尺寸差

```
Error: expected dim: 384, got 768
```

您的嵌入模型和收藏必须匹配 :

- `all-MiniLM-L6-v2`:384维
- `nomic-embed-text`:768个维度
- 开放国际 `text-embedding-3-small`: 1536 维

## 3. 缓慢的第一次查询

HNSW 将懒惰的装入记忆中。 启动后暖和起来 :

```csharp
await client.SearchAsync("blog_posts", new float[384], limit: 1);
```

## 4. 阵列过滤

使用使用 `Match.Any` 对于数组字段:

```csharp
new Match { Any = new RepeatedStrings { Strings = { "AI", "ML" } } }
```

# 资源资源资源 资源资源资源 资源资源 资源资源

## 官方 Qdrant 文档

- [概览概览概览概览概览概览概览概览](https://qdrant.tech/documentation/overview/) - 开始
- [概念概念概念](https://qdrant.tech/documentation/concepts/) - 核心概念
- [实收款](https://qdrant.tech/documentation/concepts/collections/) - 创建和管理收藏
- [点点](https://qdrant.tech/documentation/concepts/points/) - 与病媒合作
- [搜索搜索](https://qdrant.tech/documentation/concepts/search/) - 查询业务
- [过滤过滤](https://qdrant.tech/documentation/concepts/filtering/) - 过滤条件
- [编制索引索引](https://qdrant.tech/documentation/concepts/indexing/) - 新南威尔士州和有效载荷指数
- [量化](https://qdrant.tech/documentation/guides/quantization/) - 内存优化
- [安全安全安全安全安全安全安全安全](https://qdrant.tech/documentation/guides/security/) - 验证 - 验证
- [监测监测监测](https://qdrant.tech/documentation/guides/monitoring/) - 计量和遥测
- [抓图](https://qdrant.tech/documentation/concepts/snapshots/) - 备份和恢复

## 客户图书馆

- [Qdrant.net 客户端](https://github.com/qdrant/qdrant-dotnet) - 官方C#SDK
- [NuGet 软件包](https://www.nuget.org/packages/Qdrant.Client) - 最近释放

## 相关条款

- [第4部分:ONNX和Qdrant 执行](/blog/semantic-search-with-onnx-and-qdrant)
- [第5部分:混合搜索和自动插入](/blog/rag-hybrid-search-and-indexing)
- [RAG系列概览](/blog/rag-primer)

## 源代码

所有代码可用于: [com/scottgal/ mostlylylucidweb 缩略图/ com/ scottgal/ mostlylylucidweb 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图/ 缩略图](https://github.com/scottgal/mostlylucidweb)

- `Mostlylucid.SemanticSearch/Services/QdrantVectorStoreService.cs` - Qdrant 集成