Local graph search

Local graph search answers a question from the part of the knowledge graph around the entities it’s about. It’s the GraphRAG pipeline for local questions, such as “What is connected to NVIDIA?”

It has two steps:

  1. Pivot search finds the starting nodes, called pivot nodes, with vector, text or geospatial search.
  2. Relevance expansion collects the context around the pivot nodes with traversals, community detection or centrality algorithms.

In this example from medical records, pivot search finds the patient a question names, and relevance expansion adds the symptoms, treatments and similar patients connected to them before the context goes to the LLM:

A question about a patient's symptoms goes through pivot search, which finds the patient node, then relevance expansion, which adds related symptoms, treatments and patients, and the collected records are added to the LLM prompt

Which methods to use depends on your data and the questions you expect. See Choose a retrieval strategy.

Retrieval types

The methods below are grouped by the step they belong to.

Pivot search finds the nodes a question is about, where the retrieval starts.

Vector search finds nodes by the similarity of their embeddings, not by exact words. Use it when the meaning of the question matters more than its terms, for example to find similar topics or concepts.

For the question “Which European city is known for its romantic atmosphere and iconic landmarks like the Eiffel Tower?”, vector search finds the node most similar to “romantic atmosphere” and “iconic landmarks”, which leads to Paris:

The question's embedding is compared with node embeddings, and the node with the highest cosine similarity, France with the capital Paris, gives the result Paris

Read more

Text search finds nodes by the words in their properties. Use it for questions that name specific terms, phrases or entities, for example “Find all nodes that mention climate change”.

For the question “What is the capital of France?”, the keywords map to the name and capital properties, and a text search query finds the node:

Keywords extracted from the question are mapped to graph properties, a text_search.search query finds the France node, and the result is its capital, Paris

For details, see Text search.

Geospatial search finds nodes by location. Store locations as the Point type, with latitude and longitude, and create a point index for proximity queries.

For the question “Find cities within 50 km of Paris”, the query compares the distance between city locations and returns Versailles:

The question becomes a query that compares point distances from Paris and returns cities within 50 km, Versailles

Hybrid knowledge retrieval

Hybrid retrieval combines text, vector and geospatial search in one query, for questions whose results must match on meaning, specific terms and location at once. For example:

Find all nearby Italian restaurants with excellent reviews and vegetarian options.

Relevance expansion

Relevance expansion adds the entities and relationships connected to the pivot nodes to the context. It’s optional, and it gives the LLM the connections that a similarity search alone doesn’t return.

Deep path traversals

Deep path traversals follow relationships from the pivot nodes across one or more hops. Memgraph has them built into Cypher, so they need no extra module. The nodes and relationships on the paths go into the LLM prompt as context.

For the question “Who is working on ProjectX?”, pivot search finds the ProjectX node, and a one-hop expansion returns the people connected to it:

Pivot search finds the ProjectX node, a one-hop relevance expansion returns the Person nodes Alice and Bob connected by WORKS_ON, and the result is that Alice and Bob work on ProjectX

Read more:

Community detection

Community detection algorithms, such as Leiden or Louvain, group closely connected nodes into communities. You can summarize each community into a community node and add the summary of a pivot node’s community to the context, instead of searching the whole graph:

Researchers and papers grouped into two communities, each with a Community node that holds a summary of its members and is connected to them by INCLUDES

Centrality measures

Centrality algorithms rank nodes by their position in the graph, so you can put the most central or influential nodes first in the context:

  • PageRank ranks nodes by influence, which puts highly connected information first.
  • Betweenness centrality ranks the nodes that connect communities or topics, so the context covers how they relate.

For example, if several nodes could answer a question, rank them by PageRank and keep the most important ones.

Choose a retrieval strategy

Choose the methods for each step by the kind of question you expect:

QuestionPivot searchRelevance expansionExample
FactVector or text searchLittle or none“Where was Memgraph founded?”
RelationshipVector or text search1-hop or 2-hop traversal“Who collaborates on Memgraph?”
ContextVector or text searchCommunity detection“Can you give me more details about Memgraph?”
PathVector or text searchDeep path traversal“How is Alice connected to Memgraph?”
LocationGeospatial search1-hop traversal“How many employees work within a 50-mile radius?”

Most applications need some tuning beyond this table. For complete applications, see the GraphRAG examples.