Skip to main content
Neo4j is an open-source graph database with integrated support for vector similarity search
It supports:
  • approximate nearest neighbor search
  • Euclidean similarity and cosine similarity
  • Hybrid search combining vector and keyword searches
This notebook shows how to use the Neo4j vector index (Neo4jVector). See the installation instruction.
We want to use OpenAIEmbeddings so we have to get the OpenAI API Key.
The langchain-community package is no longer maintained. Examples that import from langchain_community may be outdated or broken. Use with caution.

Similarity search with cosine distance (Default)

Working with vectorstore

Above, we created a vectorstore from scratch. However, often times we want to work with an existing vectorstore. In order to do that, we can initialize it directly.
We can also initialize a vectorstore from existing graph using the from_existing_graph method. This method pulls relevant text information from the database, and calculates and stores the text embeddings back to the database.
Neo4j also supports relationship vector indexes, where an embedding is stored as a relationship property and indexed. A relationship vector index cannot be populated via LangChain, but you can connect it to existing relationship vector indexes.

Metadata filtering

Neo4j vector store also supports metadata filtering by combining parallel runtime and exact nearest neighbor search. Requires Neo4j 5.18 or greater version. Equality filtering has the following syntax.
Metadata filtering also support the following operators:
  • $eq: Equal
  • $ne: Not Equal
  • $lt: Less than
  • $lte: Less than or equal
  • $gt: Greater than
  • $gte: Greater than or equal
  • $in: In a list of values
  • $nin: Not in a list of values
  • $between: Between two values
  • $like: Text contains value
  • $ilike: lowered text contains value
You can also use OR operator between filters

Add documents

We can add documents to the existing vectorstore.

Customize response with retrieval query

You can also customize responses by using a custom Cypher snippet that can fetch other information from the graph. Under the hood, the final Cypher statement is constructed like so:
The retrieval query must return the following three columns:
  • text: Union[str, Dict] = Value used to populate page_content of a document
  • score: Float = Similarity score
  • metadata: Dict = Additional metadata of a document
Learn more in this blog post.
Here is an example of passing all node properties except for embedding as a dictionary to text column,
You can also pass Cypher parameters to the retrieval query. Parameters can be used for additional filtering, traversals, etc…

Hybrid search (vector + keyword)

Neo4j integrates both vector and keyword indexes, which allows you to use a hybrid search approach
To load the hybrid search from existing indexes, you have to provide both the vector and keyword indices

Retriever options

This section shows how to use Neo4jVector as a retriever.

Question answering with sources

This section goes over how to do question-answering with sources over an Index. It does this by using the RetrievalQAWithSourcesChain, which does the lookup of the documents from an Index.