diff --git a/lightrag_webui/src/components/graph/GraphLabels.tsx b/lightrag_webui/src/components/graph/GraphLabels.tsx index a37bd24a..2b1e3cd7 100644 --- a/lightrag_webui/src/components/graph/GraphLabels.tsx +++ b/lightrag_webui/src/components/graph/GraphLabels.tsx @@ -46,8 +46,30 @@ const GraphLabels = () => { let result: string[] = labels if (query) { - // Search labels + // Search labels using MiniSearch result = searchEngine.search(query).map((r: { id: number }) => labels[r.id]) + + // Add middle-content matching if results are few + // This enables matching content in the middle of text, not just from the beginning + if (result.length < 5) { + // Get already matched labels to avoid duplicates + const matchedLabels = new Set(result) + + // Perform middle-content matching on all labels + const middleMatchResults = labels.filter(label => { + // Skip already matched labels + if (matchedLabels.has(label)) return false + + // Match if label contains query string but doesn't start with it + return label && + typeof label === 'string' && + !label.toLowerCase().startsWith(query.toLowerCase()) && + label.toLowerCase().includes(query.toLowerCase()) + }) + + // Merge results + result = [...result, ...middleMatchResults] + } } return result.length <= labelListLimit diff --git a/lightrag_webui/src/components/graph/GraphSearch.tsx b/lightrag_webui/src/components/graph/GraphSearch.tsx index 51e76a0b..40840f59 100644 --- a/lightrag_webui/src/components/graph/GraphSearch.tsx +++ b/lightrag_webui/src/components/graph/GraphSearch.tsx @@ -123,13 +123,42 @@ export const GraphSearchInput = ({ } // If has query, search nodes and verify they still exist - const result: OptionItem[] = searchEngine.search(query) + let result: OptionItem[] = searchEngine.search(query) .filter((r: { id: string }) => graph.hasNode(r.id)) .map((r: { id: string }) => ({ id: r.id, type: 'nodes' })) + // Add middle-content matching if results are few + // This enables matching content in the middle of text, not just from the beginning + if (result.length < 5) { + // Get already matched IDs to avoid duplicates + const matchedIds = new Set(result.map(item => item.id)) + + // Perform middle-content matching on all nodes + const middleMatchResults = graph.nodes() + .filter(id => { + // Skip already matched nodes + if (matchedIds.has(id)) return false + + // Get node label + const label = graph.getNodeAttribute(id, 'label') + // Match if label contains query string but doesn't start with it + return label && + typeof label === 'string' && + !label.toLowerCase().startsWith(query.toLowerCase()) && + label.toLowerCase().includes(query.toLowerCase()) + }) + .map(id => ({ + id, + type: 'nodes' as const + })) + + // Merge results + result = [...result, ...middleMatchResults] + } + // prettier-ignore return result.length <= searchResultLimit ? result