2018-10-17 19:35:34 +08:00
|
|
|
use std::hash::Hash;
|
2019-04-18 20:11:00 +08:00
|
|
|
use std::ops::{Range, Deref};
|
2018-12-16 21:21:06 +08:00
|
|
|
use std::rc::Rc;
|
2019-04-18 20:11:00 +08:00
|
|
|
use std::time::Instant;
|
|
|
|
use std::{cmp, mem};
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
use rayon::slice::ParallelSliceMut;
|
2019-02-26 01:34:51 +08:00
|
|
|
use slice_group_by::GroupByMut;
|
|
|
|
use meilidb_tokenizer::{is_cjk, split_query_string};
|
2019-03-05 23:34:29 +08:00
|
|
|
use hashbrown::{HashMap, HashSet};
|
2018-12-02 20:11:02 +08:00
|
|
|
use fst::Streamer;
|
2019-01-06 22:01:44 +08:00
|
|
|
use log::info;
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2018-11-28 02:11:33 +08:00
|
|
|
use crate::automaton::{self, DfaExt, AutomatonExt};
|
2019-02-25 02:44:24 +08:00
|
|
|
use crate::distinct_map::{DistinctMap, BufferedDistinctMap};
|
|
|
|
use crate::criterion::Criteria;
|
2019-04-20 00:27:57 +08:00
|
|
|
use crate::raw_documents_from_matches;
|
|
|
|
use crate::{Match, DocumentId, Index, Store, RawDocument, Document};
|
2019-02-25 02:44:24 +08:00
|
|
|
|
2019-02-26 01:34:51 +08:00
|
|
|
fn generate_automatons(query: &str) -> Vec<DfaExt> {
|
2018-12-11 21:49:45 +08:00
|
|
|
let has_end_whitespace = query.chars().last().map_or(false, char::is_whitespace);
|
2019-02-26 01:34:51 +08:00
|
|
|
let mut groups = split_query_string(query).map(str::to_lowercase).peekable();
|
2019-02-23 21:57:13 +08:00
|
|
|
let mut automatons = Vec::new();
|
2019-02-26 01:34:51 +08:00
|
|
|
|
2019-02-23 21:57:13 +08:00
|
|
|
while let Some(word) = groups.next() {
|
|
|
|
let has_following_word = groups.peek().is_some();
|
|
|
|
let lev = if has_following_word || has_end_whitespace || word.chars().all(is_cjk) {
|
2018-12-11 21:49:45 +08:00
|
|
|
automaton::build_dfa(&word)
|
|
|
|
} else {
|
|
|
|
automaton::build_prefix_dfa(&word)
|
2018-12-10 19:16:24 +08:00
|
|
|
};
|
2018-11-28 02:11:33 +08:00
|
|
|
automatons.push(lev);
|
|
|
|
}
|
2018-12-11 21:49:45 +08:00
|
|
|
|
2018-11-28 02:11:33 +08:00
|
|
|
automatons
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:11:00 +08:00
|
|
|
pub struct QueryBuilder<'c, I, FI = fn(DocumentId) -> bool> {
|
|
|
|
index: I,
|
2019-02-24 18:58:22 +08:00
|
|
|
criteria: Criteria<'c>,
|
2019-03-05 23:34:29 +08:00
|
|
|
searchable_attrs: Option<HashSet<u16>>,
|
2018-12-30 03:16:29 +08:00
|
|
|
filter: Option<FI>,
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:11:00 +08:00
|
|
|
impl<'c, I> QueryBuilder<'c, I, fn(DocumentId) -> bool> {
|
|
|
|
pub fn new(index: I) -> Self {
|
2019-02-24 18:58:22 +08:00
|
|
|
QueryBuilder::with_criteria(index, Criteria::default())
|
2018-11-28 02:11:33 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2019-04-18 20:11:00 +08:00
|
|
|
pub fn with_criteria(index: I, criteria: Criteria<'c>) -> Self {
|
2019-03-05 23:34:29 +08:00
|
|
|
QueryBuilder { index, criteria, searchable_attrs: None, filter: None }
|
2018-11-28 02:11:33 +08:00
|
|
|
}
|
2019-02-03 17:55:16 +08:00
|
|
|
}
|
2018-11-28 02:11:33 +08:00
|
|
|
|
2019-04-18 20:11:00 +08:00
|
|
|
impl<'c, I, FI> QueryBuilder<'c, I, FI>
|
2019-02-03 17:55:16 +08:00
|
|
|
{
|
2019-04-18 20:11:00 +08:00
|
|
|
pub fn with_filter<F>(self, function: F) -> QueryBuilder<'c, I, F>
|
2019-02-24 18:58:22 +08:00
|
|
|
where F: Fn(DocumentId) -> bool,
|
2018-12-30 03:16:29 +08:00
|
|
|
{
|
|
|
|
QueryBuilder {
|
2019-02-24 18:58:22 +08:00
|
|
|
index: self.index,
|
2018-12-30 03:16:29 +08:00
|
|
|
criteria: self.criteria,
|
2019-03-05 23:34:29 +08:00
|
|
|
searchable_attrs: self.searchable_attrs,
|
2018-12-30 03:16:29 +08:00
|
|
|
filter: Some(function)
|
|
|
|
}
|
2018-11-28 02:11:33 +08:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:11:00 +08:00
|
|
|
pub fn with_distinct<F, K>(self, function: F, size: usize) -> DistinctQueryBuilder<'c, I, FI, F>
|
2019-02-24 18:58:22 +08:00
|
|
|
where F: Fn(DocumentId) -> Option<K>,
|
2018-12-30 03:16:29 +08:00
|
|
|
K: Hash + Eq,
|
|
|
|
{
|
2018-11-28 02:11:33 +08:00
|
|
|
DistinctQueryBuilder {
|
2018-11-29 00:12:24 +08:00
|
|
|
inner: self,
|
2018-11-28 02:11:33 +08:00
|
|
|
function: function,
|
|
|
|
size: size
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-05 23:34:29 +08:00
|
|
|
pub fn add_searchable_attribute(&mut self, attribute: u16) {
|
|
|
|
let attributes = self.searchable_attrs.get_or_insert_with(HashSet::new);
|
|
|
|
attributes.insert(attribute);
|
|
|
|
}
|
2019-04-18 20:11:00 +08:00
|
|
|
}
|
2019-03-05 23:34:29 +08:00
|
|
|
|
2019-04-20 00:27:57 +08:00
|
|
|
impl<'c, I, FI, S> QueryBuilder<'c, I, FI>
|
|
|
|
where I: Deref<Target=Index<S>>,
|
|
|
|
S: Store,
|
2019-04-18 20:11:00 +08:00
|
|
|
{
|
2019-02-02 21:22:31 +08:00
|
|
|
fn query_all(&self, query: &str) -> Vec<RawDocument> {
|
2019-02-26 01:34:51 +08:00
|
|
|
let automatons = generate_automatons(query);
|
2019-04-20 00:27:57 +08:00
|
|
|
let fst = self.index.set.as_fst();
|
2018-11-29 00:12:24 +08:00
|
|
|
|
|
|
|
let mut stream = {
|
2019-04-20 00:27:57 +08:00
|
|
|
let mut op_builder = fst::raw::OpBuilder::new();
|
2018-11-29 00:12:24 +08:00
|
|
|
for automaton in &automatons {
|
2019-04-20 00:27:57 +08:00
|
|
|
let stream = fst.search(automaton);
|
2018-11-29 00:12:24 +08:00
|
|
|
op_builder.push(stream);
|
|
|
|
}
|
2019-04-20 00:03:32 +08:00
|
|
|
op_builder.r#union()
|
2018-11-29 00:12:24 +08:00
|
|
|
};
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
let mut matches = Vec::new();
|
2018-10-11 20:04:41 +08:00
|
|
|
|
2018-11-29 00:12:24 +08:00
|
|
|
while let Some((input, indexed_values)) = stream.next() {
|
2018-10-11 20:04:41 +08:00
|
|
|
for iv in indexed_values {
|
2018-11-28 02:11:33 +08:00
|
|
|
let automaton = &automatons[iv.index];
|
2018-11-29 00:12:24 +08:00
|
|
|
let distance = automaton.eval(input).to_u8();
|
|
|
|
let is_exact = distance == 0 && input.len() == automaton.query_len();
|
|
|
|
|
2019-04-20 00:27:57 +08:00
|
|
|
// let doc_indexes = &self.index.indexes;
|
|
|
|
// let doc_indexes = &doc_indexes[iv.value as usize];
|
|
|
|
|
|
|
|
let doc_indexes = self.index.store.get_indexes(input).unwrap().unwrap();
|
2018-10-11 20:04:41 +08:00
|
|
|
|
2019-04-20 00:27:57 +08:00
|
|
|
for di in doc_indexes.as_slice() {
|
2019-03-05 23:34:29 +08:00
|
|
|
if self.searchable_attrs.as_ref().map_or(true, |r| r.contains(&di.attribute)) {
|
|
|
|
let match_ = Match {
|
|
|
|
query_index: iv.index as u32,
|
|
|
|
distance: distance,
|
|
|
|
attribute: di.attribute,
|
|
|
|
word_index: di.word_index,
|
|
|
|
is_exact: is_exact,
|
|
|
|
char_index: di.char_index,
|
|
|
|
char_length: di.char_length,
|
|
|
|
};
|
|
|
|
matches.push((di.document_id, match_));
|
|
|
|
}
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
let total_matches = matches.len();
|
|
|
|
let raw_documents = raw_documents_from_matches(matches);
|
2019-01-06 22:01:44 +08:00
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
info!("{} total documents to classify", raw_documents.len());
|
|
|
|
info!("{} total matches to classify", total_matches);
|
|
|
|
|
|
|
|
raw_documents
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 00:27:57 +08:00
|
|
|
impl<'c, I, FI, S> QueryBuilder<'c, I, FI>
|
|
|
|
where I: Deref<Target=Index<S>>,
|
2019-04-18 20:11:00 +08:00
|
|
|
FI: Fn(DocumentId) -> bool,
|
2019-04-20 00:27:57 +08:00
|
|
|
S: Store,
|
2018-10-17 19:35:34 +08:00
|
|
|
{
|
2018-12-30 03:16:59 +08:00
|
|
|
pub fn query(self, query: &str, range: Range<usize>) -> Vec<Document> {
|
2019-02-03 18:12:34 +08:00
|
|
|
// We delegate the filter work to the distinct query builder,
|
2018-12-30 03:16:59 +08:00
|
|
|
// specifying a distinct rule that has no effect.
|
|
|
|
if self.filter.is_some() {
|
2019-02-24 18:58:22 +08:00
|
|
|
let builder = self.with_distinct(|_| None as Option<()>, 1);
|
2018-12-30 03:16:59 +08:00
|
|
|
return builder.query(query, range);
|
|
|
|
}
|
|
|
|
|
2019-02-17 03:44:16 +08:00
|
|
|
let start = Instant::now();
|
|
|
|
let mut documents = self.query_all(query);
|
|
|
|
info!("query_all took {:.2?}", start.elapsed());
|
2019-01-15 04:18:46 +08:00
|
|
|
|
2018-10-17 19:35:34 +08:00
|
|
|
let mut groups = vec![documents.as_mut_slice()];
|
|
|
|
|
2019-01-15 04:18:46 +08:00
|
|
|
'criteria: for (ci, criterion) in self.criteria.as_ref().iter().enumerate() {
|
2018-10-17 19:35:34 +08:00
|
|
|
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
2018-12-16 21:21:06 +08:00
|
|
|
let mut documents_seen = 0;
|
|
|
|
|
|
|
|
for group in tmp_groups {
|
2019-01-15 04:18:46 +08:00
|
|
|
info!("criterion {}, documents group of size {}", ci, group.len());
|
|
|
|
|
2018-12-16 21:21:06 +08:00
|
|
|
// if this group does not overlap with the requested range,
|
|
|
|
// push it without sorting and splitting it
|
|
|
|
if documents_seen + group.len() < range.start {
|
|
|
|
documents_seen += group.len();
|
|
|
|
groups.push(group);
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2019-02-17 03:44:16 +08:00
|
|
|
let start = Instant::now();
|
|
|
|
group.par_sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
|
|
|
info!("criterion {} sort took {:.2?}", ci, start.elapsed());
|
2018-12-16 21:21:06 +08:00
|
|
|
|
2019-02-02 21:23:18 +08:00
|
|
|
for group in group.binary_group_by_mut(|a, b| criterion.eq(a, b)) {
|
2018-12-16 21:21:06 +08:00
|
|
|
documents_seen += group.len();
|
2018-10-17 19:35:34 +08:00
|
|
|
groups.push(group);
|
2018-12-16 21:21:06 +08:00
|
|
|
|
|
|
|
// we have sort enough documents if the last document sorted is after
|
|
|
|
// the end of the requested range, we can continue to the next criterion
|
|
|
|
if documents_seen >= range.end { continue 'criteria }
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 21:21:06 +08:00
|
|
|
let offset = cmp::min(documents.len(), range.start);
|
2019-02-02 21:22:31 +08:00
|
|
|
let iter = documents.into_iter().skip(offset).take(range.len());
|
|
|
|
iter.map(|d| Document::from_raw(&d)).collect()
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
2018-11-28 02:11:33 +08:00
|
|
|
}
|
2018-10-11 20:04:41 +08:00
|
|
|
|
2019-04-18 20:11:00 +08:00
|
|
|
pub struct DistinctQueryBuilder<'c, I, FI, FD> {
|
|
|
|
inner: QueryBuilder<'c, I, FI>,
|
2018-12-30 03:16:29 +08:00
|
|
|
function: FD,
|
2018-11-28 02:11:33 +08:00
|
|
|
size: usize,
|
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2019-04-18 20:11:00 +08:00
|
|
|
impl<'c, I, FI, FD> DistinctQueryBuilder<'c, I, FI, FD>
|
2018-12-30 03:16:29 +08:00
|
|
|
{
|
2019-04-18 20:11:00 +08:00
|
|
|
pub fn with_filter<F>(self, function: F) -> DistinctQueryBuilder<'c, I, F, FD>
|
2019-02-24 18:58:22 +08:00
|
|
|
where F: Fn(DocumentId) -> bool,
|
2018-12-30 03:16:29 +08:00
|
|
|
{
|
|
|
|
DistinctQueryBuilder {
|
|
|
|
inner: self.inner.with_filter(function),
|
|
|
|
function: self.function,
|
|
|
|
size: self.size
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 23:34:29 +08:00
|
|
|
|
|
|
|
pub fn add_searchable_attribute(&mut self, attribute: u16) {
|
|
|
|
self.inner.add_searchable_attribute(attribute);
|
|
|
|
}
|
2018-12-30 03:16:29 +08:00
|
|
|
}
|
|
|
|
|
2019-04-20 00:27:57 +08:00
|
|
|
impl<'c, I, FI, FD, K, S> DistinctQueryBuilder<'c, I, FI, FD>
|
|
|
|
where I: Deref<Target=Index<S>>,
|
2019-04-18 20:11:00 +08:00
|
|
|
FI: Fn(DocumentId) -> bool,
|
2019-02-24 18:58:22 +08:00
|
|
|
FD: Fn(DocumentId) -> Option<K>,
|
2018-11-29 00:12:24 +08:00
|
|
|
K: Hash + Eq,
|
2019-04-20 00:27:57 +08:00
|
|
|
S: Store,
|
2018-11-28 02:11:33 +08:00
|
|
|
{
|
2018-12-30 03:16:29 +08:00
|
|
|
pub fn query(self, query: &str, range: Range<usize>) -> Vec<Document> {
|
2019-02-17 03:44:16 +08:00
|
|
|
let start = Instant::now();
|
|
|
|
let mut documents = self.inner.query_all(query);
|
|
|
|
info!("query_all took {:.2?}", start.elapsed());
|
2019-02-02 21:22:31 +08:00
|
|
|
|
2018-11-29 00:12:24 +08:00
|
|
|
let mut groups = vec![documents.as_mut_slice()];
|
2018-12-16 21:22:04 +08:00
|
|
|
let mut key_cache = HashMap::new();
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-30 03:16:48 +08:00
|
|
|
let mut filter_map = HashMap::new();
|
2018-12-16 21:22:04 +08:00
|
|
|
// these two variables informs on the current distinct map and
|
|
|
|
// on the raw offset of the start of the group where the
|
|
|
|
// range.start bound is located according to the distinct function
|
|
|
|
let mut distinct_map = DistinctMap::new(self.size);
|
|
|
|
let mut distinct_raw_offset = 0;
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
'criteria: for (ci, criterion) in self.inner.criteria.as_ref().iter().enumerate() {
|
2018-11-29 00:12:24 +08:00
|
|
|
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
2018-12-16 21:22:04 +08:00
|
|
|
let mut buf_distinct = BufferedDistinctMap::new(&mut distinct_map);
|
|
|
|
let mut documents_seen = 0;
|
|
|
|
|
|
|
|
for group in tmp_groups {
|
2019-02-02 21:22:31 +08:00
|
|
|
info!("criterion {}, documents group of size {}", ci, group.len());
|
|
|
|
|
2018-12-16 21:22:04 +08:00
|
|
|
// if this group does not overlap with the requested range,
|
|
|
|
// push it without sorting and splitting it
|
|
|
|
if documents_seen + group.len() < distinct_raw_offset {
|
|
|
|
documents_seen += group.len();
|
|
|
|
groups.push(group);
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2019-02-17 03:44:16 +08:00
|
|
|
let start = Instant::now();
|
|
|
|
group.par_sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
|
|
|
info!("criterion {} sort took {:.2?}", ci, start.elapsed());
|
2018-12-16 21:22:04 +08:00
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
for group in group.binary_group_by_mut(|a, b| criterion.eq(a, b)) {
|
2018-12-16 21:22:04 +08:00
|
|
|
// we must compute the real distinguished len of this sub-group
|
2018-12-13 18:54:09 +08:00
|
|
|
for document in group.iter() {
|
2018-12-30 03:16:48 +08:00
|
|
|
let filter_accepted = match &self.inner.filter {
|
|
|
|
Some(filter) => {
|
|
|
|
let entry = filter_map.entry(document.id);
|
2019-02-24 18:58:22 +08:00
|
|
|
*entry.or_insert_with(|| (filter)(document.id))
|
2018-12-30 03:16:48 +08:00
|
|
|
},
|
2019-01-02 22:07:46 +08:00
|
|
|
None => true,
|
2018-12-13 18:54:09 +08:00
|
|
|
};
|
2018-12-16 21:22:04 +08:00
|
|
|
|
2018-12-30 03:16:48 +08:00
|
|
|
if filter_accepted {
|
|
|
|
let entry = key_cache.entry(document.id);
|
2019-02-24 18:58:22 +08:00
|
|
|
let key = entry.or_insert_with(|| (self.function)(document.id).map(Rc::new));
|
2018-12-30 03:16:48 +08:00
|
|
|
|
|
|
|
match key.clone() {
|
|
|
|
Some(key) => buf_distinct.register(key),
|
|
|
|
None => buf_distinct.register_without_key(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-16 21:22:04 +08:00
|
|
|
// the requested range end is reached: stop computing distinct
|
|
|
|
if buf_distinct.len() >= range.end { break }
|
2018-12-13 18:54:09 +08:00
|
|
|
}
|
2018-12-16 21:22:04 +08:00
|
|
|
|
|
|
|
documents_seen += group.len();
|
2018-11-29 00:12:24 +08:00
|
|
|
groups.push(group);
|
2018-12-16 21:22:04 +08:00
|
|
|
|
|
|
|
// if this sub-group does not overlap with the requested range
|
|
|
|
// we must update the distinct map and its start index
|
|
|
|
if buf_distinct.len() < range.start {
|
|
|
|
buf_distinct.transfert_to_internal();
|
|
|
|
distinct_raw_offset = documents_seen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have sort enough documents if the last document sorted is after
|
|
|
|
// the end of the requested range, we can continue to the next criterion
|
|
|
|
if buf_distinct.len() >= range.end { continue 'criteria }
|
2018-11-29 00:12:24 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-18 21:08:04 +08:00
|
|
|
}
|
|
|
|
|
2018-12-16 21:22:04 +08:00
|
|
|
let mut out_documents = Vec::with_capacity(range.len());
|
|
|
|
let mut seen = BufferedDistinctMap::new(&mut distinct_map);
|
|
|
|
|
|
|
|
for document in documents.into_iter().skip(distinct_raw_offset) {
|
2018-12-30 03:16:48 +08:00
|
|
|
let filter_accepted = match &self.inner.filter {
|
|
|
|
Some(_) => filter_map.remove(&document.id).expect("BUG: filtered not found"),
|
|
|
|
None => true,
|
2018-11-29 00:12:24 +08:00
|
|
|
};
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-30 03:16:48 +08:00
|
|
|
if filter_accepted {
|
|
|
|
let key = key_cache.remove(&document.id).expect("BUG: cached key not found");
|
|
|
|
let distinct_accepted = match key {
|
|
|
|
Some(key) => seen.register(key),
|
|
|
|
None => seen.register_without_key(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if distinct_accepted && seen.len() > range.start {
|
2019-02-02 21:22:31 +08:00
|
|
|
out_documents.push(Document::from_raw(&document));
|
2018-12-30 03:16:48 +08:00
|
|
|
if out_documents.len() == range.len() { break }
|
|
|
|
}
|
2018-11-29 00:12:24 +08:00
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:12:24 +08:00
|
|
|
out_documents
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|