recallA retrieval
field guide

Knowledge Graphs for Retrieval

Build this skill

Represent knowledge as entities and relations, extract triples from text and answer multi-hop questions by following edges.

Text search treats documents as bags of passages. But much of what an organisation knows is about things and how they relate: Dr Rao is warden of Block C; Block C houses first-year students; Dr Rao is a member of the anti-ragging committee; the anti-ragging committee reports to the Dean of Students. A question like "who should a first-year contact about ragging in their hostel?" depends on following those links.

A knowledge graph stores this kind of knowledge directly. It is made of nodes (also called entities: people, places, documents, courses, committees) and edges (relationships between them, such as "is warden of" or "reports to"). Each fact is a small sentence of three parts, a triple: subject – relation – object, for example (Dr Rao, is warden of, Block C).

Dean of StudentsoverseesAnti-raggingcommitteehas memberDr Raomemberis warden ofBlock Cfirst-year hosteloverseesHostel officerunsMess committee

Entities connected by named relationships. Following edges answers questions that no single document states.

Why graphs help with multi-hop questions

Some questions need several facts in a chain, each depending on the last. These are called multi-hop questions. "Is the warden of the first-year hostel on the anti-ragging committee?" needs: which hostel houses first-years (Block C) → who is its warden (Dr Rao) → is Dr Rao on the committee (yes). Each fact may sit in a different document, phrased differently, and a single similarity search for the whole question rarely retrieves all three.

In a graph, the same question is a short walk: start at "first-year students", follow "housed in" to Block C, follow "has warden" to Dr Rao, and check whether Dr Rao has a "member of" edge to the anti-ragging committee. Graphs are also good at questions about connections ("which committees include faculty from the CSE department?") and at listing everything linked to something ("all policies that mention the exam cell").

The program below stores a few triples and answers the multi-hop question by following edges.

python · runs in your browser

Expected output:

text
hop 1: first-year students -> Block C
hop 2: Block C -> warden Dr Rao
hop 3: is Dr Rao on the anti-ragging committee? True

Each triple is (subject, relation, object). follow(entity, relation) scans the triples and returns every object linked to that entity by that relation. The three lines after it chain three calls together, each starting from the answer to the one before, and that chaining is what "multi-hop" means.

Follow the links yourself in the lab.

Building a graph from documents

Graphs used to be built by hand, which was slow. Today, a language model can read documents and extract entities and relationships automatically. Given the sentence "Dr Rao, warden of Block C, has been appointed to the anti-ragging committee", the model outputs triples such as (Dr Rao, is warden of, Block C) and (Dr Rao, member of, anti-ragging committee).

Automatic extraction brings two problems that need care. Entity resolution: "Dr Rao", "Dr. K. Rao" and "the Block C warden" must be recognised as the same node, or the graph splits one person into three. Relation consistency: "is warden of", "wardens" and "in charge of" should map to one relation type, so a fixed list of allowed relation names (a schema) helps a lot. Extracted graphs should be spot-checked, because a wrong edge produces confidently wrong answers.

Storing and querying graphs: Neo4j and Cypher

Graphs are usually stored in a graph database. Neo4j is one of the most widely used, and it is queried with a language called Cypher, which describes patterns of nodes and edges using arrows. The multi-hop question above looks like this in Cypher:

cypher
MATCH (:Group {name: "first-year students"})-[:HOUSED_IN]->(h:Hostel)
      -[:HAS_WARDEN]->(w:Person)-[:MEMBER_OF]->(c:Committee {name: "Anti-ragging committee"})
RETURN h.name AS hostel, w.name AS warden

Read it as a picture: a group, housed in a hostel, which has a warden, who is a member of the committee. A language model can be asked to write such queries from a user's question (an idea similar to text-to-SQL), with the same need for care about safety and correctness.

Graphs and vector search together

Graphs and vector search are not rivals. A common pattern, sometimes called graph-enhanced RAG, uses both: vector search finds the passages or entities most related to the question, then the graph is used to pull in their connected facts, and both go into the prompt. Graph nodes can also link back to the source passages that mention them, so answers stay cited.

Knowledge graphs take real effort to build and maintain. They pay off when questions are about relationships between many entities (people, roles, courses, products, regulations), and less so when questions are about the content of individual passages, which vector search already handles well.

Follow your curiosity.

↑ ↓ to explore · Enter to open · Escape to close