meilisearch/meilisearch-core/src/lib.rs

93 lines
2.4 KiB
Rust
Raw Normal View History

#[cfg(test)]
2019-10-18 19:05:28 +08:00
#[macro_use]
extern crate assert_matches;
mod automaton;
2019-12-13 18:49:56 +08:00
mod bucket_sort;
mod database;
mod distinct_map;
2019-10-03 21:04:11 +08:00
mod error;
2019-10-31 00:25:42 +08:00
mod levenshtein;
2019-10-03 17:49:13 +08:00
mod number;
mod query_builder;
2019-10-03 21:04:11 +08:00
mod ranked_map;
mod raw_document;
2019-10-18 19:05:28 +08:00
mod reordered_attrs;
2019-12-13 18:49:56 +08:00
mod update;
pub mod criterion;
pub mod raw_indexer;
2019-10-03 17:49:13 +08:00
pub mod serde;
pub mod store;
pub use self::database::{BoxUpdateFn, Database, MainT, UpdateT};
2019-10-03 23:33:15 +08:00
pub use self::error::{Error, MResult};
2019-10-04 19:26:33 +08:00
pub use self::number::{Number, ParseNumberError};
pub use self::ranked_map::RankedMap;
pub use self::raw_document::RawDocument;
2019-10-04 19:26:33 +08:00
pub use self::store::Index;
pub use self::update::{EnqueuedUpdateResult, ProcessedUpdateResult, UpdateStatus, UpdateType};
pub use meilisearch_types::{DocIndex, DocumentId, Highlight};
use compact_arena::SmallArena;
use crate::bucket_sort::{QueryWordAutomaton, PostingsListView};
use crate::levenshtein::prefix_damerau_levenshtein;
2019-12-13 18:14:12 +08:00
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Document {
pub id: DocumentId,
pub highlights: Vec<Highlight>,
2019-12-13 18:14:12 +08:00
#[cfg(test)]
pub matches: Vec<crate::bucket_sort::SimpleMatch>,
}
impl Document {
pub fn from_raw<'a, 'tag, 'txn>(
raw_document: RawDocument<'a, 'tag>,
automatons: &[QueryWordAutomaton],
arena: &SmallArena<'tag, PostingsListView<'txn>>,
) -> Document
{
2019-12-13 19:38:54 +08:00
let highlights = raw_document.bare_matches.iter().flat_map(|sm| {
let postings_list = &arena[sm.postings_list];
let input = postings_list.input();
let query = &automatons[sm.query_index as usize].query;
postings_list.iter().map(move |m| {
let covered_area = if query.len() > input.len() {
input.len()
} else {
prefix_damerau_levenshtein(query.as_bytes(), input).1
};
Highlight {
attribute: m.attribute,
char_index: m.char_index,
char_length: covered_area as u16,
}
})
}).collect();
2019-12-13 18:14:12 +08:00
#[cfg(not(test))]
{
Document { id: raw_document.id, highlights }
}
#[cfg(test)]
{
let matches = raw_document.processed_matches;
Document { id: raw_document.id, highlights, matches }
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem;
#[test]
fn docindex_mem_size() {
assert_eq!(mem::size_of::<DocIndex>(), 16);
}
}