Add middle-content matching for GraphViewer

This commit is contained in:
yangdx
2025-04-09 18:33:56 +08:00
parent 7cd9859e08
commit 2e3e1fedcc
2 changed files with 53 additions and 2 deletions

View File

@@ -46,8 +46,30 @@ const GraphLabels = () => {
let result: string[] = labels let result: string[] = labels
if (query) { if (query) {
// Search labels // Search labels using MiniSearch
result = searchEngine.search(query).map((r: { id: number }) => labels[r.id]) 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 return result.length <= labelListLimit

View File

@@ -123,13 +123,42 @@ export const GraphSearchInput = ({
} }
// If has query, search nodes and verify they still exist // 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)) .filter((r: { id: string }) => graph.hasNode(r.id))
.map((r: { id: string }) => ({ .map((r: { id: string }) => ({
id: r.id, id: r.id,
type: 'nodes' 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 // prettier-ignore
return result.length <= searchResultLimit return result.length <= searchResultLimit
? result ? result