use crate::deadline::Deadline;
use crate::model::{Entry, Window, KIND_KNOWLEDGE};
use crate::store::Store;
use anyhow::Result;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use tantivy::collector::{Count, DocSetCollector, TopDocs};
use tantivy::query::{AllQuery, BooleanQuery, Occur, Query, QueryParser, RangeQuery, TermQuery};
use tantivy::schema::{
Field, IndexRecordOption, Schema, TextFieldIndexing, TextOptions, Value, FAST, INDEXED, STORED,
STRING,
};
use tantivy::tokenizer::{Language, LowerCaser, SimpleTokenizer, Stemmer, TextAnalyzer};
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
use std::sync::{Arc, Mutex, RwLock};
use tantivy::{Index, IndexReader, IndexWriter, Order, ReloadPolicy, TantivyDocument, Term};
const ANALYZER: &str = "ru_stem";
pub struct Fields {
doc_id: Field,
key: Field,
key_text: Field,
title: Field,
body: Field,
tags: Field,
tags_text: Field,
kind: Field,
service: Field,
status: Field,
severity: Field,
priority: Field,
assignee: Field,
parent_task: Field,
hosts: Field,
knowledge: Field,
blocked_reason: Field,
resolution: Field,
detection: Field,
affected: Field,
deadline: Field,
started_at: Field,
detected_at: Field,
mitigated_at: Field,
resolved_at: Field,
meta_text: Field,
sha: Field,
committed_at: Field,
seq: Field,
is_head: Field,
updated_at: Field,
}
type Tombstones = HashMap<String, Vec<(i64, u64)>>;
struct Generation {
searcher: tantivy::Searcher,
tombstones: Tombstones,
}
pub struct SearchIndex {
pub index: Index,
pub reader: IndexReader,
fields: Fields,
next_seq: AtomicU64,
pending_tombstones: Mutex<Tombstones>,
generation: RwLock<Arc<Generation>>,
}
#[derive(serde::Serialize)]
pub struct Hit {
pub key: String,
pub title: String,
pub kind: String,
pub body: String,
pub tags: Vec<String>,
#[serde(skip_serializing_if = "String::is_empty")]
pub service: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub hosts: Vec<String>,
#[serde(skip_serializing_if = "String::is_empty")]
pub severity: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub status: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub priority: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub blocked_reason: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub assignee: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub parent_task: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub knowledge: Vec<String>,
#[serde(skip_serializing_if = "String::is_empty")]
pub resolution: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub detection: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub affected: Vec<Window>,
#[serde(flatten)]
pub deadline: Deadline,
#[serde(skip_serializing_if = "String::is_empty")]
pub started_at: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub detected_at: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub mitigated_at: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub resolved_at: String,
pub sha: String,
pub committed_at: String,
pub is_head: bool,
pub updated_at: String,
pub score: f32,
}
#[derive(Default)]
pub struct SearchOpts {
pub tags: Vec<String>,
pub history: bool,
pub as_of: Option<i64>,
pub changed_between: Option<(i64, i64)>,
pub limit: usize,
pub recent: bool,
pub kind: Option<String>,
pub status: Option<String>,
pub service: Option<String>,
pub priority: Option<String>,
pub assignee: Option<String>,
pub parent_task: Option<String>,
}
fn text_opts() -> TextOptions {
TextOptions::default().set_indexing_options(
TextFieldIndexing::default()
.set_tokenizer(ANALYZER)
.set_index_option(IndexRecordOption::WithFreqsAndPositions),
)
}
fn build_schema() -> Schema {
let mut sb = Schema::builder();
sb.add_text_field("doc_id", STRING | STORED);
sb.add_text_field("key", STRING | STORED);
sb.add_text_field("key_text", text_opts());
sb.add_text_field("title", text_opts().set_stored());
sb.add_text_field("body", text_opts().set_stored());
sb.add_text_field("tags", STRING | STORED);
sb.add_text_field("tags_text", text_opts());
sb.add_text_field("kind", STRING | STORED);
sb.add_text_field("service", STRING | STORED);
sb.add_text_field("status", STRING | STORED);
sb.add_text_field("severity", STRING | STORED);
sb.add_text_field("priority", STRING | STORED);
sb.add_text_field("assignee", STRING | STORED);
sb.add_text_field("parent_task", STRING | STORED);
sb.add_text_field("hosts", STRING | STORED);
sb.add_text_field("knowledge", STRING | STORED);
sb.add_text_field("blocked_reason", TextOptions::default().set_stored());
sb.add_text_field("resolution", text_opts().set_stored());
sb.add_text_field("detection", text_opts().set_stored());
sb.add_text_field("affected", TextOptions::default().set_stored());
sb.add_text_field("deadline", TextOptions::default().set_stored());
sb.add_text_field("started_at", TextOptions::default().set_stored());
sb.add_text_field("detected_at", TextOptions::default().set_stored());
sb.add_text_field("mitigated_at", TextOptions::default().set_stored());
sb.add_text_field("resolved_at", TextOptions::default().set_stored());
sb.add_text_field("meta_text", text_opts());
sb.add_text_field("sha", STRING | STORED);
sb.add_i64_field("committed_at", INDEXED | STORED | FAST);
sb.add_u64_field("seq", STORED | FAST);
sb.add_u64_field("is_head", INDEXED | STORED | FAST);
sb.add_text_field("updated_at", TextOptions::default().set_stored());
sb.build()
}
fn head_doc_id(key: &str) -> String {
format!("head/{key}")
}
fn hist_doc_id(sha: &str, key: &str) -> String {
format!("{sha}/{key}")
}
fn iso(secs: i64) -> String {
chrono::DateTime::from_timestamp(secs, 0)
.map(|t| t.to_rfc3339())
.unwrap_or_default()
}
impl SearchIndex {
pub fn open_or_create(dir: &Path) -> Result<SearchIndex> {
std::fs::create_dir_all(dir)?;
let schema = build_schema();
let mmap = tantivy::directory::MmapDirectory::open(dir)?;
let index = match Index::open_or_create(mmap, schema.clone()) {
Ok(index) => index,
Err(err) => {
eprintln!("kyb: index schema mismatch ({err}), rebuilding from git");
std::fs::remove_dir_all(dir)?;
std::fs::create_dir_all(dir)?;
let mmap = tantivy::directory::MmapDirectory::open(dir)?;
Index::open_or_create(mmap, schema.clone())?
}
};
let analyzer = TextAnalyzer::builder(SimpleTokenizer::default())
.filter(LowerCaser)
.filter(Stemmer::new(Language::Russian))
.build();
index.tokenizers().register(ANALYZER, analyzer);
let f = |n: &str| schema.get_field(n).expect("field from our own schema");
let fields = Fields {
doc_id: f("doc_id"),
key: f("key"),
key_text: f("key_text"),
title: f("title"),
body: f("body"),
tags: f("tags"),
tags_text: f("tags_text"),
kind: f("kind"),
service: f("service"),
status: f("status"),
severity: f("severity"),
priority: f("priority"),
assignee: f("assignee"),
parent_task: f("parent_task"),
hosts: f("hosts"),
knowledge: f("knowledge"),
blocked_reason: f("blocked_reason"),
resolution: f("resolution"),
detection: f("detection"),
affected: f("affected"),
deadline: f("deadline"),
started_at: f("started_at"),
detected_at: f("detected_at"),
mitigated_at: f("mitigated_at"),
resolved_at: f("resolved_at"),
meta_text: f("meta_text"),
sha: f("sha"),
committed_at: f("committed_at"),
seq: f("seq"),
is_head: f("is_head"),
updated_at: f("updated_at"),
};
let reader = index.reader_builder().reload_policy(ReloadPolicy::Manual).try_into()?;
let generation = Generation { searcher: reader.searcher(), tombstones: HashMap::new() };
Ok(SearchIndex {
index,
reader,
fields,
next_seq: AtomicU64::new(1),
pending_tombstones: Mutex::new(HashMap::new()),
generation: RwLock::new(Arc::new(generation)),
})
}
pub fn writer(&self) -> Result<IndexWriter> {
Ok(self.index.writer(50_000_000)?)
}
fn make_doc(
&self,
e: &Entry,
sha: &str,
committed_at: i64,
is_head: bool,
seq: u64,
) -> TantivyDocument {
let mut d = TantivyDocument::default();
let id = if is_head { head_doc_id(&e.key) } else { hist_doc_id(sha, &e.key) };
d.add_text(self.fields.doc_id, id);
d.add_text(self.fields.key, &e.key);
d.add_text(self.fields.key_text, e.key.replace('-', " "));
d.add_text(self.fields.title, &e.title);
d.add_text(self.fields.body, &e.body);
for t in &e.tags {
d.add_text(self.fields.tags, t.to_lowercase());
}
d.add_text(self.fields.tags_text, e.tags.join(" ").replace('-', " "));
d.add_text(self.fields.kind, &e.kind);
if !e.service.is_empty() {
d.add_text(self.fields.service, e.service.to_lowercase());
}
if !e.status.is_empty() {
d.add_text(self.fields.status, &e.status);
}
if !e.severity.is_empty() {
d.add_text(self.fields.severity, &e.severity);
}
if !e.priority.is_empty() {
d.add_text(self.fields.priority, &e.priority);
}
if !e.blocked_reason.is_empty() {
d.add_text(self.fields.blocked_reason, &e.blocked_reason);
}
if !e.assignee.is_empty() {
d.add_text(self.fields.assignee, &e.assignee);
}
if !e.parent_task.is_empty() {
d.add_text(self.fields.parent_task, &e.parent_task);
}
for h in &e.hosts {
d.add_text(self.fields.hosts, h.to_lowercase());
}
for k in &e.knowledge {
d.add_text(self.fields.knowledge, k);
}
if !e.resolution.is_empty() {
d.add_text(self.fields.resolution, &e.resolution);
}
if !e.detection.is_empty() {
d.add_text(self.fields.detection, &e.detection);
}
if !e.affected.is_empty() {
let json = serde_json::to_string(&e.affected).expect("windows always serialize");
d.add_text(self.fields.affected, json);
}
if !e.deadline.is_empty() {
let json = serde_json::to_string(&e.deadline).expect("a deadline always serializes");
d.add_text(self.fields.deadline, json);
}
for (f, v) in [
(self.fields.started_at, &e.started_at),
(self.fields.detected_at, &e.detected_at),
(self.fields.mitigated_at, &e.mitigated_at),
(self.fields.resolved_at, &e.resolved_at),
] {
if !v.is_empty() {
d.add_text(f, v);
}
}
let meta = format!(
"{} {} {} {}",
e.service,
e.hosts.join(" "),
e.knowledge.join(" "),
e.deadline.waits_on
);
if !meta.trim().is_empty() {
d.add_text(self.fields.meta_text, meta);
}
d.add_text(self.fields.sha, sha);
d.add_i64(self.fields.committed_at, committed_at);
d.add_u64(self.fields.seq, seq);
d.add_u64(self.fields.is_head, is_head as u64);
d.add_text(self.fields.updated_at, &e.updated_at);
d
}
pub fn upsert_head(
&self,
w: &mut IndexWriter,
e: &Entry,
sha: &str,
committed_at: i64,
) -> Result<()> {
w.delete_term(Term::from_field_text(self.fields.doc_id, &head_doc_id(&e.key)));
let seq = self.next_seq.fetch_add(1, AtomicOrdering::SeqCst);
w.add_document(self.make_doc(e, sha, committed_at, true, seq))?;
w.add_document(self.make_doc(e, sha, committed_at, false, seq))?;
Ok(())
}
pub fn delete_head(&self, w: &mut IndexWriter, key: &str) {
w.delete_term(Term::from_field_text(self.fields.doc_id, &head_doc_id(key)));
}
pub fn retract(&self, key: &str, committed_at: i64) {
let seq = self.next_seq.fetch_add(1, AtomicOrdering::SeqCst);
self.pending_tombstones
.lock()
.unwrap()
.entry(key.to_string())
.or_default()
.push((committed_at, seq));
}
fn generation(&self) -> Arc<Generation> {
self.generation.read().unwrap().clone()
}
pub fn commit_and_reload(&self, w: &mut IndexWriter) -> Result<()> {
w.commit()?;
self.reader.reload()?;
let generation = Generation {
searcher: self.reader.searcher(),
tombstones: self.pending_tombstones.lock().unwrap().clone(),
};
*self.generation.write().unwrap() = Arc::new(generation);
Ok(())
}
pub fn reindex(&self, w: &mut IndexWriter, store: &Store) -> Result<(usize, usize)> {
w.delete_all_documents()?;
w.commit()?;
let (hist, retractions) = store.walk_history_and_retractions()?;
let mut seq = 0u64;
let mut last: HashMap<String, (Entry, String, i64, u64)> = HashMap::new();
for h in &hist {
seq += 1;
w.add_document(self.make_doc(&h.entry, &h.sha, h.committed_at, false, seq))?;
last.insert(h.entry.key.clone(), (h.entry.clone(), h.sha.clone(), h.committed_at, seq));
}
let mut tombstones: Tombstones = HashMap::new();
for r in retractions {
tombstones.entry(r.key).or_default().push((r.committed_at, r.after as u64));
}
let heads = store.list_head()?;
let mut live = 0usize;
let mut alive: HashSet<&str> = HashSet::new();
for e in &heads {
alive.insert(e.key.as_str());
let (sha, at, sq) = last
.get(&e.key)
.map(|(_, s, a, q)| (s.clone(), *a, *q))
.unwrap_or_default();
w.add_document(self.make_doc(e, &sha, at, true, sq))?;
live += 1;
}
for (key, (entry, sha, at, sq)) in &last {
if alive.contains(key.as_str()) || entry.kind == KIND_KNOWLEDGE {
continue;
}
w.add_document(self.make_doc(entry, sha, *at, true, *sq))?;
live += 1;
}
self.next_seq.store(seq + 1, AtomicOrdering::SeqCst);
*self.pending_tombstones.lock().unwrap() = tombstones;
self.commit_and_reload(w)?;
Ok((live, hist.len()))
}
pub fn get_live(&self, key: &str) -> Result<Option<Hit>> {
let searcher = self.reader.searcher();
let q = TermQuery::new(
Term::from_field_text(self.fields.doc_id, &head_doc_id(key)),
IndexRecordOption::Basic,
);
let top = searcher.search(&q, &TopDocs::with_limit(1))?;
match top.first() {
None => Ok(None),
Some((score, addr)) => Ok(Some(self.to_hit(&searcher, *addr, *score)?)),
}
}
pub fn search(&self, q: &str, opts: &SearchOpts) -> Result<Vec<Hit>> {
self.search_in(&self.generation(), q, opts)
}
fn search_in(&self, generation: &Generation, q: &str, opts: &SearchOpts) -> Result<Vec<Hit>> {
let searcher = generation.searcher.clone();
let base: Box<dyn Query> = if q.trim().is_empty() {
Box::new(AllQuery)
} else {
let mut parser = QueryParser::for_index(
&self.index,
vec![
self.fields.key_text,
self.fields.title,
self.fields.body,
self.fields.tags_text,
self.fields.meta_text,
self.fields.resolution,
self.fields.detection,
],
);
parser.set_field_boost(self.fields.title, 2.0);
parser.set_field_boost(self.fields.tags_text, 1.5);
parser.set_field_boost(self.fields.key_text, 1.5);
parser.set_field_boost(self.fields.meta_text, 1.5);
let (query, _errs) = parser.parse_query_lenient(q);
query
};
let mut clauses: Vec<(Occur, Box<dyn Query>)> = vec![(Occur::Must, base)];
let as_of = opts.as_of;
let window = opts.changed_between;
let versioned = opts.history || as_of.is_some() || window.is_some();
let head_val = if versioned { 0 } else { 1 };
clauses.push((
Occur::Must,
Box::new(TermQuery::new(
Term::from_field_u64(self.fields.is_head, head_val),
IndexRecordOption::Basic,
)),
));
if let Some(at) = as_of {
clauses.push((
Occur::Must,
Box::new(RangeQuery::new_i64(
"committed_at".to_string(),
i64::MIN..at.saturating_add(1),
)),
));
}
if let Some((lo, hi)) = window {
clauses.push((
Occur::Must,
Box::new(RangeQuery::new_i64(
"committed_at".to_string(),
lo..hi.saturating_add(1),
)),
));
}
for t in &opts.tags {
clauses.push((
Occur::Must,
Box::new(TermQuery::new(
Term::from_field_text(self.fields.tags, &t.to_lowercase()),
IndexRecordOption::Basic,
)),
));
}
let exact = [
(self.fields.kind, opts.kind.as_deref(), false),
(self.fields.status, opts.status.as_deref(), false),
(self.fields.service, opts.service.as_deref(), true),
(self.fields.priority, opts.priority.as_deref(), false),
(self.fields.assignee, opts.assignee.as_deref(), false),
(self.fields.parent_task, opts.parent_task.as_deref(), false),
];
for (field, value, fold) in exact {
let Some(v) = value.filter(|v| !v.is_empty()) else { continue };
let v = if fold { v.to_lowercase() } else { v.to_string() };
clauses.push((
Occur::Must,
Box::new(TermQuery::new(
Term::from_field_text(field, &v),
IndexRecordOption::Basic,
)),
));
}
let query = BooleanQuery::new(clauses);
let limit = opts.limit.max(1);
let collapse = as_of.is_some() || window.is_some();
let fetch = if collapse { searcher.search(&query, &Count)?.max(limit) } else { limit };
let top: Vec<(f32, tantivy::DocAddress)> = if opts.recent {
searcher
.search(
&query,
&TopDocs::with_limit(fetch).order_by_fast_field::<u64>("seq", Order::Desc),
)?
.into_iter()
.map(|(_seq, addr)| (0.0f32, addr))
.collect()
} else {
searcher.search(&query, &TopDocs::with_limit(fetch))?
};
let snapshot = match as_of {
Some(at) => Some(self.current_version_per_key(generation, at, window)?),
None => None,
};
let top = if collapse {
self.collapse_to_latest_per_key(&searcher, top, limit, snapshot.as_ref())?
} else {
top
};
let mut hits = vec![];
for (score, addr) in top {
hits.push(self.to_hit(&searcher, addr, score)?);
}
Ok(hits)
}
fn current_version_per_key(
&self,
generation: &Generation,
at: i64,
window: Option<(i64, i64)>,
) -> Result<HashMap<String, u64>> {
let searcher = &generation.searcher;
let mut clauses: Vec<(Occur, Box<dyn Query>)> = vec![
(
Occur::Must,
Box::new(TermQuery::new(
Term::from_field_u64(self.fields.is_head, 0),
IndexRecordOption::Basic,
)),
),
(
Occur::Must,
Box::new(RangeQuery::new_i64(
"committed_at".to_string(),
i64::MIN..at.saturating_add(1),
)),
),
];
if let Some((lo, hi)) = window {
clauses.push((
Occur::Must,
Box::new(RangeQuery::new_i64(
"committed_at".to_string(),
lo..hi.saturating_add(1),
)),
));
}
let addrs = searcher.search(&BooleanQuery::new(clauses), &DocSetCollector)?;
let mut current: HashMap<String, u64> = HashMap::new();
for addr in addrs {
let doc: TantivyDocument = searcher.doc(addr)?;
let key = doc.get_first(self.fields.key).and_then(|v| v.as_str()).unwrap_or_default();
let seq = doc.get_first(self.fields.seq).and_then(|v| v.as_u64()).unwrap_or(0);
match current.get_mut(key) {
Some(best) => *best = (*best).max(seq),
None => {
current.insert(key.to_string(), seq);
}
}
}
let tombstones = &generation.tombstones;
current.retain(|key, seq| {
!tombstones
.get(key)
.is_some_and(|ts| ts.iter().any(|(t_at, t_seq)| *t_at <= at && *t_seq >= *seq))
});
Ok(current)
}
fn collapse_to_latest_per_key(
&self,
searcher: &tantivy::Searcher,
top: Vec<(f32, tantivy::DocAddress)>,
limit: usize,
snapshot: Option<&HashMap<String, u64>>,
) -> Result<Vec<(f32, tantivy::DocAddress)>> {
let mut best: HashMap<String, (u64, usize)> = HashMap::new();
let mut rows: Vec<(f32, tantivy::DocAddress)> = Vec::with_capacity(top.len());
for (score, addr) in top {
let doc: TantivyDocument = searcher.doc(addr)?;
let key = doc
.get_first(self.fields.key)
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string();
let seq = doc.get_first(self.fields.seq).and_then(|v| v.as_u64()).unwrap_or(0);
if let Some(current) = snapshot {
if current.get(&key) == Some(&seq) {
rows.push((score, addr));
if rows.len() == limit {
break;
}
}
continue;
}
let idx = rows.len();
rows.push((score, addr));
match best.get(&key) {
Some((prev_seq, _)) if *prev_seq >= seq => {}
_ => {
best.insert(key, (seq, idx));
}
}
}
if snapshot.is_some() {
return Ok(rows);
}
let keep: HashSet<usize> = best.values().map(|(_, idx)| *idx).collect();
Ok(rows
.into_iter()
.enumerate()
.filter(|(idx, _)| keep.contains(idx))
.map(|(_, row)| row)
.take(limit)
.collect())
}
fn to_hit(
&self,
searcher: &tantivy::Searcher,
addr: tantivy::DocAddress,
score: f32,
) -> Result<Hit> {
let doc: TantivyDocument = searcher.doc(addr)?;
let text =
|f: Field| doc.get_first(f).and_then(|v| v.as_str()).unwrap_or_default().to_string();
let multi = |f: Field| -> Vec<String> {
doc.get_all(f).filter_map(|v| v.as_str()).map(String::from).collect()
};
Ok(Hit {
key: text(self.fields.key),
title: text(self.fields.title),
kind: text(self.fields.kind),
body: text(self.fields.body),
tags: multi(self.fields.tags),
service: text(self.fields.service),
hosts: multi(self.fields.hosts),
severity: text(self.fields.severity),
status: text(self.fields.status),
priority: text(self.fields.priority),
blocked_reason: text(self.fields.blocked_reason),
assignee: text(self.fields.assignee),
parent_task: text(self.fields.parent_task),
knowledge: multi(self.fields.knowledge),
resolution: text(self.fields.resolution),
detection: text(self.fields.detection),
affected: serde_json::from_str(&text(self.fields.affected)).unwrap_or_default(),
deadline: serde_json::from_str(&text(self.fields.deadline)).unwrap_or_default(),
started_at: text(self.fields.started_at),
detected_at: text(self.fields.detected_at),
mitigated_at: text(self.fields.mitigated_at),
resolved_at: text(self.fields.resolved_at),
sha: text(self.fields.sha),
committed_at: iso(
doc.get_first(self.fields.committed_at).and_then(|v| v.as_i64()).unwrap_or(0),
),
is_head: doc.get_first(self.fields.is_head).and_then(|v| v.as_u64()).unwrap_or(0) == 1,
updated_at: text(self.fields.updated_at),
score,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::Entry;
use rstest::rstest;
fn entry(key: &str, title: &str, body: &str) -> Entry {
Entry {
key: key.into(),
title: title.into(),
tags: vec!["infra".into()],
body: body.into(),
..Default::default()
}
}
fn mini(docs: &[(&str, &str, &str)]) -> (SearchIndex, tempfile::TempDir, tempfile::TempDir) {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
for (k, t, b) in docs {
store.upsert(entry(k, t, b), "2026-07-20").unwrap();
}
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
(index, data, idxd)
}
fn o(history: bool) -> SearchOpts {
SearchOpts { tags: vec![], history, limit: 10, ..Default::default() }
}
#[rstest]
#[case("стримах", "используем стримы всегда", true)]
#[case("стрим", "конфиг стримов переехал", true)]
#[case("стримом", "работаем со стримами", true)]
#[case("nats", "NATS живее всех живых", true)]
#[case("NATS", "пишем в nats постоянно", true)]
#[case("createorupdatestream", "вызываем CreateOrUpdateStream", true)]
#[case("терраформе", "весь конфиг в терраформ уехал", true)]
#[case("деплоя", "деплой только через пайплайн", false)]
#[case("реестра", "реестр знаний для агентов", true)]
#[case("пайплайну", "пайплайн собирает образ", true)]
#[case("индексы", "пересобираем индекс из гита", true)]
#[case("докере", "собираем в докер контейнере", true)]
#[case("знанием", "реестр знаний для агентов", true)]
#[case("кубернетес", "у нас нет кубера", false)]
#[case("postgres", "только clickhouse и mongo", false)]
fn stemming_matrix(#[case] q: &str, #[case] body: &str, #[case] should_match: bool) {
let (index, _d, _i) = mini(&[("k", "title", body)]);
let hits = index.search(q, &o(false)).unwrap();
assert_eq!(!hits.is_empty(), should_match, "q='{q}' vs body='{body}'");
}
#[rstest]
#[case("")]
#[case(" ")]
#[case("\"unclosed")]
#[case("title:(((")]
#[case("AND OR NOT")]
#[case("a:b:c")]
#[case("()")]
#[case("[]")]
#[case("*")]
#[case("-dash")]
#[case("+plus")]
#[case("^boost^^")]
#[case("word)")]
#[case("слово)")]
#[case("~~~")]
fn garbage_queries_dont_crash(#[case] q: &str) {
let (index, _d, _i) = mini(&[("k", "title", "an ordinary body")]);
assert!(index.search(q, &o(false)).is_ok(), "failed on q={q:?}");
assert!(index.search(q, &o(true)).is_ok(), "failed on history q={q:?}");
}
fn make_incident(key: &str, service: &str, status: &str) -> Entry {
Entry {
key: key.into(),
title: format!("{service} incident"),
kind: "incident".into(),
service: service.into(),
hosts: vec!["host-a".into()],
severity: "high".into(),
status: status.into(),
knowledge: vec!["orders-api-architecture".into()],
body: "The pipeline stalled and metrics stopped flowing.".into(),
..Default::default()
}
}
#[test]
fn incident_filters() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store.upsert(entry("orders-api-architecture", "orders_api", "The gRPC gateway."), "2026-07-22").unwrap();
store.upsert(make_incident("inc-2026-07-22-orders-api-oom", "orders_api", "open"), "2026-07-22").unwrap();
store.upsert(make_incident("inc-2026-07-21-landing-gap", "web_app", "resolved"), "2026-07-22").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let by = |kind: Option<&str>, status: Option<&str>, service: Option<&str>| {
index
.search("", &SearchOpts {
limit: 10,
kind: kind.map(String::from),
status: status.map(String::from),
service: service.map(String::from),
..Default::default()
})
.unwrap()
};
assert_eq!(by(None, None, None).len(), 3, "no filter sees everything");
assert_eq!(by(Some("incident"), None, None).len(), 2);
assert_eq!(by(Some("knowledge"), None, None).len(), 1);
assert_eq!(by(Some("incident"), Some("open"), None).len(), 1);
assert_eq!(by(Some("incident"), None, Some("ORDERS_API")).len(), 1, "service filter is case-insensitive");
assert_eq!(by(Some("incident"), Some("open"), Some("web_app")).len(), 0);
let hits = by(Some("incident"), Some("open"), None);
let h = &hits[0];
assert_eq!(h.kind, "incident");
assert_eq!(h.service, "orders_api");
assert_eq!(h.status, "open");
assert_eq!(h.severity, "high");
assert_eq!(h.hosts, vec!["host-a"]);
assert_eq!(h.knowledge, vec!["orders-api-architecture"]);
let hits = index
.search("orders api", &SearchOpts { limit: 10, kind: Some("incident".into()), ..Default::default() })
.unwrap();
assert_eq!(hits.len(), 2, "both incidents mention orders_api or link its knowledge: {:?}",
hits.iter().map(|h| &h.key).collect::<Vec<_>>());
let hits = by(Some("knowledge"), None, None);
assert_eq!(hits[0].kind, "knowledge");
assert!(hits[0].service.is_empty());
assert!(hits[0].status.is_empty());
}
fn make_task(key: &str, status: &str, priority: &str) -> Entry {
Entry {
key: key.into(),
title: format!("{key} task"),
kind: "task".into(),
status: status.into(),
priority: priority.into(),
body: "Raise the retention window for container logs.".into(),
..Default::default()
}
}
#[test]
fn task_priority_filter_and_hit_fields() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store.upsert(make_task("task-high-one", "open", "high"), "2026-08-15").unwrap();
store.upsert(make_task("task-high-two", "in_progress", "high"), "2026-08-15").unwrap();
store.upsert(make_task("task-low-one", "open", "low"), "2026-08-15").unwrap();
let mut blocked = make_task("task-blocked", "blocked", "");
blocked.blocked_reason = "waiting on the host-a disk upgrade".into();
store.upsert(blocked, "2026-08-15").unwrap();
store.upsert(entry("plain-note", "A note", "nothing to do with tasks"), "2026-08-15").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let by = |status: Option<&str>, priority: Option<&str>| {
index
.search("", &SearchOpts {
limit: 10,
kind: Some("task".into()),
status: status.map(String::from),
priority: priority.map(String::from),
..Default::default()
})
.unwrap()
};
assert_eq!(by(None, None).len(), 4, "all tasks, the note excluded");
assert_eq!(by(None, Some("high")).len(), 2);
assert_eq!(by(None, Some("low")).len(), 1);
assert_eq!(by(None, Some("critical")).len(), 0, "nothing is critical");
assert_eq!(by(Some("open"), Some("high")).len(), 1, "filters combine");
let h = &by(None, Some("low"))[0];
assert_eq!(h.key, "task-low-one");
assert_eq!(h.priority, "low");
assert!(h.blocked_reason.is_empty());
let h = &by(Some("blocked"), None)[0];
assert_eq!(h.blocked_reason, "waiting on the host-a disk upgrade");
assert!(h.priority.is_empty(), "unranked stays unranked");
}
#[test]
fn task_assignee_and_parent_filters() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
let mut parent = make_task("task-migrate-logs", "in_progress", "high");
parent.assignee = "agent-a".into();
store.upsert(parent, "2026-08-15").unwrap();
let mut child = make_task("task-migrate-logs-step-one", "in_progress", "");
child.assignee = "agent-a".into();
child.parent_task = "task-migrate-logs".into();
store.upsert(child, "2026-08-15").unwrap();
let mut other = make_task("task-swap-disk", "open", "");
other.assignee = "agent-b".into();
other.parent_task = "task-migrate-logs".into();
store.upsert(other, "2026-08-15").unwrap();
store.upsert(make_task("task-unclaimed", "open", ""), "2026-08-15").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let by = |assignee: Option<&str>, parent: Option<&str>| {
index
.search("", &SearchOpts {
limit: 10,
kind: Some("task".into()),
assignee: assignee.map(String::from),
parent_task: parent.map(String::from),
..Default::default()
})
.unwrap()
};
assert_eq!(by(None, None).len(), 4, "no filter sees every task");
assert_eq!(by(Some("agent-a"), None).len(), 2, "what one owner holds");
assert_eq!(by(Some("agent-b"), None).len(), 1);
assert_eq!(by(Some("Agent-A"), None).len(), 0, "the label matches verbatim");
assert_eq!(by(None, Some("task-migrate-logs")).len(), 2, "the children of one parent");
assert_eq!(by(Some("agent-a"), Some("task-migrate-logs")).len(), 1, "filters combine");
assert_eq!(by(None, Some("task-nothing-hangs-here")).len(), 0);
let h = &by(Some("agent-a"), Some("task-migrate-logs"))[0];
assert_eq!(h.key, "task-migrate-logs-step-one");
assert_eq!(h.assignee, "agent-a");
assert_eq!(h.parent_task, "task-migrate-logs");
let all = by(None, None);
let h = all.iter().find(|h| h.key == "task-unclaimed").unwrap();
assert!(h.assignee.is_empty());
assert!(h.parent_task.is_empty());
}
#[test]
fn archived_entries_live_in_default_search() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store.upsert(entry("gone-note", "Wrong note", "a retracted statement"), "2026-07-20").unwrap();
store.delete("gone-note").unwrap();
let mut inc = make_incident("inc-2026-07-20-oom", "svc", "resolved");
inc.resolution = "raised the memory limit".into();
store.upsert(inc, "2026-07-20").unwrap();
store.archive("inc-2026-07-20-oom").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let hits = index.search("memory limit", &o(false)).unwrap();
assert_eq!(hits.len(), 1, "{:?}", hits.iter().map(|h| &h.key).collect::<Vec<_>>());
assert_eq!(hits[0].key, "inc-2026-07-20-oom");
assert!(hits[0].is_head);
assert_eq!(hits[0].status, "resolved");
assert!(index.get_live("inc-2026-07-20-oom").unwrap().is_some());
assert!(index.search("retracted statement", &o(false)).unwrap().is_empty());
assert_eq!(index.search("retracted statement", &o(true)).unwrap().len(), 1);
assert!(index.get_live("gone-note").unwrap().is_none());
}
#[test]
fn title_ranks_above_body() {
let (index, _d, _i) = mini(&[
("in-body", "totally different", "terraform is mentioned in the body"),
("in-title", "terraform config", "nothing special here"),
]);
let hits = index.search("terraform", &o(false)).unwrap();
assert_eq!(hits.len(), 2);
assert_eq!(hits[0].key, "in-title", "title hit must rank first");
}
#[test]
fn as_of_returns_the_version_that_was_current_then() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store.upsert(entry("svc", "Service", "the port is 8080"), "2026-07-19").unwrap();
std::thread::sleep(std::time::Duration::from_millis(1100));
let mid = chrono::Utc::now().timestamp();
std::thread::sleep(std::time::Duration::from_millis(1100));
store.upsert(entry("svc", "Service", "the port is 9090"), "2026-07-21").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let hits = index.search("port", &o(false)).unwrap();
assert_eq!(hits.len(), 1);
assert!(hits[0].body.contains("9090"), "head must be current: {}", hits[0].body);
let opts = SearchOpts { as_of: Some(mid), limit: 10, ..Default::default() };
let hits = index.search("port", &opts).unwrap();
assert_eq!(hits.len(), 1, "as_of must collapse to one version per key");
assert!(hits[0].body.contains("8080"), "as_of must be the superseded value: {}", hits[0].body);
assert!(!hits[0].is_head);
let opts = SearchOpts {
as_of: Some(chrono::Utc::now().timestamp() + 60),
limit: 10,
..Default::default()
};
let hits = index.search("port", &opts).unwrap();
assert_eq!(hits.len(), 1);
assert!(hits[0].body.contains("9090"));
}
#[test]
fn as_of_before_an_entry_existed_does_not_find_it() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
let before = chrono::Utc::now().timestamp() - 3600;
store.upsert(entry("svc", "Service", "the port is 8080"), "2026-07-19").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let opts = SearchOpts { as_of: Some(before), limit: 10, ..Default::default() };
assert!(index.search("port", &opts).unwrap().is_empty(), "an entry cannot predate itself");
}
#[test]
fn as_of_collapses_a_busy_key_to_one_row() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
for i in 0..8 {
store
.upsert(entry("busy", "Busy", &format!("shared token, revision {i}")), "2026-07-20")
.unwrap();
}
store.upsert(entry("other", "Other", "shared token, elsewhere"), "2026-07-20").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let opts = SearchOpts {
as_of: Some(chrono::Utc::now().timestamp() + 60),
limit: 10,
..Default::default()
};
let hits = index.search("shared token", &opts).unwrap();
let keys: Vec<&str> = hits.iter().map(|h| h.key.as_str()).collect();
assert_eq!(hits.len(), 2, "one row per key, got {keys:?}");
assert!(keys.contains(&"busy") && keys.contains(&"other"), "{keys:?}");
let busy = hits.iter().find(|h| h.key == "busy").unwrap();
assert!(busy.body.contains("revision 7"), "newest surviving version wins: {}", busy.body);
}
#[test]
fn as_of_is_second_granular() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store.upsert(entry("svc", "Service", "value one"), "2026-07-19").unwrap();
store.upsert(entry("svc", "Service", "value two"), "2026-07-20").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let versions = store.history("svc").unwrap();
let opts = SearchOpts {
as_of: Some(store.rev_time(&versions[0].sha).unwrap().unwrap()),
limit: 10,
..Default::default()
};
let hits = index.search("value", &opts).unwrap();
assert_eq!(hits.len(), 1, "still exactly one row per key");
assert!(hits[0].body.contains("value two"), "a tie resolves to the later version");
}
#[test]
fn as_of_never_answers_from_a_version_already_superseded_at_that_instant() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store.upsert(entry("svc", "Service", "runs on legacyhost"), "2026-07-19").unwrap();
let mut inc = entry("inc-1", "Outage", "the api was down");
inc.kind = "incident".into();
inc.service = "api".into();
inc.status = "open".into();
store.upsert(inc.clone(), "2026-07-19").unwrap();
std::thread::sleep(std::time::Duration::from_millis(1100));
store.upsert(entry("svc", "Service", "runs on newhost"), "2026-07-20").unwrap();
inc.status = "resolved".into();
store.upsert(inc, "2026-07-20").unwrap();
std::thread::sleep(std::time::Duration::from_millis(1100));
let after_both = chrono::Utc::now().timestamp();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let at = |q: &str, status: Option<&str>| {
let opts = SearchOpts {
as_of: Some(after_both),
status: status.map(String::from),
limit: 10,
..Default::default()
};
index.search(q, &opts).unwrap()
};
assert!(
at("legacyhost", None).is_empty(),
"a superseded version must not answer: {:?}",
at("legacyhost", None).iter().map(|h| &h.body).collect::<Vec<_>>()
);
let hits = at("newhost", None);
assert_eq!(hits.len(), 1);
assert!(hits[0].body.contains("newhost"));
assert!(at("", Some("open")).is_empty(), "status is read from the version current at T");
let hits = at("", Some("resolved"));
assert_eq!(hits.iter().map(|h| h.key.as_str()).collect::<Vec<_>>(), vec!["inc-1"]);
}
#[test]
fn as_of_pairs_documents_with_the_tombstones_of_their_own_generation() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
let mut w = index.writer().unwrap();
let now = chrono::Utc::now().timestamp();
let created = |out: crate::store::UpsertOutcome| match out {
crate::store::UpsertOutcome::Created(c) => c,
_ => panic!("expected a creation"),
};
for key in ["a", "b"] {
let c = created(store.upsert(entry(key, "Short lived", "a fact that was retracted"), "2026-07-19").unwrap());
index.upsert_head(&mut w, &c.entry, &c.sha, c.committed_at).unwrap();
index.commit_and_reload(&mut w).unwrap();
store.delete(key).unwrap();
index.delete_head(&mut w, key);
index.retract(key, now);
index.commit_and_reload(&mut w).unwrap();
}
let c = created(store.upsert(entry("kept", "Kept", "a fact that stays"), "2026-07-19").unwrap());
index.upsert_head(&mut w, &c.entry, &c.sha, c.committed_at).unwrap();
index.commit_and_reload(&mut w).unwrap();
let opts = SearchOpts { as_of: Some(now + 3600), limit: 10, ..Default::default() };
let keys = |hits: Vec<Hit>| hits.into_iter().map(|h| h.key).collect::<Vec<_>>();
let before = index.generation();
assert_eq!(keys(index.search_in(&before, "fact", &opts).unwrap()), vec!["kept"]);
index.reindex(&mut w, &store).unwrap();
assert_ne!(
before.tombstones, index.generation().tombstones,
"the rebuild must have renumbered, or this test proves nothing"
);
assert_eq!(keys(index.search_in(&before, "fact", &opts).unwrap()), vec!["kept"]);
assert_eq!(keys(index.search("fact", &opts).unwrap()), vec!["kept"]);
}
#[test]
fn as_of_forgets_retracted_knowledge_from_the_instant_of_its_deletion() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
let pause = || std::thread::sleep(std::time::Duration::from_millis(1100));
store.upsert(entry("gone", "Gone", "an ephemeral fact"), "2026-07-19").unwrap();
let mut inc = entry("inc-old", "Old outage", "the queue was stuck");
inc.kind = "incident".into();
inc.service = "queue".into();
inc.status = "resolved".into();
store.upsert(inc, "2026-07-19").unwrap();
pause();
let while_alive = chrono::Utc::now().timestamp();
pause();
store.delete("gone").unwrap();
store.archive("inc-old").unwrap();
pause();
let after_deletion = chrono::Utc::now().timestamp();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let at = |q: &str, t: i64| {
let opts = SearchOpts { as_of: Some(t), limit: 10, ..Default::default() };
index.search(q, &opts).unwrap()
};
assert_eq!(at("ephemeral", while_alive).len(), 1, "it existed then");
assert!(at("ephemeral", after_deletion).is_empty(), "retracted: no current version at T");
assert_eq!(at("queue", after_deletion).len(), 1, "archived is not retracted");
pause();
store.upsert(entry("gone", "Gone", "an ephemeral fact"), "2026-07-22").unwrap();
index.reindex(&mut w, &store).unwrap();
assert!(at("ephemeral", after_deletion).is_empty(), "still gone at the earlier instant");
assert_eq!(at("ephemeral", chrono::Utc::now().timestamp() + 60).len(), 1, "back since the rewrite");
}
#[test]
fn history_search_no_duplicates() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store.upsert(entry("k", "t", "common word, first version"), "2026-07-19").unwrap();
store.upsert(entry("k", "t", "common word, second version"), "2026-07-20").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let hits = index.search("common", &o(true)).unwrap();
assert_eq!(hits.len(), 2, "duplicates: {:?}", hits.iter().map(|h| &h.sha).collect::<Vec<_>>());
assert!(hits.iter().all(|h| !h.is_head));
let shas: std::collections::HashSet<_> = hits.iter().map(|h| h.sha.clone()).collect();
assert_eq!(shas.len(), 2);
let hits = index.search("common", &o(false)).unwrap();
assert_eq!(hits.len(), 1);
assert!(hits[0].is_head);
}
#[test]
fn multi_tag_and_semantics() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
let mut e = entry("k", "t", "body");
e.tags = vec!["NATS".into(), "Infra".into()];
store.upsert(e, "2026-07-20").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let search_tags = |tags: &[&str]| {
index
.search("", &SearchOpts {
tags: tags.iter().map(|s| s.to_string()).collect(),
limit: 10,
..Default::default()
})
.unwrap()
.len()
};
assert_eq!(search_tags(&["nats"]), 1, "case must not matter");
assert_eq!(search_tags(&["NATS", "infra"]), 1, "both tags present");
assert_eq!(search_tags(&["nats", "prod"]), 0, "AND: prod is absent");
assert_eq!(search_tags(&["prod"]), 0);
}
#[test]
fn tags_are_searchable_as_text() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
let mut e = entry("probe", "Nothing to see", "This body talks about apples and bicycles.");
e.tags = vec!["kubernetes".into(), "multi-word-tag".into()];
store.upsert(e, "2026-07-20").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
assert_eq!(index.search("kubernetes", &o(false)).unwrap().len(), 1, "tag must be findable");
assert_eq!(index.search("multi word tag", &o(false)).unwrap().len(), 1, "dashed tag tokenized");
assert!(index.search("postgres", &o(false)).unwrap().is_empty(), "no false positives");
}
#[test]
fn key_is_searchable_tokenized() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store
.upsert(entry("orders-api-deploy", "Shipping notes", "Ships through the pipeline."), "2026-07-20")
.unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
assert_eq!(index.search("orders api", &o(false)).unwrap().len(), 1);
assert_eq!(index.search("orders-api-deploy", &o(false)).unwrap().len(), 1);
}
#[test]
fn sort_by_recent_overrides_relevance() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
store
.upsert(entry("older", "Shared shared topic", "shared shared shared word"), "2026-07-19")
.unwrap();
store.upsert(entry("newer", "Later note", "shared word once"), "2026-07-20").unwrap();
let mut w = index.writer().unwrap();
index.reindex(&mut w, &store).unwrap();
let by_score = index.search("shared", &o(false)).unwrap();
assert_eq!(by_score[0].key, "older", "relevance should favour the denser match");
let by_time = index
.search("shared", &SearchOpts { tags: vec![], history: false, limit: 10, recent: true, ..Default::default() })
.unwrap();
assert_eq!(by_time[0].key, "newer", "recent sort must put the latest commit first");
}
#[test]
fn recent_sort_stable_within_one_second() {
let data = tempfile::tempdir().unwrap();
let idxd = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idxd.path()).unwrap();
let mut w = index.writer().unwrap();
for i in 0..6 {
let out = store.upsert(entry(&format!("burst-{i}"), "t", "same body"), &format!("2026-07-22T00:00:0{i}Z")).unwrap();
let c = match out {
crate::store::UpsertOutcome::Created(c) => c,
_ => panic!(),
};
index.upsert_head(&mut w, &c.entry, &c.sha, c.committed_at).unwrap();
}
index.commit_and_reload(&mut w).unwrap();
let opts = SearchOpts { limit: 10, recent: true, ..Default::default() };
let keys = |index: &SearchIndex| -> Vec<String> {
index.search("", &opts).unwrap().into_iter().map(|h| h.key).collect()
};
let want: Vec<String> = (0..6).rev().map(|i| format!("burst-{i}")).collect();
assert_eq!(keys(&index), want, "incremental path");
index.reindex(&mut w, &store).unwrap();
assert_eq!(keys(&index), want, "after full reindex");
}
#[test]
fn limit_respected() {
let docs: Vec<(String, String, String)> = (0..7)
.map(|i| (format!("k-{i}"), format!("title {i}"), "the same body about the service".to_string()))
.collect();
let refs: Vec<(&str, &str, &str)> =
docs.iter().map(|(a, b, c)| (a.as_str(), b.as_str(), c.as_str())).collect();
let (index, _d, _i) = mini(&refs);
for limit in [1, 3, 7, 50] {
let hits = index
.search("service", &SearchOpts { limit, ..Default::default() })
.unwrap();
assert_eq!(hits.len(), limit.min(7), "limit={limit}");
}
}
#[test]
fn search_head_and_history() {
let data = tempfile::tempdir().unwrap();
let idx_dir = tempfile::tempdir().unwrap();
let store = Store::open(data.path()).unwrap();
let index = SearchIndex::open_or_create(idx_dir.path()).unwrap();
let mut w = index.writer().unwrap();
store
.upsert(entry("nats-streams", "NATS: CreateOrUpdateStream", "Всегда используем стримы через CreateOrUpdateStream."), "2026-07-19")
.unwrap();
store
.upsert(entry("nats-streams", "NATS: CreateOrUpdateStream", "New rule: config moved to terraform."), "2026-07-20")
.unwrap();
let (heads, hist) = index.reindex(&mut w, &store).unwrap();
assert_eq!(heads, 1);
assert_eq!(hist, 2);
let opts = |history| SearchOpts { tags: vec![], history, limit: 10, ..Default::default() };
let old_only = index.search("стримах", &opts(false)).unwrap();
assert!(old_only.is_empty(), "old text must not be found in HEAD");
let old_hist = index.search("стримах", &opts(true)).unwrap();
assert_eq!(old_hist.len(), 1);
assert!(!old_hist[0].is_head);
let head = index.search("terraform config", &opts(false)).unwrap();
assert_eq!(head.len(), 1);
assert!(head[0].is_head);
assert_eq!(head[0].sha.len(), 40);
let tagged = index
.search("nats", &SearchOpts { tags: vec!["infra".into()], history: false, limit: 10, recent: false, ..Default::default() })
.unwrap();
assert_eq!(tagged.len(), 1);
let missing = index
.search("nats", &SearchOpts { tags: vec!["absent".into()], history: false, limit: 10, recent: false, ..Default::default() })
.unwrap();
assert!(missing.is_empty());
let out = store.upsert(entry("orders-api", "orders_api deploys", "Deploys go through the pipeline only."), "2026-07-20").unwrap();
let c = match out {
crate::store::UpsertOutcome::Created(c) => c,
_ => panic!(),
};
index.upsert_head(&mut w, &c.entry, &c.sha, c.committed_at).unwrap();
index.commit_and_reload(&mut w).unwrap();
let found = index.search("pipeline deploys", &opts(false)).unwrap();
assert_eq!(found.len(), 1);
assert_eq!(found[0].key, "orders-api");
}
}