> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentfront.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# HNSW Configuration

> HNSW parameter reference and configuration options

Learn about HNSW parameters and how they affect performance.

## Parameter Reference

| Option           | Default | Description                                                |
| ---------------- | ------- | ---------------------------------------------------------- |
| `M`              | 16      | Connections per node in layer > 0 (higher = better recall) |
| `M0`             | 32      | Connections for layer 0 (typically M x 2)                  |
| `efConstruction` | 200     | Candidate list size during construction                    |
| `efSearch`       | 50      | Candidate list size during search                          |

## M (Max Connections)

Controls the number of connections each node has in the graph.

```ts title="src/hnsw-m-param.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Low M (8-12) - faster build, less memory, lower recall
const fast = new VectoriaDB({
  useHNSW: true,
  hnsw: { M: 8 },
});

// High M (24-48) - slower build, more memory, higher recall
const accurate = new VectoriaDB({
  useHNSW: true,
  hnsw: { M: 32 },
});
```

### M Impact

| M Value | Build Speed | Memory | Recall |
| ------- | ----------- | ------ | ------ |
| 8       | Fast        | Low    | \~93%  |
| 16      | Medium      | Medium | \~96%  |
| 32      | Slow        | High   | \~98%  |

## efConstruction

Controls build-time quality. Higher values = better graph structure but slower indexing.

```ts title="src/hnsw-ef-construction.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Fast construction, acceptable quality
const quickBuild = new VectoriaDB({
  useHNSW: true,
  hnsw: { efConstruction: 100 },
});

// Slow construction, excellent quality
const highQuality = new VectoriaDB({
  useHNSW: true,
  hnsw: { efConstruction: 400 },
});
```

### efConstruction Impact

| Value | Build Speed | Index Quality |
| ----- | ----------- | ------------- |
| 100   | Fast        | Good          |
| 200   | Medium      | Very Good     |
| 400   | Slow        | Excellent     |

## efSearch

Controls search-time quality. Can be adjusted per-query:

```ts title="src/hnsw-ef-search.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const db = new VectoriaDB({
  useHNSW: true,
  hnsw: { efSearch: 50 }, // Default
});

// High-precision search
const results = await db.search(query, {
  topK: 10,
  efSearch: 200, // Override for this query
});

// Fast search (lower recall)
const quick = await db.search(query, {
  topK: 10,
  efSearch: 20,
});
```

### efSearch Impact

| Value | Search Speed | Recall |
| ----- | ------------ | ------ |
| 20    | Very Fast    | \~92%  |
| 50    | Fast         | \~96%  |
| 100   | Medium       | \~98%  |
| 200   | Slow         | \~99%  |

## Configuration Presets

```ts title="src/hnsw-presets.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Speed-optimized (95%+ recall)
const speedOptimized = {
  M: 12,
  efConstruction: 100,
  efSearch: 30,
};

// Balanced (97%+ recall)
const balanced = {
  M: 16,
  efConstruction: 200,
  efSearch: 50,
};

// Quality-optimized (99%+ recall)
const qualityOptimized = {
  M: 32,
  efConstruction: 400,
  efSearch: 100,
};
```

## Full Configuration Example

```ts title="src/hnsw-full-config.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const db = new VectoriaDB<ToolDocument>({
  useHNSW: true,
  hnsw: {
    M: 16,              // Connections per node
    M0: 32,             // Layer 0 connections (optional)
    efConstruction: 200, // Build quality
    efSearch: 50,       // Search quality
  },
  maxDocuments: 100000,
  maxBatchSize: 1000,
});
```

## Related

<CardGroup cols={3}>
  <Card title="HNSW Overview" icon="chart-network" href="/vectoriadb/guides/scaling/hnsw-overview">
    Introduction to HNSW
  </Card>

  <Card title="HNSW Tuning" icon="sliders" href="/vectoriadb/guides/scaling/hnsw-tuning">
    Tune for your use case
  </Card>

  <Card title="Search Performance" icon="gauge" href="/vectoriadb/guides/search/performance">
    General performance tips
  </Card>
</CardGroup>
