2019-10-08 21:22:36 +08:00
|
|
|
#[cfg(test)]
|
2019-10-18 19:05:28 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate assert_matches;
|
2019-10-08 21:22:36 +08:00
|
|
|
|
2019-10-02 23:34:32 +08:00
|
|
|
mod automaton;
|
2019-12-13 18:49:56 +08:00
|
|
|
mod bucket_sort;
|
2019-10-04 22:49:17 +08:00
|
|
|
mod database;
|
2019-10-09 19:44:18 +08:00
|
|
|
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;
|
2019-10-02 23:34:32 +08:00
|
|
|
mod query_builder;
|
2020-01-08 00:40:58 +08:00
|
|
|
mod query_tree;
|
2020-01-13 20:29:47 +08:00
|
|
|
mod query_words_mapper;
|
2019-10-03 21:04:11 +08:00
|
|
|
mod ranked_map;
|
2019-10-02 23:34:32 +08:00
|
|
|
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;
|
2019-10-02 23:34:32 +08:00
|
|
|
pub mod store;
|
2019-11-30 23:53:34 +08:00
|
|
|
|
2019-11-26 23:12:06 +08:00
|
|
|
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;
|
2019-10-04 22:49:17 +08:00
|
|
|
pub use self::raw_document::RawDocument;
|
2019-10-04 19:26:33 +08:00
|
|
|
pub use self::store::Index;
|
2019-10-31 18:13:37 +08:00
|
|
|
pub use self::update::{EnqueuedUpdateResult, ProcessedUpdateResult, UpdateStatus, UpdateType};
|
2019-12-13 18:46:53 +08:00
|
|
|
pub use meilisearch_types::{DocIndex, DocumentId, Highlight};
|
2020-01-13 20:29:47 +08:00
|
|
|
pub use query_words_mapper::QueryWordsMapper;
|
2019-10-02 23:34:32 +08:00
|
|
|
|
2019-12-12 00:36:53 +08:00
|
|
|
use compact_arena::SmallArena;
|
|
|
|
use crate::bucket_sort::{QueryWordAutomaton, PostingsListView};
|
|
|
|
use crate::levenshtein::prefix_damerau_levenshtein;
|
2019-12-13 20:22:54 +08:00
|
|
|
use crate::reordered_attrs::ReorderedAttrs;
|
2019-12-12 00:36:53 +08:00
|
|
|
|
2019-12-13 18:14:12 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
2019-10-02 23:34:32 +08:00
|
|
|
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>,
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|
|
|
|
|
2019-12-13 20:22:54 +08:00
|
|
|
fn highlights_from_raw_document<'a, 'tag, 'txn>(
|
|
|
|
raw_document: &RawDocument<'a, 'tag>,
|
|
|
|
automatons: &[QueryWordAutomaton],
|
|
|
|
arena: &SmallArena<'tag, PostingsListView<'txn>>,
|
|
|
|
searchable_attrs: Option<&ReorderedAttrs>,
|
|
|
|
) -> Vec<Highlight>
|
|
|
|
{
|
|
|
|
let mut highlights = Vec::new();
|
|
|
|
|
|
|
|
for bm in raw_document.bare_matches.iter() {
|
|
|
|
let postings_list = &arena[bm.postings_list];
|
|
|
|
let input = postings_list.input();
|
|
|
|
let query = &automatons[bm.query_index as usize].query;
|
|
|
|
|
|
|
|
for di in postings_list.iter() {
|
|
|
|
let covered_area = if query.len() > input.len() {
|
|
|
|
input.len()
|
|
|
|
} else {
|
|
|
|
prefix_damerau_levenshtein(query.as_bytes(), input).1
|
|
|
|
};
|
|
|
|
|
|
|
|
let attribute = searchable_attrs
|
|
|
|
.and_then(|sa| sa.reverse(di.attribute))
|
|
|
|
.unwrap_or(di.attribute);
|
|
|
|
|
|
|
|
let highlight = Highlight {
|
|
|
|
attribute: attribute,
|
|
|
|
char_index: di.char_index,
|
|
|
|
char_length: covered_area as u16,
|
|
|
|
};
|
|
|
|
|
|
|
|
highlights.push(highlight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
highlights
|
|
|
|
}
|
|
|
|
|
2019-12-12 00:36:53 +08:00
|
|
|
impl Document {
|
2019-12-21 20:44:19 +08:00
|
|
|
#[cfg(not(test))]
|
|
|
|
pub fn from_highlights(id: DocumentId, highlights: &[Highlight]) -> Document {
|
|
|
|
Document { id, highlights: highlights.to_owned() }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn from_highlights(id: DocumentId, highlights: &[Highlight]) -> Document {
|
|
|
|
Document { id, highlights: highlights.to_owned(), matches: Vec::new() }
|
|
|
|
}
|
|
|
|
|
2019-12-13 20:22:54 +08:00
|
|
|
#[cfg(not(test))]
|
2019-12-12 00:36:53 +08:00
|
|
|
pub fn from_raw<'a, 'tag, 'txn>(
|
|
|
|
raw_document: RawDocument<'a, 'tag>,
|
2020-01-13 21:36:06 +08:00
|
|
|
// automatons: &[QueryWordAutomaton],
|
2019-12-12 00:36:53 +08:00
|
|
|
arena: &SmallArena<'tag, PostingsListView<'txn>>,
|
2019-12-13 20:22:54 +08:00
|
|
|
searchable_attrs: Option<&ReorderedAttrs>,
|
2019-12-12 00:36:53 +08:00
|
|
|
) -> Document
|
|
|
|
{
|
2020-01-13 21:36:06 +08:00
|
|
|
// let highlights = highlights_from_raw_document(
|
|
|
|
// &raw_document,
|
|
|
|
// automatons,
|
|
|
|
// arena,
|
|
|
|
// searchable_attrs,
|
|
|
|
// );
|
|
|
|
|
|
|
|
let highlights = Vec::new();
|
2019-12-13 20:22:54 +08:00
|
|
|
|
|
|
|
Document { id: raw_document.id, highlights }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn from_raw<'a, 'tag, 'txn>(
|
|
|
|
raw_document: RawDocument<'a, 'tag>,
|
2020-01-13 21:36:06 +08:00
|
|
|
// automatons: &[QueryWordAutomaton],
|
2019-12-13 20:22:54 +08:00
|
|
|
arena: &SmallArena<'tag, PostingsListView<'txn>>,
|
|
|
|
searchable_attrs: Option<&ReorderedAttrs>,
|
|
|
|
) -> Document
|
|
|
|
{
|
|
|
|
use crate::bucket_sort::SimpleMatch;
|
2019-12-13 18:14:12 +08:00
|
|
|
|
2020-01-13 21:36:06 +08:00
|
|
|
// let highlights = highlights_from_raw_document(
|
|
|
|
// &raw_document,
|
|
|
|
// automatons,
|
|
|
|
// arena,
|
|
|
|
// searchable_attrs,
|
|
|
|
// );
|
|
|
|
|
|
|
|
let highlights = Vec::new();
|
2019-12-13 20:22:54 +08:00
|
|
|
|
|
|
|
let mut matches = Vec::new();
|
|
|
|
for sm in raw_document.processed_matches {
|
|
|
|
let attribute = searchable_attrs
|
|
|
|
.and_then(|sa| sa.reverse(sm.attribute))
|
|
|
|
.unwrap_or(sm.attribute);
|
|
|
|
|
|
|
|
matches.push(SimpleMatch { attribute, ..sm });
|
2019-12-13 18:14:12 +08:00
|
|
|
}
|
2019-12-13 20:22:54 +08:00
|
|
|
matches.sort_unstable();
|
|
|
|
|
|
|
|
Document { id: raw_document.id, highlights, matches }
|
2019-12-12 00:36:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 23:34:32 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn docindex_mem_size() {
|
|
|
|
assert_eq!(mem::size_of::<DocIndex>(), 16);
|
|
|
|
}
|
|
|
|
}
|