accesspatterns.dev

Three ways to be near

The metric is a decision you make once, at index creation, and cannot change.

Why

Three indexes, one rgb attribute, three distance functions. The vectors are identical in all three, so every difference in the results is the metric and nothing else.

Cosine measures the angle between two vectors and throws away their length. For colours that means hue without brightness. Euclidean measures the straight-line gap, so brightness is distance. Dot product combines direction and magnitude, which is why a pale mint green beats a crimson against a red query: it is simply a longer vector.

Read the scores carefully, because they do not all run the same way. Cosine and Euclidean are distances: lower is nearer, and a vector against itself scores about zero. Dot product is a similarity: higher is nearer, and it can go negative. Sorting a dot-product result the way you would sort a cosine result gives you a ranking that is exactly inverted, and it still returns TopK rows and still looks like it worked.

You choose the function when you create the index and you cannot change it afterwards; changing your mind means a new index. A table holds at most five vector indexes, and two indexes over the same attribute have to agree on dimensions. Which to pick depends on your model: most sentence embedding models are normalised to unit length, and once every vector is the same length cosine and dot product rank identically.

Cosine ignores magnitude, Euclidean does not, and dot product rewards it; the choice is fixed at index creation, and lower-is-nearer for two of them and higher-is-nearer for the third.

PaintMetrics

These rows are real and already here. Running an operation against them downloads a DynamoDB-compatible engine, about 850 KB, and executes it in this tab.

Try an example

Brick and Crimson come back first, together. Cosine compares direction and ignores magnitude, so a dark red and a bright red are near-identical to it: same hue, different brightness.

pk(pk)
name
family
rgb
COLOUR#crimson
CrimsonS
redS
[220, 20, 60]L
COLOUR#brick
BrickS
redS
[140, 30, 30]L
COLOUR#rose
RoseS
redS
[250, 120, 130]L
COLOUR#navy
NavyS
blueS
[20, 30, 110]L
COLOUR#azure
AzureS
blueS
[40, 110, 230]L
COLOUR#sky
SkyS
blueS
[140, 200, 245]L
COLOUR#forest
ForestS
greenS
[20, 90, 40]L
COLOUR#lime
LimeS
greenS
[110, 220, 60]L
COLOUR#mint
MintS
greenS
[170, 230, 190]L
COLOUR#amber
AmberS
yellowS
[250, 190, 40]L
COLOUR#rust
RustS
yellowS
[180, 90, 20]L
COLOUR#slate
SlateS
neutralS
[90, 100, 110]L
COLOUR#charcoal
CharcoalS
neutralS
[40, 42, 45]L

Run an operation to see the raw engine response.

table PaintMetricskeys pkitems 13