This SQL script demonstrates how to perform a vector search using vector functions within a SQL database. The goal is to find the top 10 most relevant products from the walmart_product_details table that match a given search query based on semantic similarity.
-
Text to Vector Conversion:
- The search text, in this case, 'help me plan a high school graduation party', is declared as an
nvarchar(max)variable named@search_text. - A
vector(1536)variable named@search_vectoris declared to store the vector representation of the search text.
- The search text, in this case, 'help me plan a high school graduation party', is declared as an
-
Creating Embeddings:
- The
dbo.create_embeddingsstored procedure is executed with@search_textas input and@search_vectoras output. - This procedure converts the search text into a vector embedding, which captures the semantic meaning of the text.
- The
-
Vector Search Query:
- The
SELECTstatement retrieves the top 10 records from thewalmart_product_detailstable. - The
vector_distancefunction computes the 'cosine' distance between the@search_vectorand theproduct_description_vectorcolumn in the table. - The results are ordered by the
distance, with the closest vectors (most semantically similar) appearing first.
- The
-- Declare the search text
declare @search_text nvarchar(max) = 'help me plan a high school graduation party';
-- Declare a variable to hold the search vector
declare @search_vector vector(1536);
-- Generate the search vector using the 'create_embeddings' stored procedure
exec dbo.create_embeddings @search_text, @search_vector output;
-- Perform the search query
SELECT TOP(10)
id, product_name, description,
-- Calculate the cosine distance between the search vector and product description vectors
vector_distance('cosine', @search_vector, product_description_vector) AS distance
FROM [dbo].[walmart_product_details]
ORDER BY distance; -- Order by the closest distanceAs there are duplicate products, rewriting query to remove duplicates
declare @search_text nvarchar(max) = 'help me plan a high school graduation party';
-- Declare a variable to hold the search vector
declare @search_vector vector(1536);
-- Generate the search vector using the 'create_embeddings' stored procedure
exec dbo.create_embeddings @search_text, @search_vector output;
-- Perform the search query
SELECT TOP(10)
id,
product_name,
description,
-- Calculate the cosine distance between the search vector and product description vectors
vector_distance('cosine', @search_vector, product_description_vector) AS distance
FROM (
SELECT
id,
product_name,
description,
product_description_vector,
ROW_NUMBER() OVER (PARTITION BY product_name, description ORDER BY (SELECT NULL)) AS rn
FROM [dbo].[walmart_product_details]
) AS unique_products
WHERE rn = 1
ORDER BY distance;