Shaf Lab

Vector search

Finding items by closeness in embedding space rather than exact keywords.

Vector search finds items by closeness in embedding space, rather than by exact keywords. A query is turned into a vector, and the index returns the nearest neighbors. This is how you find a passage that answers a question even if the wording is different, or a piece of code that does the same job under another name.

It is a better fit than keyword search when the user speaks in sentences, or when the corpus uses many words for the same idea. It is a worse fit when the user is looking for an exact identifier, an error string, or a rare proper name. Those queries still want a lexical index. In practice the best systems run both and blend the results.

Chunking and the embedding model set the ceiling on quality. A vector for a whole chapter is usually too coarse; a vector for a half sentence is usually too thin. Return the original text, not just an id, and show enough of it that a person or a model can use it.

Evaluate with real questions and a simple metric, such as whether the right passage is in the top few hits. If it is not, a larger language model downstream will not save you.

HNSW

Hierarchical navigable small world: a layered graph index for fast approximate vector search.

HNSW, short for hierarchical navigable small world, is a graph index for approximate nearest neighbor search and one of the most widely used. Every vector is a node linked to some of its near neighbors. The graph has layers: the top layers hold few nodes with long-range links, and the bottom layer holds every node. A search starts at the top, moves greedily toward the query, drops to the next layer, and refines until it reaches the bottom.

A few settings control the trade-offs. The number of links per node, often called M, raises recall and memory use. The candidate list size during index building improves graph quality at the cost of slower builds. The candidate list size during search, often called ef, lets you trade query speed for recall at query time. HNSW is fast and accurate but keeps the graph in memory and handles heavy deletes less gracefully than some alternatives.