floatvector 类型

VexDB 支持 floatvector 类型,用于存储浮点类型的向量数据。每个向量是一个固定长度的数组,可以存储机器学习模型的嵌入向量、图像特征、文本的词嵌入等。

CREATE TABLE items (
id SERIAL PRIMARY KEY,
embedding floatvector(128) -- 存储一个 128 维的向量
);
  • 一个表可以包含1个或者多个floatvector字段。
  • 创建 floatvector 类型时,需要指定向量的维度,例如 floatvector(128) 表示每个向量有 128 个元素。目前支持的向量维度取值范围为1,16384

类型转换

  • array -> floatvector
    SELECT ARRAY[1,1,1]::floatvector;
    
  • string ->floatvector
    SELECT '[1,1,1]'::floatvector;
    
  • text /char(n) / nchar(n)/ varchar(n) / nvarchar(n) / clob / bpchar -> floatvector
    SELECT '[1.0]'::text::floatvector AS vector_example;
    SELECT '[1.0]'::char(5)::floatvector AS vector_example;
    SELECT '[1.0]'::nchar(5)::floatvector AS vector_example;
    SELECT '[1.0]'::varchar(5)::floatvector AS vector_example;
    SELECT '[1.0]'::nvarchar(5)::floatvector AS vector_example;
    SELECT '[1.0]'::clob::floatvector AS vector_example;
    SELECT '[1.0]'::bpchar(5)::floatvector AS vector_example;
    

示例

  1. 创建一个含向量列的新表。
    CREATE TABLE items (id bigserial PRIMARY KEY, embedding floatvector(3));
    
  2. 插入向量数据。
    INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
    
  3. UPSERT 向量数据。
    INSERT INTO items (id, embedding)
    VALUES
    (1, '[1, 2, 4]'),
    (2, '[4, 5, 7]')
    ON CONFLICT (id)
    DO UPDATE SET embedding = EXCLUDED.embedding;
    
  4. 更新向量数据。
    UPDATE items SET embedding = '[1, 2, 8]' WHERE id =1;
    
  5. 删除向量数据。
    DELETE FROM items WHERE id =1;
    
  6. 向己存在的表中添加一个向量列。
    ALTER TABLE items ADD COLUMN embedding1 floatvector (3);
    

需要帮助?

扫码添加企业微信
获得专业技术支持

企业微信二维码
🎯 快速响应💡 专业解答