Searching the Graph

How to retrieve information from your Graphiti graph

The examples below demonstrate two search approaches in the Graphiti library:

  1. Hybrid Search:

    1await graphiti.search(query)

    Combines semantic similarity and BM25 retrieval, reranked using Reciprocal Rank Fusion.

    Example: Does a broad retrieval of facts related to Allbirds Wool Runners and Jane’s purchase.

  2. Node Distance Reranking:

    1await client.search(query, focal_node_uuid)

    Extends Hybrid Search above by prioritizing results based on proximity to a specified node in the graph.

    Example: Focuses on Jane-specific information, highlighting her wool allergy.

Node Distance Reranking is particularly useful for entity-specific queries, providing more contextually relevant results. It weights facts by their closeness to the focal node, emphasizing information directly related to the entity of interest.

This dual approach allows for both broad exploration and targeted, entity-specific information retrieval from the knowledge graph.

1query = "Can Jane wear Allbirds Wool Runners?"
2jane_node_uuid = "123e4567-e89b-12d3-a456-426614174000"
3
4def print_facts(edges):
5 print("\n".join([edge.fact for edge in edges]))
6
7# Hybrid Search
8results = await graphiti.search(query)
9print_facts(results)
10
11> The Allbirds Wool Runners are sold by Allbirds.
12> Men's SuperLight Wool Runners - Dark Grey (Medium Grey Sole) has a runner silhouette.
13> Jane purchased SuperLight Wool Runners.
14
15# Hybrid Search with Node Distance Reranking
16await client.search(query, jane_node_uuid)
17print_facts(results)
18
19> Jane purchased SuperLight Wool Runners.
20> Jane is allergic to wool.
21> The Allbirds Wool Runners are sold by Allbirds.