Implement fielded Telegram search with a pluggable index store
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
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<DocumentId> {
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user