0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · Lekcja

Wybór metryk odległości w pgvector

Zrozum operatory odległości cosine, L2 i inner product w pgvector oraz dowiedz się, jak wybrać właściwy operator dla swoich embeddingów.

Wybór metryk odległości w pgvector to bezpłatna lekcja Vector Databases: Pinecone, Weaviate & pgvector na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Vector Databases: Pinecone, Weaviate & pgvector, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Vector Databases: Pinecone, Weaviate & pgvector zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Wybór metryk odległości w pgvector” jest bezpłatna?

Tak — pełny tekst „Wybór metryk odległości w pgvector” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Vector Databases: Pinecone, Weaviate & pgvector, przejdź na CoddyKit PRO. Kurs Vector Databases: Pinecone, Weaviate & pgvector zawiera 4 lekcji w sumie.

Co nauczysz się w „Wybór metryk odległości w pgvector”?

Zrozum operatory odległości cosine, L2 i inner product w pgvector oraz dowiedz się, jak wybrać właściwy operator dla swoich embeddingów. Ćwiczysz Vector Databases: Pinecone, Weaviate & pgvector z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Vector Databases: Pinecone, Weaviate & pgvector?

Nie wymagamy żadnego doświadczenia. Vector Databases: Pinecone, Weaviate & pgvector w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Wybór metryk odległości w pgvector”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Vector Databases: Pinecone, Weaviate & pgvector?

Tak. Każda lekcja Vector Databases: Pinecone, Weaviate & pgvector zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Konfigurowanie rozszerzenia pgvector
  2. Przechowywanie wektorów w PostgreSQL
  3. Wykonywanie zapytań o podobieństwo
  4. Wybór metryk odległości w pgvector
← Powrót do Vector Databases: Pinecone, Weaviate & pgvector