MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / _keyword_search_nodes

Function _keyword_search_nodes

skills/wiki-store/scripts/wiki_db.py:522–578  ·  view source on GitHub ↗
(
    user_id: str,
    query_text: str,
    node_type: Optional[str],
    limit: int,
)

Source from the content-addressed store, hash-verified

520
521
522def _keyword_search_nodes(
523 user_id: str,
524 query_text: str,
525 node_type: Optional[str],
526 limit: int,
527) -> List[Dict[str, Any]]:
528 conn = db_ops.get_connection()
529 cursor = conn.cursor()
530 params: List[Any] = [_fts_query(query_text), user_id]
531 type_filter = ""
532 if node_type:
533 type_filter = " AND n.node_type = ?"
534 params.append(node_type)
535 params.append(max(1, int(limit)))
536 try:
537 rows = cursor.execute(
538 f"""
539 SELECT n.*, bm25(wiki_nodes_fts) AS search_rank
540 FROM wiki_nodes_fts
541 JOIN wiki_nodes n ON n.id = wiki_nodes_fts.rowid
542 WHERE wiki_nodes_fts MATCH ?
543 AND n.user_id = ?
544 {type_filter}
545 ORDER BY search_rank, n.updated_at DESC
546 LIMIT ?
547 """,
548 params,
549 ).fetchall()
550 conn.close()
551 nodes = _rows_to_nodes(rows)
552 for index, node in enumerate(nodes):
553 node["keyword_score"] = 1.0 - (index / max(1, len(nodes)))
554 return nodes
555 except sqlite3.OperationalError:
556 like = f"%{query_text}%"
557 params = [user_id, like, like, like]
558 type_filter = ""
559 if node_type:
560 type_filter = " AND node_type = ?"
561 params.append(node_type)
562 params.append(max(1, int(limit)))
563 rows = cursor.execute(
564 f"""
565 SELECT * FROM wiki_nodes
566 WHERE user_id = ?
567 AND (title LIKE ? OR body LIKE ? OR keywords LIKE ?)
568 {type_filter}
569 ORDER BY updated_at DESC, id DESC
570 LIMIT ?
571 """,
572 params,
573 ).fetchall()
574 conn.close()
575 nodes = _rows_to_nodes(rows)
576 for index, node in enumerate(nodes):
577 node["keyword_score"] = 1.0 - (index / max(1, len(nodes)))
578 return nodes
579

Callers 1

search_nodesFunction · 0.85

Calls 6

_fts_queryFunction · 0.85
_rows_to_nodesFunction · 0.85
cursorMethod · 0.80
fetchallMethod · 0.45
executeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected