اختيار مقاييس المسافة في pgvector
افهم عوامل تشغيل مسافة cosine وL2 والجداء الداخلي في pgvector، وكيفية اختيار العامل المناسب لـ Embeddings الخاصة بك.
اختيار مقاييس المسافة في pgvector درس مجاني في Vector Databases: Pinecone, Weaviate & pgvector على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Vector Databases: Pinecone, Weaviate & pgvector، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Vector Databases: Pinecone, Weaviate & pgvector 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Distance Matters
Similarity search ranks vectors by distance. The metric you choose must match how your embedding model was trained, or results will be subtly wrong.
The Three Operators
pgvector provides three distance operators:
<->Euclidean (L2) distance<=>Cosine distance<#>Negative inner product
SELECT '[1,2,3]'::vector <=> '[1,2,4]'::vector AS cosine_distance;Cosine Distance
Cosine measures angle, ignoring magnitude. It is the default for most text embedding models like OpenAI and Sentence Transformers.
SELECT id, embedding <=> '[0.1,0.2,0.3]'::vector AS dist
FROM items
ORDER BY dist
LIMIT 5;Euclidean (L2) Distance
L2 measures straight-line distance and is sensitive to magnitude. Use it when vector length carries meaning, such as raw feature vectors.
SELECT id, embedding <-> '[0.1,0.2,0.3]'::vector AS dist
FROM items
ORDER BY dist
LIMIT 5;Inner Product
The inner product operator <#> returns the negative dot product (so smaller is more similar). It is fastest and ideal for normalized vectors.
SELECT id, (embedding <#> '[0.1,0.2,0.3]'::vector) * -1 AS similarity
FROM items
ORDER BY embedding <#> '[0.1,0.2,0.3]'::vector
LIMIT 5;Match Metric to Model
Always check your embedding model's documentation:
- OpenAI text-embedding-3: cosine
- Normalized vectors: inner product (equivalent to cosine, faster)
- Geometric/spatial features: L2
Normalization Trick
If your vectors are unit-normalized, cosine and inner product give the same ranking. Inner product is cheaper, so normalize once at insert time.
-- Normalize before insert in app code, then use <#>
UPDATE items SET embedding = l2_normalize(embedding);Metric in Indexes
Indexes are metric-specific. You must declare the operator class so the index matches your query operator.
CREATE INDEX ON items
USING hnsw (embedding vector_cosine_ops);Mismatched Operators
If your index uses vector_l2_ops but you query with <=>, Postgres ignores the index and does a slow sequential scan. Keep them aligned.
Verifying Index Usage
Use EXPLAIN to confirm the planner uses your vector index for the chosen metric.
EXPLAIN ANALYZE
SELECT id FROM items
ORDER BY embedding <=> '[0.1,0.2,0.3]'::vector
LIMIT 5;Summary of Choices
Rule of thumb:
- Text/semantic search: cosine
- Pre-normalized vectors: inner product
- Raw numeric features: L2
Quick Check
Pick the right metric.
Recap
You learned pgvector's three distance operators, when to use cosine, L2, and inner product, and why your index operator class must match your query operator for fast searches.
تعلم Vector Databases: Pinecone, Weaviate & pgvector مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «اختيار مقاييس المسافة في pgvector» مجاني؟
نعم — نص درس «اختيار مقاييس المسافة في pgvector» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Vector Databases: Pinecone, Weaviate & pgvector، انتقل إلى CoddyKit PRO. تتضمن دورة Vector Databases: Pinecone, Weaviate & pgvector 4 دروس في المجموع.
ماذا ستتعلم في «اختيار مقاييس المسافة في pgvector»؟
افهم عوامل تشغيل مسافة cosine وL2 والجداء الداخلي في pgvector، وكيفية اختيار العامل المناسب لـ Embeddings الخاصة بك. تتمرن على Vector Databases: Pinecone, Weaviate & pgvector مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Vector Databases: Pinecone, Weaviate & pgvector؟
لا تُشترط خبرة سابقة. Vector Databases: Pinecone, Weaviate & pgvector على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «اختيار مقاييس المسافة في pgvector»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Vector Databases: Pinecone, Weaviate & pgvector هذا؟
نعم. كل درس في Vector Databases: Pinecone, Weaviate & pgvector يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد إضافة pgvector
- تخزين المتجهات في PostgreSQL
- تنفيذ استعلامات التشابه
- اختيار مقاييس المسافة في pgvector