use telefind_core::{ Document, DocumentId, Kind, Media, MemoryIndex, PublicName, Query, SearchIndex, }; fn post(chat: i64, message: u32, title: &str, body: &str) -> Document { Document { id: DocumentId::Post { chat, message }, username: PublicName::new(&format!("chat{chat}")).unwrap(), title: title.into(), body: body.into(), media: None, } } fn ids(index: &MemoryIndex, q: &Query) -> Vec { index .search(q) .unwrap() .hits .into_iter() .map(|doc| doc.id) .collect() } #[test] fn mixed_script_and_single_character_queries() { let mut index = MemoryIndex::default(); let doc = post(1, 2, "深圳二手", "RTX 5090"); index.upsert(doc.clone()).unwrap(); for keyword in ["深圳 RTX 5090", "深", "5090 二手", "二手 二手"] { assert_eq!(ids(&index, &Query::new(keyword)), vec![doc.id]); } for keyword in ["深圳 5080", "", "!!"] { assert!(ids(&index, &Query::new(keyword)).is_empty()); } } #[test] fn edits_and_deletes_remove_stale_postings_and_keep_other_chats() { let mut index = MemoryIndex::default(); let first = post(1, 7, "旧标题", ""); let second = post(2, 7, "旧标题", ""); for doc in [&first, &second, &first] { index.upsert(doc.clone()).unwrap(); } assert_eq!(index.search(&Query::new("旧标题")).unwrap().total, 2); index.upsert(post(1, 7, "新标题", "")).unwrap(); assert_eq!(ids(&index, &Query::new("旧标题")), vec![second.id]); assert_eq!(ids(&index, &Query::new("新标题")), vec![first.id]); assert!(index.remove(first.id).unwrap()); assert!(!index.remove(first.id).unwrap()); assert!(ids(&index, &Query::new("新标题")).is_empty()); } #[test] fn directory_identity_and_exact_title_rank_first() { let mut index = MemoryIndex::default(); let mut channel = post(1, 1, "摄影", ""); channel.id = DocumentId::Channel(1); channel.username = PublicName::new("@Photo_News").unwrap(); index.upsert(post(2, 3, "摄影", "photo_news")).unwrap(); index.upsert(channel.clone()).unwrap(); for term in ["摄影", "@PHOTO_NEWS"] { assert_eq!(ids(&index, &Query::new(term))[0], channel.id); } assert_eq!(channel.url(), "https://t.me/photo_news"); assert!(PublicName::new("a/b?c").is_none()); } #[test] fn title_and_contiguous_phrase_outrank_scattered_words() { let mut index = MemoryIndex::default(); let exact = post(1, 1, "rust search", ""); let separated = post(1, 2, "search rust", ""); let body = post(1, 3, "", "rust and search"); for doc in [&body, &separated, &exact] { index.upsert(doc.clone()).unwrap(); } assert_eq!( ids(&index, &Query::new("rust search")), vec![exact.id, separated.id, body.id] ); } #[test] fn filtering_precedes_paging_and_ties_are_stable() { let mut index = MemoryIndex::default(); for id in (1..=5).rev() { let mut doc = post(1, id, "sample", ""); if id % 2 == 1 { doc.media = Some(Media::Video); } index.upsert(doc).unwrap(); } let mut query = Query { kind: Some(Kind::Post), media: Some(Media::Video), offset: 1, limit: 1, ..Query::new("sample") }; let page = index.search(&query).unwrap(); assert_eq!(page.total, 3); assert_eq!(page.hits[0].url(), "https://t.me/chat1/3"); query.offset = usize::MAX; assert!(ids(&index, &query).is_empty()); query.offset = 0; query.limit = 0; assert_eq!(index.search(&query).unwrap().total, 3); assert!(ids(&index, &query).is_empty()); query.kind = Some(Kind::Bot); assert_eq!(index.search(&query).unwrap().total, 0); } #[test] fn quoted_phrase_requires_adjacency_in_a_single_field() { let mut index = MemoryIndex::default(); for doc in [ post(1, 1, "Rust Search", "入门"), post(1, 2, "Rust", "Search 入门"), post(1, 3, "Search Rust", "入门"), ] { index.upsert(doc).unwrap(); } assert_eq!( ids(&index, &Query::new("\"rust search\" 入门")), vec![DocumentId::Post { chat: 1, message: 1 }] ); } #[test] fn unspaced_chinese_query_matches_words_across_fields_and_keeps_model_required() { let mut index = MemoryIndex::default(); index .upsert(post(1, 1, "上海", "二手 相机 RTX 5090")) .unwrap(); index .upsert(post(1, 2, "上海", "二手 相机 RTX 5080")) .unwrap(); assert_eq!( ids(&index, &Query::new("上海二手相机 5090")), vec![DocumentId::Post { chat: 1, message: 1 }] ); } #[test] #[ignore = "synthetic capacity measurement; run explicitly in release mode"] fn measure_ranked_queries_over_25000_posts() { use std::time::Instant; let mut index = MemoryIndex::default(); let start = Instant::now(); for id in 1..=25_000 { let title = format!("频道sku{} 相机 {}", id % 100, id % 50); index.upsert(post(1, id, &title, "摄影教程与设备")).unwrap(); } let ingest = start.elapsed(); let queries = ["相机", "sku3 3", "设备", "摄影教程 17"]; for text in queries { let query = Query::new(text); let page = index.search(&query).unwrap(); let expected = match text { "sku3 3" => 250, "摄影教程 17" => 500, _ => 25_000, }; assert_eq!(page.total, expected); let mut times = Vec::new(); for _ in 0..30 { let start = Instant::now(); let result = index.search(&query).unwrap(); assert_eq!(result.hits.len(), 10); times.push(start.elapsed()); } times.sort_unstable(); println!( "records=25000 ingest={ingest:?} query={text:?} matches={expected} p50={:?} p95={:?}", times[14], times[28] ); } }