在 AlloyDB Omni 中執行向量搜尋

本教學課程說明如何在 AlloyDB Omni 設定及執行向量搜尋。您可以使用精確 K 近鄰搜尋 (KNN) 和近似近鄰 (ANN) 的 ScaNN 索引執行查詢。您也會瞭解如何使用 ScaNN 索引,有效率地執行篩選過的向量搜尋查詢。

目標

  • 在 AlloyDB Omni 中安裝 AlloyDB AI。
  • 連線至資料庫,並安裝必要擴充功能。
  • 建立 product 和 product inventory 資料表。
  • 將資料插入 product 和 product inventory 資料表,並執行基本向量搜尋。
  • 在 products 資料表建立 ScaNN 索引。
  • 執行向量搜尋。
  • 使用篩選器和 join 執行複雜的向量搜尋。

費用

在本文件中,您將使用下列 Google Cloud計費元件:

您可以使用 Pricing Calculator,根據預測用量估算費用。

完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」一節。

必要條件

執行向量搜尋前,請先完成下列必要條件。

根據運算環境,在 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

安裝必要的擴充功能

執行下列查詢,安裝 vector、alloydb_scann 和 google_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_inventory 和 product 資料表,執行複雜的向量搜尋查詢。

  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。

在 products 資料表建立 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_leaves 和 scann.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

後續步驟