@@ -609,7 +609,7 @@ async function doSearch(q) {
609609 currentData = await res . json ( ) ;
610610 renderResults ( currentData ) ;
611611 // Load related concepts
612- loadRelated ( q ) ;
612+ loadRelated ( currentData , q ) ;
613613 // Refresh trending after search (new query logged)
614614 setTimeout ( ( ) => loadTrending ( ) , 500 ) ;
615615 // Show concept subgraph for the search term
@@ -620,10 +620,29 @@ async function doSearch(q) {
620620}
621621
622622// ── Related Concepts ──────────────────────────────────────
623- async function loadRelated ( q ) {
623+ async function loadRelated ( searchData , q ) {
624624 try {
625- const res = await fetch ( `${ API } /api/related?q=${ encodeURIComponent ( q ) } &limit=10` ) ;
626- const data = await res . json ( ) ;
625+ const conceptCounts = new Map ( ) ;
626+ const groups = Array . isArray ( searchData ?. groups ) ? searchData . groups : [ ] ;
627+ for ( const item of groups ) {
628+ const concepts = Array . isArray ( item ?. matched_concepts ) ? item . matched_concepts : [ ] ;
629+ for ( const concept of concepts ) {
630+ const term = String ( concept || '' ) . trim ( ) ;
631+ if ( ! term || term === q || term . includes ( q ) || q . includes ( term ) ) continue ;
632+ conceptCounts . set ( term , ( conceptCounts . get ( term ) || 0 ) + 1 ) ;
633+ }
634+ }
635+
636+ let data = Array . from ( conceptCounts . entries ( ) )
637+ . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] || a [ 0 ] . length - b [ 0 ] . length )
638+ . slice ( 0 , 10 )
639+ . map ( ( [ term , count ] ) => ( { term, count } ) ) ;
640+
641+ if ( data . length === 0 ) {
642+ const res = await fetch ( `${ API } /api/related?q=${ encodeURIComponent ( q ) } &limit=10` ) ;
643+ data = await res . json ( ) ;
644+ }
645+
627646 if ( data . length > 0 ) {
628647 relatedBarEl . innerHTML = `
629648 <span class="related-label">🔗 相关概念:</span>
0 commit comments