在 AlloyDB Omni 中执行向量搜索

选择文档版本:

本教程介绍如何在 AlloyDB Omni 中设置并执行向量搜索。您将使用基于精确最近邻搜索 (KNN) 和基于近似最近邻 (ANN) 的 ScaNN 索引运行查询。您还将了解如何使用 ScaNN 索引高效运行过滤向量搜索查询。

目标

  • 在 AlloyDB Omni 中安装 AlloyDB AI。
  • 连接到您的数据库并安装所需的扩展程序。
  • 创建 productproduct inventory 表。
  • 将数据插入 productproduct inventory 表,并执行基本向量搜索。
  • 在商品表上创建 ScaNN 索引。
  • 执行向量搜索。
  • 执行包含过滤条件和联接的复杂向量搜索。

费用

在本文档中,您将使用 Google Cloud的以下收费组件:

您可使用价格计算器根据您的预计使用量来估算费用。

完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理

前提条件

在执行向量搜索之前,请完成以下前提条件。

根据您的计算环境在 AlloyDB Omni 中安装 AlloyDB AI

根据您使用的计算环境,按照在 AlloyDB Omni 中安装 AlloyDB AI 中的说明安装 AlloyDB Omni。

使用 psql 连接到数据库

选择您使用的计算环境,以便使用 psql 连接到数据库:

Docker

  docker exec -it my-omni-1 psql -U postgres

Podman

  podman exec -it my-omni-1 psql -U postgres

安装必需的扩展程序

运行以下查询以安装 vectoralloydb_scanngoogle_ml_integration 扩展程序:

  CREATE EXTENSION IF NOT EXISTS vector;
  CREATE EXTENSION IF NOT EXISTS alloydb_scann;
  CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;

完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅“清理”。

插入商品和商品目录数据,并执行基本向量搜索

在本教程中,我们将使用 product_inventoryproduct 表运行复杂的向量搜索查询。

  1. 运行以下语句,以创建执行以下操作的 product 表:

    • 存储基本商品信息。
    • 包含 embedding 向量列,用于计算和存储每个商品的商品说明的嵌入向量。
      CREATE TABLE product (
        id INT PRIMARY KEY,
        name VARCHAR(255) NOT NULL,
        description TEXT,
        category VARCHAR(255),
        color VARCHAR(255),
        embedding vector(768) GENERATED ALWAYS AS (embedding('text-embedding-004', description)) STORED
      );
    
  2. 运行以下查询,以创建一个 product_inventory 表,用于存储有关可用商品目录和相应价格的信息。

    CREATE TABLE product_inventory (
      id INT PRIMARY KEY,
      product_id INT REFERENCES product(id),
      inventory INT,
      price DECIMAL(10,2)
    );
    
  3. 运行以下查询,以将商品数据插入 product 表中:

    INSERT INTO product (id, name, description,category, color) VALUES
    (1, 'Stuffed Elephant', 'Soft plush elephant with floppy ears.', 'Plush Toys', 'Gray'),
    (2, 'Remote Control Airplane', 'Easy-to-fly remote control airplane.', 'Vehicles', 'Red'),
    (3, 'Wooden Train Set', 'Classic wooden train set with tracks and trains.', 'Vehicles', 'Multicolor'),
    (4, 'Kids Tool Set', 'Toy tool set with realistic tools.', 'Pretend Play', 'Multicolor'),
    (5, 'Play Food Set', 'Set of realistic play food items.', 'Pretend Play', 'Multicolor'),
    (6, 'Magnetic Tiles', 'Set of colorful magnetic tiles for building.', 'Construction Toys', 'Multicolor'),
    (7, 'Kids Microscope', 'Microscope for kids with different magnification levels.', 'Educational Toys', 'White'),
    (8, 'Telescope for Kids', 'Telescope designed for kids to explore the night sky.', 'Educational Toys', 'Blue'),
    (9, 'Coding Robot', 'Robot that teaches kids basic coding concepts.', 'Educational Toys', 'White'),
    (10, 'Kids Camera', 'Durable camera for kids to take pictures and videos.', 'Electronics', 'Pink'),
    (11, 'Walkie Talkies', 'Set of walkie talkies for kids to communicate.', 'Electronics', 'Blue'),
    (12, 'Karaoke Machine', 'Karaoke machine with built-in microphone and speaker.', 'Electronics', 'Black'),
    (13, 'Kids Drum Set', 'Drum set designed for kids with adjustable height.', 'Musical Instruments', 'Blue'),
    (14, 'Kids Guitar', 'Acoustic guitar for kids with nylon strings.', 'Musical Instruments', 'Brown'),
    (15, 'Kids Keyboard', 'Electronic keyboard with different instrument sounds.', 'Musical Instruments', 'Black'),
    (16, 'Art Easel', 'Double-sided art easel with chalkboard and whiteboard.', 'Arts & Crafts', 'White'),
    (17, 'Finger Paints', 'Set of non-toxic finger paints for kids.', 'Arts & Crafts', 'Multicolor'),
    (18, 'Modeling Clay', 'Set of colorful modeling clay.', 'Arts & Crafts', 'Multicolor'),
    (19, 'Watercolor Paint Set', 'Watercolor paint set with brushes and palette.', 'Arts & Crafts', 'Multicolor'),
    (20, 'Beading Kit', 'Kit for making bracelets and necklaces with beads.', 'Arts & Crafts', 'Multicolor'),
    (21, '3D Puzzle', '3D puzzle of a famous landmark.', 'Puzzles', 'Multicolor'),
    (22, 'Race Car Track Set', 'Race car track set with cars and accessories.', 'Vehicles', 'Multicolor'),
    (23, 'RC Monster Truck', 'Remote control monster truck with oversized tires.', 'Vehicles', 'Green'),
    (24, 'Train Track Expansion Set', 'Expansion set for wooden train tracks.', 'Vehicles', 'Multicolor');
    
  4. 可选:如需验证数据是否已插入到 product 表中,请运行以下查询:

    SELECT * FROM product;
    
  5. 运行以下查询,以将商品目录数据插入 product_inventory 表中:

    INSERT INTO product_inventory (id, product_id, inventory, price) VALUES
    (1, 1, 9, 13.09),
    (2, 2, 40, 79.82),
    (3, 3, 34, 52.49),
    (4, 4, 9, 12.03),
    (5, 5, 36, 71.29),
    (6, 6, 10, 51.49),
    (7, 7, 7, 37.35),
    (8, 8, 6, 10.87),
    (9, 9, 7, 42.47),
    (10, 10, 3, 24.35),
    (11, 11, 4, 10.20),
    (12, 12, 47, 74.57),
    (13, 13, 5, 28.54),
    (14, 14, 11, 25.58),
    (15, 15, 21, 69.84),
    (16, 16, 6, 47.73),
    (17, 17, 26, 81.00),
    (18, 18, 11, 91.60),
    (19, 19, 8, 78.53),
    (20, 20, 43, 84.33),
    (21, 21, 46, 90.01),
    (22, 22, 6, 49.82),
    (23, 23, 37, 50.20),
    (24, 24, 27, 99.27);
    
  6. 运行以下向量搜索查询,以搜索与字词 music 类似的商品。即使商品说明中未明确提及 music 字词,结果也会显示与查询相关的商品:

    SELECT * FROM product
    ORDER BY embedding <=> embedding('text-embedding-005', 'music')::vector
    LIMIT 3;
    

    如果您在未创建索引的情况下执行基本向量搜索,AlloyDB AI 会使用 KNN,这可提供高效的召回率;不过,大规模使用 KNN 可能会影响性能。为了获得更好的查询性能,我们建议您使用 ScaNN 索引进行 ANN 搜索,这可在低延迟的情况下提供高召回率。

    如果您未创建索引,AlloyDB Omni 默认会使用 KNN。

在商品表上创建 ScaNN 索引

运行以下查询,以在 product 表上创建 product_index ScaNN 索引:

  CREATE INDEX product_index ON product
  USING scann (embedding cosine)
  WITH (num_leaves=5);

num_leaves 参数表示基于树的索引构建索引时使用的叶节点数量。如需详细了解如何对此参数进行调优,请参阅对向量查询性能进行调优

运行以下向量搜索查询,尝试查找与自然语言查询 music 类似的商品。即使商品说明中未包含 music 字词,结果也会显示与查询相关的商品:

SET LOCAL scann.num_leaves_to_search = 2;

SELECT * FROM product
ORDER BY embedding <=> embedding('text-embedding-005', 'music')::vector
  LIMIT 3;

scann.num_leaves_to_search 查询参数用于控制在相似度搜索期间搜索的叶节点数量。num_leavesscann.num_leaves_to_search 参数值有助于平衡查询性能和召回率。

运行以下复杂的向量搜索查询,该查询会返回满足查询条件的相关结果,即使具有过滤条件也是如此:

SET LOCAL scann.num_leaves_to_search = 2;

SELECT * FROM product p
JOIN product_inventory pi ON p.id = pi.product_id
WHERE pi.price < 80.00
ORDER BY embedding <=> embedding('text-embedding-005', 'music')::vector
LIMIT 3;

清理

Docker

  docker container stop my-omni-1
  docker container rm my-omni-1

Podman

  podman container stop my-omni-1
  podman container rm my-omni-1

后续步骤