使用向量搜索
创建向量键空间
创建要用于向量搜索表的键空间。此示例使用 cycling
作为 键空间名称
CREATE KEYSPACE IF NOT EXISTS cycling
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };
创建向量表
在您的键空间中创建一个新表,包括用于向量的 comments_vector
列。以下代码创建了一个包含五个值的向量
CREATE TABLE IF NOT EXISTS cycling.comments_vs (
record_id timeuuid,
id uuid,
commenter text,
comment text,
comment_vector VECTOR <FLOAT, 5>,
created_at timestamp,
PRIMARY KEY (id, created_at)
)
WITH CLUSTERING ORDER BY (created_at DESC);
可以选择更改现有表以添加向量列
ALTER TABLE cycling.comments_vs
ADD comment_vector VECTOR <FLOAT, 5>(1)
创建向量索引
使用存储附加索引 (SAI) 创建自定义索引
CREATE INDEX IF NOT EXISTS ann_index
ON cycling.comments_vs(comment_vector) USING 'sai';
有关 SAI 的更多信息,请参阅 存储附加索引 文档。
可以使用定义相似度函数的选项创建索引
|
将向量数据加载到您的数据库中
使用新类型将数据插入表中
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
'2017-02-14 12:43:20-0800',
'Raining too hard should have postponed',
'Alex',
[0.45, 0.09, 0.01, 0.2, 0.11]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
'2017-03-21 13:11:09.999-0800',
'Second rest stop was out of water',
'Alex',
[0.99, 0.5, 0.99, 0.1, 0.34]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
'2017-04-01 06:33:02.16-0800',
'LATE RIDERS SHOULD NOT DELAY THE START',
'Alex',
[0.9, 0.54, 0.12, 0.1, 0.95]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
c7fceba0-c141-4207-9494-a29f9809de6f,
totimestamp(now()),
'The gift certificate for winning was the best',
'Amy',
[0.13, 0.8, 0.35, 0.17, 0.03]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
c7fceba0-c141-4207-9494-a29f9809de6f,
'2017-02-17 12:43:20.234+0400',
'Glad you ran the race in the rain',
'Amy',
[0.3, 0.34, 0.2, 0.78, 0.25]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
c7fceba0-c141-4207-9494-a29f9809de6f,
'2017-03-22 5:16:59.001+0400',
'Great snacks at all reststops',
'Amy',
[0.1, 0.4, 0.1, 0.52, 0.09]
);
INSERT INTO cycling.comments_vs (record_id, id, created_at, comment, commenter, comment_vector)
VALUES (
now(),
c7fceba0-c141-4207-9494-a29f9809de6f,
'2017-04-01 17:43:08.030+0400',
'Last climb was a killer',
'Amy',
[0.3, 0.75, 0.2, 0.2, 0.5]
);
使用 CQL 查询向量数据
要使用向量搜索查询数据,请使用 SELECT
查询
SELECT * FROM cycling.comments_vs
ORDER BY comment_vector ANN OF [0.15, 0.1, 0.1, 0.35, 0.55]
LIMIT 3;
要获取作为结果一部分的与查询数据最接近的最佳评分节点的相似度计算,请使用 SELECT
查询
SELECT comment, similarity_cosine(comment_vector, [0.2, 0.15, 0.3, 0.2, 0.05])
FROM cycling.comments_vs
ORDER BY comment_vector ANN OF [0.1, 0.15, 0.3, 0.12, 0.05]
LIMIT 1;
此类查询支持的函数为
-
similarity_dot_product
-
similarity_cosine
-
similarity_euclidean
参数为 (<vector_column>,<embedding_value>)。两个参数都代表向量。
|