Neo4j Vector Index and Search

Grounding LLM Responses with Implicit and Explicit Search Through Neo4js Knowledge Graph 2048x1152

The Neo4j Vector index implements HNSW (Hierarchical Navigatable Small World) for creating layers of k-nearest neighbors to enable efficient and robust approximate nearest neighbor search. The index is designed to work with embeddings, such as those generated by machine learning models, and can be used to find similar nodes in a graph based on their embeddings.

Functionality Includes

  • Create a vector index with a specified number of dimensions and similarity function (euclidean, cosine)

  • Query vector index with embedding and top-k, returning nodes and similarity score

  • Functions to compute text vector embeddings with OpenAI, AWS Bedrock, Google Vertex AI, and other ML platforms

Usage

// create vector index
CREATE VECTOR INDEX `abstract-embeddings`
FOR (n: Abstract) ON (n.embedding)
OPTIONS {indexConfig: {
 `vector.dimensions`: 1536,
 `vector.similarity_function`: 'cosine'
}};

// set embedding as parameter
MATCH (a:Abstract {id: $id})
CALL db.create.setNodeVectorProperty(a, 'embedding', $embedding);

// use a genai function to compute the embedding
MATCH (a:Abstract {id: $id})
WITH a, genai.vector.encode(a.text, "OpenAI", { token: $token }) AS embedding
CALL db.create.setNodeVectorProperty(n, 'embedding', embedding);

// query vector index for similar abstracts
MATCH (title:Title)<--(:Paper)-->(abstract:Abstract)
WHERE toLower(title.text) = 'efficient and robust approximate nearest neighbor search using
  hierarchical navigable small world graphs'

CALL db.index.vector.queryNodes('abstract-embeddings', 10, abstract.embedding)
YIELD node AS similarAbstract, score

MATCH (similarAbstract)<--(:Paper)-->(similarTitle:Title)
RETURN similarTitle.text AS title, score;

Documentation

Authors

Neo4j Engineering

Community Support

Neo4j Online Community

Repository

GitHub

Issues

https://github.com/neo4j/neo4j/issues

Documentation

Vector Index and Search

Documentation

GenAI Embedding Functions

Videos & Tutorials