Skip to main content

Create Non Linear Algorithm Index

The CreateNonLinearAlgorithmIndex request creates a non-linear index (KDTree or HNSW) to speed up similarity searches.

  • Input: Store name and list of non-linear indices to create.

  • Behavior: Builds the specified non-linear index structure for faster similarity queries.

  • Response: Confirmation of index creation.

Create a KDTree Index​

Click to expand source code
import { createDbClient } from "ahnlich-client-node";
import { CreateNonLinearAlgorithmIndex } from "ahnlich-client-node/grpc/db/query_pb";
import { NonLinearIndex, KDTreeConfig } from "ahnlich-client-node/grpc/algorithm/nonlinear_pb";

async function createKDTreeIndex() {
const client = createDbClient("127.0.0.1:1369");

await client.createNonLinearAlgorithmIndex(
new CreateNonLinearAlgorithmIndex({
store: "my_store",
nonLinearIndices: [
new NonLinearIndex({
index: { case: "kdtree", value: new KDTreeConfig() },
}),
],
})
);

console.log("KDTree index created successfully");
}

createKDTreeIndex();

Create an HNSW Index​

Click to expand source code
import { createDbClient } from "ahnlich-client-node";
import { CreateNonLinearAlgorithmIndex } from "ahnlich-client-node/grpc/db/query_pb";
import { NonLinearIndex, HNSWConfig } from "ahnlich-client-node/grpc/algorithm/nonlinear_pb";

async function createHNSWIndex() {
const client = createDbClient("127.0.0.1:1369");

await client.createNonLinearAlgorithmIndex(
new CreateNonLinearAlgorithmIndex({
store: "my_store",
nonLinearIndices: [
new NonLinearIndex({
index: { case: "hnsw", value: new HNSWConfig() },
}),
],
})
);

console.log("HNSW index created successfully");
}

createHNSWIndex();

Parameters​

ParameterTypeRequiredDescription
storestringYesThe name of the store
nonLinearIndicesNonLinearIndex[]YesList of indices to create

Index Types​

TypeDescriptionBest For
KDTreeK-dimensional treeLower dimensions (less than 20), exact searches
HNSWHierarchical Navigable Small WorldHigh dimensions, approximate but fast

Notes​

  • Non-linear indices dramatically improve GetSimN performance on large stores
  • HNSW is generally recommended for high-dimensional embeddings (128+)
  • Building indices takes time and memory proportional to store size
  • You can have multiple index types on the same store