use super::filters::SearchFilters;
use crate::core::index::Connection;
use crate::core::text::{excerpt, fts_segment, is_searchable_query, query_terms, SNIPPET_CHARS};
use anyhow::Result;
use std::path::{Path, PathBuf};
pub const MIN_QUERY_LEN: usize = 3;
#[derive(Debug, Clone)]
pub struct RawHit {
pub chunk_id: i64,
pub path: String,
pub page: i64,
pub line_start: i64,
pub line_end: i64,
pub anchor: Option<String>,
pub snippet: String,
pub score: f32,
pub source: HitSource,
pub mtime: i64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HitSource {
Keyword,
Semantic,
}
const STOPWORDS: &[&str] = &[
"a", "an", "and", "are", "as", "at", "be", "by", "can", "could", "did", "do", "does", "for",
"from", "had", "has", "have", "how", "i", "in", "is", "it", "its", "me", "my", "of", "on",
"or", "our", "please", "should", "show", "tell", "that", "the", "their", "there", "these",
"this", "those", "to", "was", "we", "were", "what", "when", "where", "which", "who", "will",
"with", "would", "you", "your",
];
pub fn fts5_match_expr(query: &str) -> String {
let segmented = fts_segment(query);
let terms: Vec<String> = segmented
.split_whitespace()
.filter(|term| !STOPWORDS.contains(&term.to_lowercase().as_str()))
.map(|term| format!("\"{}\"", term.replace('"', "\"\"")))
.collect();
if terms.is_empty() {
return segmented
.split_whitespace()
.map(|term| format!("\"{}\"", term.replace('"', "\"\"")))
.collect::<Vec<_>>()
.join(" OR ");
}
terms.join(" OR ")
}
fn root_filter(roots: &[PathBuf]) -> (String, Vec<String>) {
if roots.is_empty() {
return (String::new(), vec![]);
}
let mut clauses = Vec::with_capacity(roots.len());
let mut params = Vec::with_capacity(roots.len());
for root in roots {
let mut prefix = root.to_string_lossy().to_string();
if !prefix.ends_with(std::path::MAIN_SEPARATOR) {
prefix.push(std::path::MAIN_SEPARATOR);
}
clauses.push("(chunks.path = ? OR chunks.path LIKE ?)");
params.push(root.to_string_lossy().to_string());
params.push(format!("{}%", prefix));
}
(format!(" AND ({})", clauses.join(" OR ")), params)
}
pub fn tier0_search(
conn: &Connection,
query: &str,
roots: &[PathBuf],
limit: usize,
) -> Result<Vec<RawHit>> {
tier0_search_filtered(conn, query, roots, &SearchFilters::default(), limit)
}
pub fn tier0_search_filtered(
conn: &Connection,
query: &str,
roots: &[PathBuf],
filters: &SearchFilters,
limit: usize,
) -> Result<Vec<RawHit>> {
if !is_searchable_query(query) {
return Ok(vec![]);
}
let match_expr = fts5_match_expr(query);
if match_expr.is_empty() {
return Ok(vec![]);
}
let (roots_sql, root_params) = root_filter(roots);
let (filters_sql, filter_values) = filters.sql();
let sql = format!(
"SELECT chunks.id, chunks.path, chunks.page, chunks.line_start, chunks.line_end,
chunks.anchor, chunks.mtime,
chunks.content,
bm25(chunks_fts) AS rank
FROM chunks_fts
JOIN chunks ON chunks.id = chunks_fts.rowid
WHERE chunks_fts MATCH ?{roots_sql}{filters_sql}
ORDER BY rank
LIMIT ?"
);
let mut params: Vec<rusqlite::types::Value> = vec![rusqlite::types::Value::Text(match_expr)];
params.extend(root_params.into_iter().map(rusqlite::types::Value::Text));
params.extend(filter_values);
params.push(rusqlite::types::Value::Integer(limit as i64));
let refs: Vec<&dyn rusqlite::ToSql> =
params.iter().map(|p| p as &dyn rusqlite::ToSql).collect();
let mut stmt = conn.inner().prepare(&sql)?;
let rows = stmt.query_map(refs.as_slice(), |row| {
Ok((
row.get::<_, i64>(0)?,
row.get::<_, String>(1)?,
row.get::<_, i64>(2)?,
row.get::<_, i64>(3)?,
row.get::<_, i64>(4)?,
row.get::<_, Option<String>>(5)?,
row.get::<_, i64>(6)?,
row.get::<_, String>(7)?,
row.get::<_, f64>(8)?,
))
})?;
let mut hits = Vec::new();
let mut best: f32 = 0.0;
let terms = query_terms(query);
for row in rows {
let (id, path, page, ls, le, anchor, mtime, content, bm25) = row?;
let relevance = (-bm25 as f32).max(0.0);
best = best.max(relevance);
hits.push(RawHit {
chunk_id: id,
path,
page,
line_start: ls,
line_end: le,
anchor,
snippet: excerpt(&content, &terms, SNIPPET_CHARS),
score: relevance,
source: HitSource::Keyword,
mtime,
});
}
if best > 0.0 {
for hit in &mut hits {
hit.score /= best;
}
}
Ok(hits)
}
#[cfg(target_os = "macos")]
pub fn spotlight_paths(query: &str, roots: &[PathBuf], limit: usize) -> Result<Vec<PathBuf>> {
use std::process::Command;
if !is_searchable_query(query) {
return Ok(vec![]);
}
let escaped = query.replace('"', "\\\"");
let predicate = format!("kMDItemTextContent == \"*{}*\"cd", escaped);
let roots: Vec<PathBuf> = if roots.is_empty() {
vec![std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/"))]
} else {
roots.to_vec()
};
let mut out = Vec::new();
for root in roots {
let output = Command::new("mdfind")
.arg("-onlyin")
.arg(&root)
.arg(&predicate)
.output();
let Ok(output) = output else { continue };
for line in String::from_utf8_lossy(&output.stdout).lines() {
let p = PathBuf::from(line);
if p.is_file() {
out.push(p);
}
if out.len() >= limit {
return Ok(out);
}
}
}
Ok(out)
}
#[cfg(not(target_os = "macos"))]
pub fn spotlight_paths(_query: &str, _roots: &[PathBuf], _limit: usize) -> Result<Vec<PathBuf>> {
Ok(vec![])
}
pub fn within_roots(path: &Path, roots: &[PathBuf]) -> bool {
roots.is_empty() || roots.iter().any(|r| path.starts_with(r))
}