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::TopDocs;
use tantivy::query::{AllQuery, BooleanQuery, Occur, Query, QueryParser, 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 tantivy::{Index, IndexReader, IndexWriter, Order, 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,
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,
}
pub struct SearchIndex {
pub index: Index,
pub reader: IndexReader,
fields: Fields,
next_seq: AtomicU64,
}
#[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(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 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("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"),
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()?;
Ok(SearchIndex { index, reader, fields, next_seq: AtomicU64::new(1) })
}
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);
}
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(" "));
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 commit_and_reload(&self, w: &mut IndexWriter) -> Result<()> {
w.commit()?;
self.reader.reload()?;
Ok(())
}
pub fn reindex(&self, w: &mut IndexWriter, store: &Store) -> Result<(usize, usize)> {
w.delete_all_documents()?;
w.commit()?;
let hist = store.walk_history()?;
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 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.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>> {
let searcher = self.reader.searcher();
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 head_val = if opts.history { 0 } else { 1 };
clauses.push((
Occur::Must,
Box::new(TermQuery::new(
Term::from_field_u64(self.fields.is_head, head_val),
IndexRecordOption::Basic,
)),
));
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 top: Vec<(f32, tantivy::DocAddress)> = if opts.recent {
searcher
.search(
&query,
&TopDocs::with_limit(limit).order_by_fast_field::<u64>("seq", Order::Desc),
)?
.into_iter()
.map(|(_seq, addr)| (0.0f32, addr))
.collect()
} else {
searcher.search(&query, &TopDocs::with_limit(limit))?
};
let mut hits = vec![];
for (score, addr) in top {
hits.push(self.to_hit(&searcher, addr, score)?);
}
Ok(hits)
}
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(),
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 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");
}
}