Qdrant 矢量相似性搜索引擎的详情使用说明

19 min read

Qdrant 是一款开源的矢量相似性搜索引擎,它可以用于快速、高效地查询相似的向量。下面是 Qdrant 的详细使用说明:

  1. 准备数据

在开始使用 Qdrant 之前,需要准备好向量数据。每个向量都应该是一个固定长度的数组,元素的值可以是整数、浮点数或二进制数据。可以把每个向量看作是一个点,在高维空间中定义的。

  1. 安装 Qdrant

Qdrant 可以在 Linux、MacOS 和 Windows 上运行。可以从 Qdrant 的 GitHub 页面下载二进制文件,也可以使用 Docker 镜像进行安装。

在 Linux 上安装 Qdrant 的方式如下:

下载二进制文件:

$ curl -L https://github.com/qdrant/qdrant/releases/download/v0.4.0/qdrant-linux-amd64-0.4.0.tar.gz | tar xvz

启动 Qdrant:

$ ./qdrant start
  1. 创建索引

创建索引之前,需要先定义向量的属性。例如,我们可以定义每个向量的颜色、大小和形状:

{
    "name": "vector",
    "type": "vector",
    "params": {
        "embedding_dim": 3
    }
},
{
    "name": "color",
    "type": "string"
},
{
    "name": "size",
    "type": "integer"
},
{
    "name": "shape",
    "type": "string"
}

创建索引的方式如下:

$ curl -X POST -H 'Content-Type: application/json' -d '{
    "space": "my_space",
    "collection": "my_collection",
    "index": {
        "backend": "hnsw",
        "metric": "euclidean",
        "params": {
            "efConstruction": 200,
            "M": 64,
            "efSearch": 200
        }
    },
    "attributes": [
        {
            "name": "vector",
            "type": "vector",
            "params": {
                "embedding_dim": 3
            }
        },
        {
            "name": "color",
            "type": "string"
        },
        {
            "name": "size",
            "type": "integer"
        },
        {
            "name": "shape",
            "type": "string"
        }
    ]
}' http://localhost:6333/collections
  1. 导入数据

将数据导入到索引中的方式如下:

$ curl -X POST -H 'Content-Type: application/json' -d '[{
    "id": 1,
    "vector": [1, 2, 3],
    "color": "red",
    "size": 10,
    "shape": "square"
}, {
    "id": 2,
    "vector": [4, 5, 6],
    "color": "blue",
    "size": 20,
    "shape": "circle"
}, {
    "id": 3,
    "vector": [7, 8, 9],
    "color": "green",
    "size": 30,
    "shape": "triangle"
}]' http://localhost:6333/collections/my_collection/entities
  1. 搜索相似向量

搜索与给定向量最相似的向量的方式如下:

$ curl -X POST -H 'Content-Type: application/json' -d '{
    "vector": [1, 2, 3]
}' http://localhost:6333/collections/my_collection/search

返回结果:

{
    "took": 0.0003,
    "result": [
        {
            "id": 1,
            "score": 0
        },
        {
            "id": 2,
            "score": 9
        },
        {
            "id": 3,
            "score": 36
        }
    ]
}

其中,id 表示向量在索引中的唯一标识,score 表示与给定向量的距离,越小表示越相似。

  1. 总结

Qdrant 是一款强大的矢量相似性搜索引擎,它可以用于快速、高效地查询相似的向量。在使用 Qdrant 之前,需要先准备好向量数据,并进行索引创建和数据导入。使用 Qdrant 查询相似向量的方式非常简单,只需要提供一个给定向量即可。