srcsearch
srcsearch is a lightweight search engine for source code and project documentation. It indexes Rust source files and Markdown content, then lets developers query it using Tantivy-powered full-text search with BM25 ranking.
It can be used in two ways:
- CLI (
srcsearch) for local workflows and scripting. - Library (
srcsearch) for embedding indexing/search in your own Rust tooling.
CLI usage
The crate provides a binary named srcsearch with these subcommands:
json— build a JSON outputindex— build a Tantivy index directoryupdate— incrementally update an existing Tantivy index for changed filessearch— query a Tantivy index
Build and run
1) Generate a JSON output
Short form:
2) Build a Tantivy index directory
Short form:
--output-dirmust be empty (or not exist yet) when creating a fresh index.
3) Update an existing index after file changes
Short form:
4) Search the index
Search all fields (default scope):
Restrict search to documentation-focused fields only:
JSON output:
Explain top scores:
--explain asks Tantivy for score explanations for the top three returned hits. The flag is most useful with --json, where each result includes the regular hit payload plus an explanation string. Hits after the top three have null explanations so large result sets stay compact.
Example JSON result payload:
Search scopes
all(default): query title/body text + Rust symbol/signature/doc/code fieldsdoc: query title/body text + Rust doc fields only (ignores signatures/code)
Notes:
- Queries run against
title,body_text, and Rustdocfields use stemming, so inflected forms (for examplerunningvsrun) may match.
Convenience scripts
The repository includes a few helper scripts under scripts/ for common local workflows. They all assume the index directory is .srcsearch at the project root.
scripts/srcindex— create a fresh.srcsearchindex from the current project.scripts/srcreindex— remove.srcsearchand rebuild it from scratch.scripts/srcquery "<query>"— run a regular search (--scope all) against.srcsearch(optionally add--json).scripts/srcdoc "<query>"— run a docs-focused search (--scope doc) against.srcsearch(optionally add--json).scripts/summarize.py— readsrcsearch search --json --explainoutput from stdin and print a compact per-hit score breakdown.
Typical usage:
|
scripts/summarize.py is intended for ranking diagnostics. It extracts each explained scoring clause and prints the query term, matched field, contributed score, percentage of the hit score, boost, unboosted base score, term frequency (freq), inverse document frequency (idf), document length (dl), average document length (avgdl), and matching document count (n). Rows are separated by ----------------- between hits. A typical summary looks like:
src/lib.rs:331:1: index_project (score: 8.009)
term field score percent boost base freq idf dl avgdl n
index signature 3.015 37.6% 2.0 1.507 1.0 1.881 9.0 5.605 12.0
index doc 2.717 33.9% 2.0 1.358 1.0 2.267 8.0 3.037 8.0
index code 2.277 28.4% 1.0 2.277 2.0 1.293 24.0 108.667 22.0
-----------------
src/lib.rs:542:1: update_tantivy_index (score: 7.644)
term field score percent boost base freq idf dl avgdl n
index signature 3.291 43.1% 2.0 1.645 2.0 1.881 17.0 5.605 12.0
index doc 1.736 22.7% 2.0 0.868 1.0 2.267 15.0 3.037 8.0
index code 2.617 34.2% 1.0 2.617 30.0 1.293 280.0 108.667 22.0
-----------------
src/lib.rs:657:1: register_doc_text_analyzer (score: 7.196)
term field score percent boost base freq idf dl avgdl n
index signature 4.834 67.2% 2.0 2.417 2.0 1.881 7.0 5.605 12.0
index code 2.361 32.8% 1.0 2.361 3.0 1.293 38.0 108.667 22.0
-----------------
Library usage
Add srcsearch from crates.io:
[]
= "0.1"
If you are working from a local checkout instead, you can use a path dependency:
= { = "../srcsearch" }
Build records from a project (or a single target)
use Path;
use ;
Write JSON or Tantivy index
use Path;
use ;
Incremental update
use Path;
use ;
Search from code
use Path;
use ;
Development