目标
- 在 AlloyDB Omni 中安装 AlloyDB AI。
- 连接到您的数据库并安装所需的扩展程序。
- 创建
product和product inventory表。 - 将数据插入
product和product inventory表,并执行基本向量搜索。 - 在商品表上创建 ScaNN 索引。
- 执行向量搜索。
- 执行包含过滤条件和联接的复杂向量搜索。
费用
在本文档中,您将使用 Google Cloud的以下收费组件:
您可使用价格计算器根据您的预计使用量来估算费用。
完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理。
前提条件
在执行向量搜索之前,请完成以下前提条件。
根据您的计算环境在 AlloyDB Omni 中安装 AlloyDB AI
根据您使用的计算环境,按照在 AlloyDB Omni 中安装 AlloyDB AI 中的说明安装 AlloyDB Omni。
使用 psql 连接到数据库
使用 psql 连接到数据库:
export DBPOD=`kubectl get pod --selector=alloydbomni.internal.dbadmin.goog/dbcluster=my-db-cluster,alloydbomni.internal.dbadmin.goog/task-type=database -o jsonpath='{.items[0].metadata.name}'`kubectl exec -ti $DBPOD -c database -- psql -h localhost -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 表运行复杂的向量搜索查询。
运行以下语句,以创建执行以下操作的
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 );运行以下查询,以创建一个
product_inventory表,用于存储有关可用商品目录和相应价格的信息。CREATE TABLE product_inventory ( id INT PRIMARY KEY, product_id INT REFERENCES product(id), inventory INT, price DECIMAL(10,2) );运行以下查询,以将商品数据插入
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');可选:如需验证数据是否已插入到
product表中,请运行以下查询:SELECT * FROM product;运行以下查询,以将商品目录数据插入
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);运行以下向量搜索查询,以搜索与字词
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_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;
清理
删除数据库集群
kubectl patch dbclusters.alloydbomni.dbadmin.goog my-db-cluster -p '{"spec":{"isDeleted":true}}' --type=merge卸载 AlloyDB Omni operator
如需从 Kubernetes 集群中卸载 AlloyDB Omni Kubernetes 操作器,请完成以下步骤:
删除所有数据库集群:
for ns in $(kubectl get dbclusters.alloydbomni.dbadmin.goog --all-namespaces -o=jsonpath='{range .items[*]}{.metadata.namespace}{"\n"}{end}'); do for cr in $(kubectl get dbclusters.alloydbomni.dbadmin.goog -n $ns -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'); do kubectl patch dbclusters.alloydbomni.dbadmin.goog $cr -n $ns --type=merge -p '{"spec":{"isDeleted":true}}' done done ```运行以下命令,验证 AlloyDB Omni Kubernetes 操作器是否已删除您的所有数据库集群:
kubectl get dbclusters.alloydbomni.dbadmin.goog --all-namespaces删除 AlloyDB Omni Kubernetes operator 创建的其他资源:
kubectl delete failovers.alloydbomni.dbadmin.goog --all --all-namespaceskubectl delete restores.alloydbomni.dbadmin.goog --all --all-namespaceskubectl delete switchovers.alloydbomni.dbadmin.goog --all --all-namespaces卸载 AlloyDB Omni Kubernetes operator:
helm uninstall alloydbomni-operator --namespace alloydb-omni-system清理与 AlloyDB Omni Kubernetes operator 相关的 Secret、自定义资源说明和命名空间:
kubectl delete certificate -n alloydb-omni-system --allkubectl get secrets --all-namespaces -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,ANNOTATION:.metadata.annotations.cert-manager\.io/issuer-name | grep -E 'alloydbomni|dbs-al' | awk '{print $1 " " $2}' | xargs -n 2 kubectl delete secret -nkubectl delete crd -l alloydb-omni=truekubectl delete ns alloydb-omni-system