0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · บทเรียน

การดำเนินการค้นหาความคล้ายคลึง

ดำเนินการค้นหาความคล้ายคลึงของเวกเตอร์เบื้องต้นด้วยตัวดำเนินการ pgvector เพื่อค้นหาเพื่อนบ้านที่ใกล้ที่สุดในข้อมูลของคุณ

การดำเนินการค้นหาความคล้ายคลึง เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vector Databases: Pinecone, Weaviate & pgvector และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Unlocking Similarity Queries

Welcome! In this lesson, we'll dive into one of the most powerful features of vector databases: similarity queries.

You'll learn how to ask your database to find items that are 'similar' to a given item, based on their vector embeddings.

Why Similarity Matters

Similarity queries are at the heart of many AI applications:

  • Recommendation Systems: Find products similar to what a user liked.
  • Semantic Search: Retrieve documents with similar meaning, not just keyword matches.
  • Anomaly Detection: Identify data points that are unusually 'far' from others.

They help us make sense of high-dimensional data.

Measuring Vector Distance

How do we define 'similarity' for vectors? We use distance metrics.

Imagine vectors as points in space. The 'closer' two points are, the more similar their underlying data is. Different metrics measure this distance in different ways.

Euclidean Distance (L2 Norm)

Euclidean distance, also known as L2 distance, is the most intuitive metric. It's the straight-line distance between two points in a Euclidean space.

In pgvector, you use the <-> operator to calculate Euclidean distance. A smaller value means higher similarity.

Cosine Similarity / Distance

Cosine similarity measures the cosine of the angle between two vectors. It tells you if vectors are pointing in roughly the same direction, regardless of their magnitude (length).

pgvector uses the <#> operator for cosine distance. Cosine distance is 1 - cosine_similarity. A smaller cosine distance (closer to 0) means the vectors are more aligned and similar.

Preparing Our Data for Queries

To demonstrate queries, let's set up a simple table with some 3-dimensional vectors. This ensures our code snippets are runnable.

CREATE EXTENSION IF NOT EXISTS vector;

DROP TABLE IF EXISTS items;
CREATE TABLE items (
  id serial PRIMARY KEY,
  embedding vector(3)
);

INSERT INTO items (embedding) VALUES ('[1,2,3]');
INSERT INTO items (embedding) VALUES ('[1.1,2.1,3.1]');
INSERT INTO items (embedding) VALUES ('[10,20,30]');
INSERT INTO items (embedding) VALUES ('[0.9,1.9,2.9]');
INSERT INTO items (embedding) VALUES ('[1.2,2.2,3.2]');

Euclidean Distance Query Example

Now, let's find the 3 items whose embeddings are closest to [1,2,3] using Euclidean distance.

Notice the <-> operator in action!

SELECT
  id,
  embedding,
  embedding <-> '[1,2,3]' AS euclidean_distance
FROM items
ORDER BY euclidean_distance
LIMIT 3;

Interpreting Euclidean Results

When you run the query, you'll see a euclidean_distance column. The items with the smallest distance values are the most similar to your query vector [1,2,3].

For example, [1.1,2.1,3.1] should have a very small Euclidean distance, indicating high similarity.

Cosine Distance Query Example

Next, let's perform a similarity search using cosine distance. We'll again query for items similar to [1,2,3].

Observe the <#> operator. Remember, it returns cosine distance, where lower values mean higher similarity.

SELECT
  id,
  embedding,
  embedding <#> '[1,2,3]' AS cosine_distance
FROM items
ORDER BY cosine_distance
LIMIT 3;

Interpreting Cosine Results

The cosine_distance column shows how aligned the vectors are. A value close to 0 means the vectors point in almost the same direction (very similar).

A value close to 2 means they point in opposite directions (very dissimilar). Values near 1 mean they are orthogonal.

Choosing the Right Metric

Which metric should you use?

  • Euclidean distance is great when the magnitude (length) of the vector is important.
  • Cosine similarity/distance is preferred when only the direction of the vector matters, not its length. This is common for text embeddings where vector length might vary but direction captures semantic meaning.

Quick Check: Operators

You've learned about two key pgvector operators for similarity queries. Let's test your knowledge!

Recap: Similarity Queries

Great job! You've learned how to perform similarity queries with pgvector.

  • We use distance metrics to quantify similarity.
  • Euclidean distance (<->) measures straight-line distance.
  • Cosine distance (<#>) measures the angle between vectors.
  • Choosing the right metric depends on whether vector magnitude or direction is more relevant for your data.

คำถามที่พบบ่อย

บทเรียน “การดำเนินการค้นหาความคล้ายคลึง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การดำเนินการค้นหาความคล้ายคลึง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การดำเนินการค้นหาความคล้ายคลึง”

ดำเนินการค้นหาความคล้ายคลึงของเวกเตอร์เบื้องต้นด้วยตัวดำเนินการ pgvector เพื่อค้นหาเพื่อนบ้านที่ใกล้ที่สุดในข้อมูลของคุณ คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การดำเนินการค้นหาความคล้ายคลึง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม

ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตั้งค่าส่วนขยาย pgvector
  2. การจัดเก็บเวกเตอร์ใน PostgreSQL
  3. การดำเนินการค้นหาความคล้ายคลึง
  4. เลือกตัวชี้วัดระยะห่างใน pgvector
← กลับไปที่ Vector Databases: Pinecone, Weaviate & pgvector