* Find nodes whose name contains a substring (LIKE-based). * Useful for CamelCase-part matching where FTS fails because * e.g. "TransportSearchAction" is one FTS token, not matchable by "Search"*. * * Results are ordered by name length (shorter = more likely to be the core type).
(
substring: string,
options: SearchOptions & { excludePrefix?: boolean } = {}
)
| 2524 | * Results are ordered by name length (shorter = more likely to be the core type). |
| 2525 | */ |
| 2526 | findNodesByNameSubstring( |
| 2527 | substring: string, |
| 2528 | options: SearchOptions & { excludePrefix?: boolean } = {} |
| 2529 | ): SearchResult[] { |
| 2530 | const { kinds, languages, limit = 30, excludePrefix } = options; |
| 2531 | |
| 2532 | let sql = ` |
| 2533 | SELECT nodes.*, 1.0 as score |
| 2534 | FROM nodes |
| 2535 | WHERE name LIKE ? |
| 2536 | `; |
| 2537 | const params: (string | number)[] = [`%${substring}%`]; |
| 2538 | |
| 2539 | // Exclude prefix matches (handled by FTS-based prefix search in Step 2b) |
| 2540 | if (excludePrefix) { |
| 2541 | sql += ` AND name NOT LIKE ?`; |
| 2542 | params.push(`${substring}%`); |
| 2543 | } |
| 2544 | |
| 2545 | if (kinds && kinds.length > 0) { |
| 2546 | sql += ` AND kind IN (${kinds.map(() => '?').join(',')})`; |
| 2547 | params.push(...kinds); |
| 2548 | } |
| 2549 | |
| 2550 | if (languages && languages.length > 0) { |
| 2551 | sql += ` AND language IN (${languages.map(() => '?').join(',')})`; |
| 2552 | params.push(...languages); |
| 2553 | } |
| 2554 | |
| 2555 | sql += ' ORDER BY length(name) ASC LIMIT ?'; |
| 2556 | params.push(limit); |
| 2557 | |
| 2558 | // Explore asks this four times for every word of its query, and the |
| 2559 | // leading `%` makes each one a scan of every node of those kinds (#2620). |
| 2560 | // The in-memory index answers with the same rows in the same order: ties |
| 2561 | // in length come out in the order SQLite's plan for this very statement |
| 2562 | // reads rows, the kind index's or the table's. Anything it can't answer |
| 2563 | // exactly (no kind filter, a language filter, a `%` in the word, another |
| 2564 | // plan, a transaction) is asked of SQLite. |
| 2565 | if (kinds && kinds.length > 0 && !(languages && languages.length > 0)) { |
| 2566 | const index = this.memoizedRead('nameSubstringIndex', () => this.readNameSubstringIndex()); |
| 2567 | const order = index ? this.substringScanOrder(index, sql, params, excludePrefix ? 2 : 1) : null; |
| 2568 | const rowids = order ? searchNameSubstring(index!.index, substring, kinds, excludePrefix === true, limit, order) : null; |
| 2569 | if (rowids) { |
| 2570 | return this.getNodeRowsByRowid(rowids).map((row) => ({ node: rowToNode(row), score: 1.0 })); |
| 2571 | } |
| 2572 | } |
| 2573 | |
| 2574 | const rows = this.db.prepare(sql).all(...params) as (NodeRow & { score: number })[]; |
| 2575 | return rows.map((row) => ({ |
| 2576 | node: rowToNode(row), |
| 2577 | score: row.score, |
| 2578 | })); |
| 2579 | } |
| 2580 | |
| 2581 | /** |
| 2582 | * The order SQLite's plan for a substring lookup reads candidate rows in, |