SKEG · CONCEPTS

From a question to results

Follow a help-centre question through vectors, scores and a missed article.

Skeg 0.8 · Development documentation

Someone opens your help centre and asks, “How do I get back into my account?” You want to show articles about passwords and account access before an article about invoices. How does that question become a list of article IDs and scores?

This page follows that search. First we give the documents coordinates, then calculate their scores, and finally see how a fast search can miss a good answer.

1. Give the question and the articles a common representation

To compare sentences by vector similarity, your application first passes the article text and the question through an embedding model. The model returns a vector: an ordered list of numbers. The search then compares these vectors.

For this explanation we choose two coordinates by hand. They illustrate the calculation; they are not the output of an embedding model for these sentences. The letters identify articles in the diagram:

ArticleLabelVector
Reset your passwordA(1, 0)
Download an invoiceB(0, 1)
Recover account accessC(0.8, 0.6)
The user’s questionqq(1, 0)

All four vectors have length 1. For C, this follows from 0.82+0.62=0.64+0.36=10.8^2 + 0.6^2 = 0.64 + 0.36 = 1.

Three unit vectors: A and the query point right, B points up, and C lies between them.

A lies exactly along the query. C points partly in that direction. B is perpendicular to it. The diagram therefore predicts the password article first, account access second and invoices third.

2. Turn the comparison into a score

For two nonzero vectors xx and qq, cosine similarity is their dot product divided by their lengths:

cosine⁡(x,q)=x⋅q∥x∥2 ∥q∥2\operatorname{cosine}(x,q) = \frac{x \cdot q}{\lVert x\rVert_2\,\lVert q\rVert_2}

The dot product x⋅qx \cdot q multiplies corresponding coordinates and adds them. The notation ∥x∥2\lVert x\rVert_2 means the Euclidean length of xx. For our two-dimensional vectors:

x⋅q=x1q1+x2q2,∥x∥2=x12+x22x \cdot q = x_1q_1 + x_2q_2, \qquad \lVert x\rVert_2 = \sqrt{x_1^2 + x_2^2}

The subscripts 1 and 2 identify the first and second coordinates. Dividing by the lengths removes their scale and compares their directions.

Since every length is 1, the score in this example is just the dot product.

ItemCalculation against q = (1, 0)Score
A1 × 1 + 0 × 01
C0.8 × 1 + 0.6 × 00.8
B0 × 1 + 1 × 00

If we request the two highest scores, the exact answer is A followed by C. The score 0.8 is a geometric similarity, not an 80% probability of relevance. Whether nearby vectors represent useful matches depends on the model and data. The zero vector has no direction, so cosine similarity is undefined for it.

3. Understand why a precisely scored result can still miss an article

With three articles, comparing every vector is easy. With a large archive, doing that for every question can take too much work. An approximate index looks for promising articles first. Those articles form the candidate list. Reranking calculates more precise scores for that list and puts it in order.

Consider two possible candidate sets for the same query:

Candidates foundScores after exact rerankingReturned top two
A, B, CA: 1; C: 0.8; B: 0A, C
B, CC: 0.8; B: 0C, B

In the second case the password article A never enters the candidate list. Its score would have been the highest, but the reranking step never gets to calculate it. The user sees account access and invoices instead.

This is the limitation to remember when tuning a search: accurate scores for the articles found do not tell you whether a better article was missed.

4. Measure how many of the best articles were found

Recall answers that question. In the second search we wanted A and C, but recovered only C. We found one of the two desired articles, so recall@2 is 1/21/2, or 50%. Recovering both would give 100% for this query.

We can state that limitation with sets. Let DD be the complete dataset and C(q)\mathcal{C}(q) the candidates found for query qq. The expression

C(q)⊆D\mathcal{C}(q) \subseteq D

means that every candidate belongs to the dataset. Let T2(q)T_2(q) be the exact top two in the entire dataset, and R2(q)R_2(q) the two highest-scoring members of the candidate set after reranking. In our example:

T2(q)={A,C},recall@2⁡(q)=∣R2(q)∩T2(q)∣2T_2(q)=\{A,C\}, \qquad \operatorname{recall@2}(q) =\frac{\left|R_2(q)\cap T_2(q)\right|}{2}

The intersection symbol ∩\cap selects items present in both sets; the bars ∣⋅∣|\cdot| count those items. The denominator is the two exact neighbours we wanted to recover.

With candidates {B,C}\{B,C\}, the intersection is {C}\{C\}, so recall@2 is 1/21/2. With candidates {A,B,C}\{A,B,C\}, it is {A,C}\{A,C\}, so recall@2 is 11. Here there are no tied scores and at least two candidates. A general evaluation must also specify tie handling, filters and what happens with fewer results.

5. Ask a different question

Now let q=(0,1)q = (0,1). Calculate the dot products again: B scores 1, C scores 0.6, and A scores 0. The exact top two become B and C. The stored articles did not change; the query changed which direction matters. In an application, a different sentence produces its own query vector through the same embedding model used for the documents.

Use these ideas in a running server

Skeg’s disk search uses candidate search and reranking; see the architecture explanation. The number of requested results and the search effort are different controls. Choose search settings by measuring the recall and latency of representative queries.

Follow getting started to store these three articles and retrieve their titles. Then add a language or date filter, or learn how to choose search effort when a search is too slow or misses useful neighbours.