Milvus

以下是所有可用的 Milvus 过程列表

名称 描述

apoc.vectordb.milvus.info(hostOrKey, collection, $config)

获取指定现有集合的信息,如果不存在,则返回代码 100 的响应

apoc.vectordb.milvus.createCollection(hostOrKey, collection, similarity, size, $config)

创建一个集合,名称在第 2 个参数中指定,并具有指定的 similaritysize。默认端点为 <hostOrKey 参数>/v2/vectordb/collections/create

apoc.vectordb.milvus.deleteCollection(hostOrKey, collection, $config)

删除名称在第 2 个参数中指定的集合。默认端点为 <hostOrKey 参数>/v2/vectordb/collections/drop

apoc.vectordb.milvus.upsert(hostOrKey, collection, vectors, $config)

在名称在第 2 个参数中指定的集合中,执行 Upsert 操作,向量为 [{id: 'id', vector: '<vectorDb>', medatada: '<metadata>'}]. 默认端点为 <hostOrKey 参数>/v2/vectordb/entities/upsert

apoc.vectordb.milvus.delete(hostOrKey, collection, ids, $config)

删除具有指定 ids 的向量。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/delete

apoc.vectordb.milvus.get(hostOrKey, collection, ids, $config)

获取具有指定 ids 的向量。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/get

apoc.vectordb.milvus.query(hostOrKey, collection, vector, filter, limit, $config)

在名称在第 2 个参数中指定的集合中,检索与定义的 vector 最接近的向量,结果数量限制为 limit。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/search

apoc.vectordb.milvus.getAndUpdate(hostOrKey, collection, ids, $config)

获取具有指定 ids 的向量。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/get,并可选择创建/更新 neo4j 实体。

apoc.vectordb.milvus.queryAndUpdate(hostOrKey, collection, vector, filter, limit, $config)

在名称在第 2 个参数中指定的集合中,检索与定义的 vector 最接近的向量,结果数量限制为 limit,并可选择创建/更新 neo4j 实体。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/search

其中第 1 个参数可以是 apoc 配置 apoc.milvus.<key>.host=myHost 定义的键。当 hostOrKey=null 时,默认主机是 'https://:19530'。

示例

以下是使用默认端口 19531 的本地安装示例列表。

获取集合信息(它利用 此 API
CALL apoc.vectordb.milvus.info($host, 'test_collection', '', {<optional config>})
表 1. 示例结果

{"data": {"shardsNum": 1, "aliases": [], "autoId": false, "description": "", "partitionsNum": 1, "collectionName": "test_collection", "indexes": [{"metricType": "COSINE", "indexName": "vector", "fieldName": "vector"}], "load": "LoadStateLoading", "consistencyLevel": "Bounded", "fields": [{"partitionKey": false, "autoId": false, "name": "id", "description": "", "id": 100, "type": "Int64", "primaryKey": true}, {"partitionKey": false, "autoId": false, "name": "vector", "description": "", "id": 101, "params": [{"value": 4, "key": "dim"}], "type": "FloatVector", "primaryKey": false} ], "collectionID": "451046728334049293", "enableDynamicField": true, "properties": []}, "message": "", "code": 200 }

创建集合(它利用 此 API
CALL apoc.vectordb.milvus.createCollection('https://:19531', 'test_collection', 'COSINE', 4, {<optional config>})
表 2. 示例结果
数据 代码

null

200

删除集合(它利用 此 API
CALL apoc.vectordb.milvus.deleteCollection('https://:19531', 'test_collection', {<optional config>})
表 3. 示例结果
数据 代码

null

200

Upsert 向量(它利用 此 API
CALL apoc.vectordb.milvus.upsert('https://:19531', 'test_collection',
    [
        {id: 1, vector: [0.05, 0.61, 0.76, 0.74], metadata: {city: "Berlin", foo: "one"}},
        {id: 2, vector: [0.19, 0.81, 0.75, 0.11], metadata: {city: "London", foo: "two"}}
    ],
    {<optional config>})
表 4. 示例结果
数据 代码

{"upsertCount": 2, "upsertId": [1, 2]}

200

获取向量(它利用 此 API
CALL apoc.vectordb.milvus.get('https://:19531', 'test_collection', [1,2], {<optional config>})
表 5. 示例结果
分数 元数据 id 向量 文本 实体

null

{city: "Berlin", foo: "one"}

null

null

null

null

null

{city: "Berlin", foo: "two"}

null

null

null

null

获取带有 {allResults: true} 的向量
CALL apoc.vectordb.milvus.get('https://:19531', 'test_collection', [1,2], {allResults: true, <optional config>})
表 6. 示例结果
分数 元数据 id 向量 文本 实体

null

{city: "Berlin", foo: "one"}

1

[…​]

null

null

null

{city: "Berlin", foo: "two"}

2

[…​]

null

null

查询向量(它利用 此 API
CALL apoc.vectordb.milvus.query('https://:19531',
    'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    { must:
        [ { key: "city", match: { value: "London" } } ]
    },
    5,
    {allResults: true, <optional config>})
表 7. 示例结果
分数 元数据 id 向量 文本 实体

1,

{city: "Berlin", foo: "one"}

1

[…​]

null

null

0.1

{city: "Berlin", foo: "two"}

2

[…​]

null

null

我们可以通过利用向量元数据定义映射,自动创建单个/多个节点和关系。

例如,如果使用上述 upsert 过程创建了 2 个向量,我们可以填充一些现有节点(即 (:Test {myId: 'one'})(:Test {myId: 'two'})

CALL apoc.vectordb.milvus.queryAndUpdate('https://:19531', 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            embeddingKey: "vect",
            nodeLabel: "Test",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

这将填充这两个节点为:(:Test {myId: 'one', city: 'Berlin', vect: [vector1]})(:Test {myId: 'two', city: 'London', vect: [vector2]}),它们将在 entity 结果列中返回。

我们还可以将映射配置的 mode 设置为 CREATE_IF_MISSING(如果节点不存在则创建)、READ_ONLY(用于搜索节点/关系,不进行更新)或 UPDATE_EXISTING(默认行为)

CALL apoc.vectordb.milvus.queryAndUpdate('https://:19531', 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            mode: "CREATE_IF_MISSING",
            embeddingKey: "vect",
            nodeLabel: "Test",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

这将创建并填充如上所示的 2 个新节点。

或者,我们可以填充现有关系(即 (:Start)-[:TEST {myId: 'one'}]→(:End)(:Start)-[:TEST {myId: 'two'}]→(:End)

CALL apoc.vectordb.milvus.queryAndUpdate('https://:19531', 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            embeddingKey: "vect",
            relType: "TEST",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

这将填充这两个关系为:()-[:TEST {myId: 'one', city: 'Berlin', vect: [vector1]}]-()()-[:TEST {myId: 'two', city: 'London', vect: [vector2]}]-(),它们将在 entity 结果列中返回。

我们还可以将映射用于 apoc.vectordb.milvus.query 过程,用于搜索符合标签/类型和元数据键的节点/关系,而不进行更新(即,等同于具有映射配置 mode: "READ_ONLY"*.queryOrUpdate 过程)。

例如,对于先前的关系,我们可以执行以下过程,它只在 rel 列中返回这些关系

CALL apoc.vectordb.milvus.query('https://:19531', 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            embeddingKey: "vect",
            relType: "TEST",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

我们也可以将映射用于 apoc.vectordb.milvus.get* 过程

为了优化性能,我们可以选择使用 apoc.vectordb.milvus.query*apoc.vectordb.milvus.get* 过程 YIELD 什么。

例如,通过执行 CALL apoc.vectordb.milvus.query(…​) YIELD metadata, score, id,RestAPI 请求将包含 {"with_payload": false, "with_vectors": false},这样我们就不会返回不需要的其他值。

可以将向量数据库过程与 apoc.ml.rag 一起执行,如下所示

CALL apoc.vectordb.milvus.getAndUpdate($host, $collection, [<id1>, <id2>], $conf) YIELD node, metadata, id, vector
WITH collect(node) as paths
CALL apoc.ml.rag(paths, $attributes, $question, $confPrompt) YIELD value
RETURN value

它返回一个字符串,通过利用数据库向量的嵌入来回答 $question

删除向量(它利用 此 API
CALL apoc.vectordb.milvus.delete('https://:19531', 'test_collection', [1,2], {<optional config>})
表 8. 示例结果
数据 代码

null

200

© . All rights reserved.