> For the complete documentation index, see [llms.txt](https://docs.hyper-space.io/hyperspace-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hyper-space.io/hyperspace-docs/~/changes/uCQNjcW7J3OXknfYJaVa/projects/setting-up/creating-a-database-schema-configuration-file/vector-search-metrics.md).

# Vector Search Metrics

The Metric defines the distance measure between vectors during the vector search. From a mathematical point of view, the K nearest neighbors are those with the smallest distance from the search query vector. By contrast, in vector search higher score reflects greater similarity. Hyperspace bridges this gap using algebraic relations between the distance and score, as described below.

<table><thead><tr><th width="124.33333333333331">Metric</th><th width="306">          Distance Measure</th><th>            Hyperspace Score</th></tr></thead><tbody><tr><td>l2</td><td><span class="math">d(X, Y) = \sum_{i} (x_i - y_i)^2</span></td><td><span class="math">\frac{1}{1 + d}</span></td></tr><tr><td>ip</td><td><span class="math">d(X,Y)=-∑_i  x _i ⋅y_ i </span></td><td><span class="math">\begin{cases}     \frac{1}{1 + d} &#x26; \text{if } d \geq 0 \\     1 - d &#x26; \text{d&#x3C;0} \end{cases}</span></td></tr><tr><td>hamming</td><td><span class="math">d(X,Y)= \sum_{i=1}^{n} \delta(x_i, y_i) </span></td><td><span class="math">\frac{1}{1 + d}</span></td></tr></tbody></table>

### Euclidean Metric (L2) <a href="#euclidean-distance-l2" id="euclidean-distance-l2"></a>

The $$L2$$  metric represents the distance between 2 points in a Euclidean space. The metric is derived from the Pythagorean theorem and represents the length of the shortest path between the two points.

The $$L2$$  metric is a special case of the $$Lp$$  metric, given by

$$
L^p(X, Y) = \left( \sum\_{i} |x\_i - y\_i|^p \right)^{\frac{1}{p}}
$$

In Hyperspace, we use the squared  $$L2$$  metric.

### Inner Product (ip) <a href="#euclidean-distance-l2" id="euclidean-distance-l2"></a>

The inner product metric, denoted as $$X \cdot Y$$, is a measure of similarity between two vectors. The mathematical operation calculates the projection of one vector on the other, which is 1 if the vectors are parallel and 0 if they are perpendicular. In a Euclidean distance, the inner product is given by

$$
X \cdot Y =∑\_i  x *i ⋅y* i
$$

### Hamming Distance (hamming) <a href="#euclidean-distance-l2" id="euclidean-distance-l2"></a>

The Hamming distance quantifies the difference between two strings of equal length by measuring the number of positions at which the corresponding symbols differ. For  binary strings, it counts the bits that are different between two binary vectors. The lower the Hamming distance, the more similar the strings are considered to be, and vice versa.

The hamming distance is calculated as

$$
\text{HammingDistance}(X, Y) = \sum\_{i=1}^{n} \delta(x\_i, y\_i)
$$

where  $$\delta(x\_i, y\_i)  =\begin{cases}     1 & \text{if }   x\_i=y\_i\     0 & \text{otherwise} \end{cases}$$ is the Kronecker delta. The above formula counts the number of characters that are different between $$X$$ and $$Y$$ .
