fix(graphrag): return a chunk once from entity_pages

The subject flag was in the GROUP BY, so a chunk two matching entities link --
one the page is about, one merely mentioned in it -- came back as two
identical pages and spent the caller's page budget twice on the same text.
It is aggregated with bool_or now, which is what the ordering wanted anyway.

Covered by a live test against a pgvector-shaped documents table: the graph
tables alone cannot answer this query, so nothing exercised it before.
This commit is contained in:
Alex committed 2026-09-20 10:45:04 +01:00
1 parent ccd8eb612f
commit 2b6d4d509e
2 files changed
+69 -2

No files matched your search

+6 -2
View File
@@ -1265,13 +1265,17 @@ class GraphStore:
sql.SQL(
"""
SELECT d.{metadata}, d.{text},
(lower(n.name) = %s OR lower(n.name) LIKE %s) AS is_subject
bool_or(lower(n.name) = %s OR lower(n.name) LIKE %s) AS is_subject
FROM graph_node_chunks gc
JOIN graph_nodes n ON n.id = gc.node_id
JOIN {table} d ON d.id::text = gc.chunk_id
WHERE gc.source_id = %s AND d.{source} = %s
AND (lower(n.name) = %s OR lower(n.name) LIKE %s OR n.name ILIKE %s)
GROUP BY d.{metadata}, d.{text}, is_subject
-- One page per chunk. Grouping on the subject flag as well
-- split a chunk two entities link -- one naming it, one
-- merely mentioned -- into two identical pages, spending
-- the caller's page budget twice on the same text.
GROUP BY d.{metadata}, d.{text}
ORDER BY is_subject DESC, (d.{text} ILIKE %s) DESC
LIMIT %s;
"""