Cassandra 文档

版本

您正在查看预发布版本的文档。

向量 CQL 数据类型

向量搜索需要一个新的 CQL 数据类型 VECTORissues.apache.org/jira/browse/CASSANDRA-18504 将此数据类型添加到 Cassandra 5.0。 新类型 VECTOR 具有以下属性

  • 固定长度数组

  • 元素不能为 null

  • 扁平化数组(又名多单元格 = false)

虽然用于向量搜索的向量指定的数据类型是浮点数,但向量数据类型可用于创建任何数据类型的向量。 例如,可以创建 intbigint 的向量,但不能与向量搜索一起使用。

向量限制为最大 8K(2^13)个项目的维度。

创建键空间

CREATE KEYSPACE IF NOT EXISTS cycling
   WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };

使用键空间

USE cycling;

此数据类型可以在 CREATE TABLE 语句中使用

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);
1 创建 5 维嵌入

索引向量列

CREATE INDEX IF NOT EXISTS ann_index
  ON cycling.comments_vs(comment_vector) USING '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]
);

选择数据

SELECT * FROM cycling.comments_vs
    ORDER BY comment_vector ANN OF [0.15, 0.1, 0.1, 0.35, 0.55]
    LIMIT 3;