sort results by timestamp

This commit is contained in:
Nikita Galaiko 2023-03-06 10:42:41 +01:00
parent b1fc932a81
commit cfd151fcb9
2 changed files with 50 additions and 2 deletions

View File

@ -62,7 +62,13 @@ impl Deltas {
let mmap_dir = MmapDirectory::open(dir)?;
let schema = build_schema();
let index = tantivy::Index::open_or_create(mmap_dir, schema)?;
let index_settings = tantivy::IndexSettings {
..Default::default()
};
let index = tantivy::IndexBuilder::new()
.schema(schema)
.settings(index_settings)
.open_or_create(mmap_dir)?;
let reader = index.reader()?;
let writer = index.writer(WRITE_BUFFER_SIZE)?;
@ -279,7 +285,12 @@ pub fn search(
reader.reload()?;
let searcher = reader.searcher();
let top_docs = searcher.search(query, &collector::TopDocs::with_limit(10))?;
let top_docs = searcher.search(
query,
&collector::TopDocs::with_limit(10)
.order_by_u64_field(index.schema().get_field("timestamp_ms").unwrap()),
)?;
let results = top_docs
.iter()

View File

@ -26,6 +26,43 @@ fn test_project() -> Result<(git2::Repository, projects::Project)> {
Ok((repo, project))
}
#[test]
fn test_sorted_by_timestamp() {
let (repo, project) = test_project().unwrap();
let index_path = tempdir().unwrap().path().to_str().unwrap().to_string();
let mut session = sessions::Session::from_head(&repo, &project).unwrap();
deltas::write(
&repo,
&project,
Path::new("test.txt"),
&vec![
deltas::Delta {
operations: vec![Operation::Insert((0, "Hello".to_string()))],
timestamp_ms: 0,
},
deltas::Delta {
operations: vec![Operation::Insert((5, " World".to_string()))],
timestamp_ms: 1,
},
],
)
.unwrap();
session.flush(&repo, &None, &project).unwrap();
let mut searcher = super::Deltas::at(index_path.into()).unwrap();
let write_result = searcher.index_session(&repo, &project, &session);
assert!(write_result.is_ok());
let search_result = searcher.search(&project.id, "hello world");
assert!(search_result.is_ok());
let search_result = search_result.unwrap();
assert_eq!(search_result.len(), 2);
assert_eq!(search_result[0].index, 1);
assert_eq!(search_result[1].index, 0);
}
#[test]
fn test_simple() {
let (repo, project) = test_project().unwrap();