@@ -19,12 +19,32 @@ const useGraphData = (
1919 includeDocuments : boolean ,
2020 includeTextUnits : boolean ,
2121 includeCommunities : boolean ,
22- includeCovariates : boolean
22+ includeCovariates : boolean ,
23+ maxEntities : number = 0 // 0 means no limit
2324) => {
2425
2526 const [ graphData , setGraphData ] = useState < CustomGraphData > ( { nodes : [ ] , links : [ ] } ) ;
2627 useEffect ( ( ) => {
27- const nodes : CustomNode [ ] = entities . map ( ( entity ) => ( {
28+ // Calculate relationship count for each entity to determine top N
29+ const entityRelationshipCount : { [ key : string ] : number } = { } ;
30+ relationships . forEach ( ( rel ) => {
31+ entityRelationshipCount [ rel . source ] = ( entityRelationshipCount [ rel . source ] || 0 ) + 1 ;
32+ entityRelationshipCount [ rel . target ] = ( entityRelationshipCount [ rel . target ] || 0 ) + 1 ;
33+ } ) ;
34+
35+ // Sort entities by relationship count and limit to maxEntities if specified
36+ let filteredEntities = entities ;
37+ if ( maxEntities > 0 && entities . length > maxEntities ) {
38+ filteredEntities = [ ...entities ]
39+ . sort ( ( a , b ) => {
40+ const countA = entityRelationshipCount [ a . title ] || 0 ;
41+ const countB = entityRelationshipCount [ b . title ] || 0 ;
42+ return countB - countA ; // Sort descending
43+ } )
44+ . slice ( 0 , maxEntities ) ;
45+ }
46+
47+ const nodes : CustomNode [ ] = filteredEntities . map ( ( entity ) => ( {
2848 uuid : entity . id ,
2949 id : entity . title , // use title as id because relationships use title as source/target
3050 name : entity . title , // legacy field for old GraphRAG 0.2.x - 0.3.x
@@ -111,13 +131,17 @@ const useGraphData = (
111131 const textUnitEntityLinks = textunits
112132 . filter ( ( textunit ) => ( textunit . entity_ids ?? [ ] ) . length > 0 )
113133 . flatMap ( ( textunit ) =>
114- textunit . entity_ids . map ( ( entityId ) => ( {
115- source : textunit . id ,
116- target : nodes . find ( ( e ) => e . uuid === entityId ) ?. name || "" ,
117- type : "HAS_ENTITY" ,
118- id : `${ textunit . id } -${ entityId } ` ,
119- } ) )
120- ) ;
134+ textunit . entity_ids . map ( ( entityId ) => {
135+ const targetName = nodes . find ( ( e ) => e . uuid === entityId ) ?. name ;
136+ return targetName ? {
137+ source : textunit . id ,
138+ target : targetName ,
139+ type : "HAS_ENTITY" ,
140+ id : `${ textunit . id } -${ entityId } ` ,
141+ } : null ;
142+ } )
143+ )
144+ . filter ( ( link ) : link is NonNullable < typeof link > => link !== null ) ;
121145
122146 links . push ( ...textUnitEntityLinks ) ;
123147 }
@@ -160,7 +184,8 @@ const useGraphData = (
160184
161185 const newLinks = [ ] ;
162186
163- if ( ! uniqueLinks . has ( sourceLinkId ) ) {
187+ // Only add link if source node exists
188+ if ( ! uniqueLinks . has ( sourceLinkId ) && nodesMap [ relationship . source ] ) {
164189 uniqueLinks . add ( sourceLinkId ) ;
165190 newLinks . push ( {
166191 source : relationship . source ,
@@ -170,7 +195,8 @@ const useGraphData = (
170195 } ) ;
171196 }
172197
173- if ( ! uniqueLinks . has ( targetLinkId ) ) {
198+ // Only add link if target node exists
199+ if ( ! uniqueLinks . has ( targetLinkId ) && nodesMap [ relationship . target ] ) {
174200 uniqueLinks . add ( targetLinkId ) ;
175201 newLinks . push ( {
176202 source : relationship . target ,
@@ -242,12 +268,14 @@ const useGraphData = (
242268 covariateNodes . forEach ( node => nodesMap [ node . id ] = node ) ;
243269 nodes . push ( ...covariateNodes ) ;
244270
245- const covariateTextUnitLinks = covariates . map ( ( covariate ) => ( {
246- source : covariate . text_unit_id ,
247- target : covariate . id ,
248- type : "HAS_COVARIATE" ,
249- id : `${ covariate . text_unit_id } -${ covariate . id } ` ,
250- } ) ) ;
271+ const covariateTextUnitLinks = covariates
272+ . filter ( ( covariate ) => nodesMap [ covariate . text_unit_id ] && nodesMap [ covariate . id ] )
273+ . map ( ( covariate ) => ( {
274+ source : covariate . text_unit_id ,
275+ target : covariate . id ,
276+ type : "HAS_COVARIATE" ,
277+ id : `${ covariate . text_unit_id } -${ covariate . id } ` ,
278+ } ) ) ;
251279
252280 links . push ( ...covariateTextUnitLinks ) ;
253281 }
@@ -283,6 +311,7 @@ const useGraphData = (
283311 includeTextUnits ,
284312 includeCommunities ,
285313 includeCovariates ,
314+ maxEntities ,
286315 ] ) ;
287316
288317 return graphData ;
0 commit comments