[BUG] [PERF] FalkorDB: edge_fulltext_search causes full graph scan due to re-MATCH pattern instead of using startNode/endNode
Description
The edge_fulltext_search method in FalkorSearchOperations uses a MATCH (n:Entity)-[e:RELATES_TO {uuid: rel.uuid}]->(m:Entity) pattern to retrieve source and target nodes after a fulltext search on relationships. This causes FalkorDB to perform a full scan of all RELATES_TO edges for each fulltext result, leading to O(n×m) complexity and query timeouts on moderately-sized graphs.
The fulltext procedure already returns the relationship object — startNode(rel) and endNode(rel) can be used to directly access the connected nodes without any additional scan.
Environment Graphiti version: latest (main branch) Database: FalkorDB v4.x (Docker) Graph size: ~5,169 RELATES_TO edges, ~1,500 Entity nodes Reproduction Run a fulltext edge search on any graph with >1,000 RELATES_TO edges:
python from graphiti_core import Graphiti results = await graphiti.search("API test system", group_ids=["my_group"]) The generated Cypher is:
cypher CALL db.idx.fulltext.queryRelationships('RELATES_TO', '(@group_id:"my_group") (API | test | system)') YIELD relationship AS rel, score MATCH (n:Entity)-[e:RELATES_TO {uuid: rel.uuid}]->(m:Entity) -- ← full scan here WHERE e.group_id IN ["my_group"] WITH e, score, n, m RETURN ... ORDER BY score DESC LIMIT 20 GRAPH.EXPLAIN confirms the scan:
Results Aggregate Filter Edge By Index Scan | [e:RELATES_TO] ← scans ALL edges Node By Label Scan | (n:Entity) ← scans ALL nodes ProcedureCall Benchmark Results Query Pattern Internal Time Rows Fulltext only (count) 2ms 1,492 Fulltext + MATCH {uuid: rel.uuid} (412 FT results) 26.7s 412 Full query (1,492 FT results + MATCH + WHERE + ORDER) 118.4s 20 Root Cause In graphiti_core/driver/falkordb/operations/search_ops.py , the edge_fulltext_search method (and edge_bfs_search) re-MATCHes the relationship by UUID instead of using the relationship object directly returned by the fulltext procedure:
python
Current (slow) - line ~306
MATCH (n:Entity)-[e:RELATES_TO {uuid: rel.uuid}]->(m:Entity) Suggested Fix Replace the re-MATCH with direct endpoint access:
diff
- YIELD relationship AS rel, score
- MATCH (n:Entity)-[e:RELATES_TO {uuid: rel.uuid}]->(m:Entity)
- YIELD relationship AS e, score
- WITH e, score, startNode(e) AS n, endNode(e) AS m This transforms the query from O(n×m) to O(n), eliminating the full graph scan entirely.
The same pattern also appears in
edge_bfs_search (line ~418).
Impact Queries timeout on graphs with ~5,000+ RELATES_TO edges The default FalkorDB timeout (120s) is exceeded by the full query Adding a Range index on RELATES_TO.uuid does not help — FalkorDB's query planner doesn't use it for this join pattern
Source: getzep/graphiti