MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / findNodesByNameSubstring

Method findNodesByNameSubstring

src/db/queries.ts:2510–2563  ·  view source on GitHub ↗

* 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 } = {}
  )

Source from the content-addressed store, hash-verified

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

Callers 4

findRelevantContextMethod · 0.80
expectSameRowsFunction · 0.80

Calls 11

memoizedReadMethod · 0.95
substringScanOrderMethod · 0.95
getNodeRowsByRowidMethod · 0.95
searchNameSubstringFunction · 0.90
rowToNodeFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
allMethod · 0.65
prepareMethod · 0.65
joinMethod · 0.45

Tested by 1

expectSameRowsFunction · 0.64