Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions backend/app/rag/graph_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ def _match_query_nodes(graph: nx.Graph, query: str) -> Set[str]:
return matched

query_text = query.casefold()
for node_id, data in graph.nodes(data=True):
name = data.get("name", "").casefold()
if name and name in query_text:

# Check if the inverted index is already built and cached on the graph metadata
if "_name_to_node_id" not in graph.graph:
name_index = {}
for node_id, data in graph.nodes(data=True):
name = data.get("name", "")
if name:
name_index[name.casefold()] = node_id
graph.graph["_name_to_node_id"] = name_index
else:
name_index = graph.graph["_name_to_node_id"]

# O(M) lookup over unique names instead of scanning full node data arrays
for name, node_id in name_index.items():
if name in query_text:
matched.add(node_id)

return matched


def _format_pages(pages: List[int]) -> str:
if not pages:
return "unknown pages"
Expand Down
Loading