Develop
Distance Functions
Currently we support the following distance functions:
Name | Operator Class | Function | Data Types | Operator |
---|---|---|---|---|
Euclidean | dist_l2sq_ops | l2sq_dist | REAL[] , VECTOR | <-> |
Cosine | dist_cos_ops | cos_dist | REAL[] , VECTOR | <-> |
Hamming | dist_hamming_ops | hamming_dist | INTEGER[] | <-> |
To create an index you can use the following syntax:
CREATE INDEX ON [TABLE] USING hnsw ([column] [operator class]) WITH (m=[int], ef_construction=[int], ef=[int]);
Example of creating and index which will use cosine function for distance calculation
CREATE INDEX ON lantern_demo USING hnsw (v dist_cos_ops) WITH (m=8, ef_construction=64, ef=128);
You can also use the provided distance functions without index:
SELECT l2sq_dist(ARRAY[0,0.1,0], ARRAY[0.5,0.0,0.2]); -- Euclidean
SELECT cos_dist(ARRAY[0,1,0], ARRAY[1,1,1]); -- Cosine
SELECT hamming_dist(ARRAY[0,1,0], ARRAY[1,1,1]); -- Hamming
Was this page helpful?