Skip to main content

Views

A view, in this context, is a virtual table that is based on result-set of an SQL query. Using views, one can create a similarity search to go over the vector embeddings and get the most relevant results.

For example, we embedded our data using vector embeddings but now we want to do a semantic search on it. We will use the VIEW.

CREATE VIEW similar(query String "Query to search similar sections in pdf documents") AS
WITH query AS (
SELECT embedding::Array(Float32) AS query FROM generate_embeddings($query)
)
SELECT
p.id,
p.content,
cosineDistance(p.embeddings, query) AS similarity
FROM
pdf_embeddings p
CROSS JOIN
query
ORDER BY
similarity ASC
LIMIT 5

This VIEW generates embeddings for the query argument and compares them with those in the pdf_embeddings table (using their cosineDistances) to find the five most similar text segments.