HNSW vector index

HNSW 벡터 인덱스

Follow layered proximity graphs to find neighbors of a query vector.

···
html
<div class="v"><header><b>HNSW LAYERS</b><span id="state"></span></header><main><div class="hlevels" id="hlevels"></div><div class="hkey">upper jumps → lower refinement</div></main><footer>sparse graph → dense graph</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,4.5vmin,19px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.45} .hlevels{height:76%;display:flex;flex-direction:column;justify-content:space-around;gap:3px}.hrow{display:flex;align-items:center;gap:5px}.hrow small{width:40px;color:var(--muted);font-size:.72em}.hnode{height:clamp(20px,5vmin,30px);min-width:clamp(20px,5vmin,30px);border:1px solid var(--line);border-radius:50%;display:grid;place-items:center;font:600 .75em ui-monospace,monospace}.hline{flex:1;height:1px;background:var(--line)}.hkey{text-align:center;color:var(--muted);font-size:.75em}
js
const rows=[["A","C"],["A","B","C"],["A","B","D","C"]];let step=0;function draw(){document.getElementById("hlevels").innerHTML=rows.map((row,layer)=>"<div class=hrow><small>L"+(2-layer)+"</small>"+row.map((node,i)=>"<b class='hnode "+(layer===Math.floor(step/2)&&i===(step%2?row.length-1:0)?"active":"")+"'>"+node+"</b>"+(i<row.length-1?"<i class=hline></i>":"")).join("")+"</div>").join("");document.getElementById("state").textContent=["entry","long jump","refine","neighbor","fine search","nearest"][step];step=(step+1)%6}draw();setInterval(draw,850)

HNSW arranges vector-neighbor links in several graph layers. Sparse upper layers make long jumps; denser lower layers refine the search near the query.

It avoids comparing every vector, so results are approximate. Graph degree and candidate breadth affect recall, memory, and query latency.

When to use

Use it when designing or comparing approximate nearest-neighbor indexes for large vector sets.

Open as page ↗