MeiliSearch/src/lib.rs

327 lines
13 KiB
Rust
Raw Normal View History

mod best_proximity;
mod iter_shortest_paths;
mod query_tokens;
2020-06-04 20:25:51 +02:00
use std::borrow::Cow;
2020-05-31 16:09:34 +02:00
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
2020-06-10 21:35:01 +02:00
use std::time::Instant;
2020-05-31 16:09:34 +02:00
2020-05-31 17:48:13 +02:00
use cow_utils::CowUtils;
use fst::{IntoStreamer, Streamer};
2020-05-31 16:09:34 +02:00
use fxhash::FxHasher32;
2020-05-31 17:48:13 +02:00
use heed::types::*;
use heed::{PolyDatabase, Database};
use levenshtein_automata::LevenshteinAutomatonBuilder as LevBuilder;
use once_cell::sync::Lazy;
2020-05-31 17:48:13 +02:00
use roaring::RoaringBitmap;
2020-06-04 20:25:51 +02:00
use self::query_tokens::{QueryTokens, QueryToken};
use self::best_proximity::BestProximity;
2020-05-31 16:09:34 +02:00
// Building these factories is not free.
static LEVDIST0: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(0, true));
static LEVDIST1: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(1, true));
static LEVDIST2: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(2, true));
2020-05-31 16:09:34 +02:00
pub type FastMap4<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher32>>;
pub type SmallString32 = smallstr::SmallString<[u8; 32]>;
2020-06-11 11:55:03 +02:00
pub type SmallVec32<T> = smallvec::SmallVec<[T; 32]>;
pub type SmallVec16<T> = smallvec::SmallVec<[T; 16]>;
2020-05-31 16:09:34 +02:00
pub type BEU32 = heed::zerocopy::U32<heed::byteorder::BE>;
pub type DocumentId = u32;
pub type AttributeId = u32;
2020-05-31 16:09:34 +02:00
2020-05-31 17:48:13 +02:00
#[derive(Clone)]
pub struct Index {
pub main: PolyDatabase,
pub postings_attrs: Database<Str, ByteSlice>,
pub prefix_postings_attrs: Database<ByteSlice, ByteSlice>,
pub postings_ids: Database<ByteSlice, ByteSlice>,
pub prefix_postings_ids: Database<ByteSlice, ByteSlice>,
2020-05-31 17:48:13 +02:00
pub documents: Database<OwnedType<BEU32>, ByteSlice>,
}
impl Index {
pub fn new(env: &heed::Env) -> heed::Result<Index> {
let main = env.create_poly_database(None)?;
let postings_attrs = env.create_database(Some("postings-attrs"))?;
let prefix_postings_attrs = env.create_database(Some("prefix-postings-attrs"))?;
2020-05-31 17:48:13 +02:00
let postings_ids = env.create_database(Some("postings-ids"))?;
let prefix_postings_ids = env.create_database(Some("prefix-postings-ids"))?;
2020-05-31 17:48:13 +02:00
let documents = env.create_database(Some("documents"))?;
Ok(Index { main, postings_attrs, prefix_postings_attrs, postings_ids, prefix_postings_ids, documents })
2020-05-31 17:48:13 +02:00
}
pub fn headers<'t>(&self, rtxn: &'t heed::RoTxn) -> heed::Result<Option<&'t [u8]>> {
self.main.get::<_, Str, ByteSlice>(rtxn, "headers")
}
pub fn search(&self, rtxn: &heed::RoTxn, query: &str) -> anyhow::Result<Vec<DocumentId>> {
let fst = match self.main.get::<_, Str, ByteSlice>(rtxn, "words-fst")? {
Some(bytes) => fst::Set::new(bytes)?,
None => return Ok(Vec::new()),
};
let (lev0, lev1, lev2) = (&LEVDIST0, &LEVDIST1, &LEVDIST2);
2020-05-31 17:48:13 +02:00
let words: Vec<_> = QueryTokens::new(query).collect();
let ends_with_whitespace = query.chars().last().map_or(false, char::is_whitespace);
2020-05-31 17:48:13 +02:00
let number_of_words = words.len();
let dfas = words.into_iter().enumerate().map(|(i, word)| {
2020-06-04 20:25:51 +02:00
let (word, quoted) = match word {
QueryToken::Free(word) => (word.cow_to_lowercase(), false),
QueryToken::Quoted(word) => (Cow::Borrowed(word), true),
2020-06-04 20:25:51 +02:00
};
2020-05-31 17:48:13 +02:00
let is_last = i + 1 == number_of_words;
2020-06-04 20:25:51 +02:00
let is_prefix = is_last && !ends_with_whitespace && !quoted;
let lev = match word.len() {
0..=4 => if quoted { lev0 } else { lev0 },
5..=8 => if quoted { lev0 } else { lev1 },
_ => if quoted { lev0 } else { lev2 },
2020-05-31 17:48:13 +02:00
};
let dfa = if is_prefix {
lev.build_prefix_dfa(&word)
} else {
lev.build_dfa(&word)
};
(word, is_prefix, dfa)
});
2020-06-11 11:55:03 +02:00
let mut words = Vec::new();
let mut positions = Vec::new();
2020-06-10 21:35:01 +02:00
let before = Instant::now();
2020-06-11 11:55:03 +02:00
for (word, _is_prefix, dfa) in dfas {
let before = Instant::now();
let mut count = 0;
let mut union_positions = RoaringBitmap::default();
2020-06-11 11:55:03 +02:00
let mut derived_words = Vec::new();
// TODO re-enable the prefixes system
let mut stream = fst.search(&dfa).into_stream();
while let Some(word) = stream.next() {
let word = std::str::from_utf8(word)?;
if let Some(attrs) = self.postings_attrs.get(rtxn, word)? {
2020-06-18 18:37:57 +02:00
let right = RoaringBitmap::deserialize_from_slice(attrs)?;
union_positions.union_with(&right);
derived_words.push((word.as_bytes().to_vec(), right));
2020-06-11 11:55:03 +02:00
count += 1;
}
}
eprintln!("{} words for {:?} we have found positions {:?} in {:.02?}",
count, word, union_positions, before.elapsed());
2020-06-11 11:55:03 +02:00
words.push(derived_words);
positions.push(union_positions.iter().collect());
}
2020-06-10 21:35:01 +02:00
eprintln!("Retrieving words positions took {:.02?}", before.elapsed());
let mut documents = Vec::new();
let mut debug_intersects = HashMap::new();
let mut intersect_cache = HashMap::new();
2020-06-18 18:37:57 +02:00
let mut lunion_docids = RoaringBitmap::default();
let mut runion_docids = RoaringBitmap::default();
2020-06-16 12:10:23 +02:00
let contains_documents = |(lword, lpos): (usize, u32), (rword, rpos): (usize, u32)| {
let proximity = best_proximity::positions_proximity(lpos, rpos);
if proximity == 0 { return false }
2020-06-16 12:10:23 +02:00
*intersect_cache.entry(((lword, lpos), (rword, rpos))).or_insert_with(|| {
let (nb_words, nb_docs_intersect, lnblookups, lnbbitmaps, rnblookups, rnbbitmaps) =
debug_intersects.entry((lword, lpos, rword, rpos, proximity)).or_default();
2020-06-16 12:10:23 +02:00
let left = &words[lword];
let right = &words[rword];
*nb_words = left.len() + right.len();
2020-06-16 12:10:23 +02:00
let mut l_lookups = 0;
let mut l_bitmaps = 0;
let mut r_lookups = 0;
let mut r_bitmaps = 0;
2020-06-18 18:37:57 +02:00
// This for the left word
lunion_docids.clear();
for (word, attrs) in left {
2020-06-18 18:37:57 +02:00
if attrs.contains(lpos) {
l_lookups += 1;
let mut key = word.clone();
key.extend_from_slice(&lpos.to_be_bytes());
if let Some(attrs) = self.postings_ids.get(rtxn, &key).unwrap() {
l_bitmaps += 1;
let right = RoaringBitmap::deserialize_from_slice(attrs).unwrap();
lunion_docids.union_with(&right);
}
}
2020-06-18 18:37:57 +02:00
}
2020-06-18 18:37:57 +02:00
// This for the right word
runion_docids.clear();
for (word, attrs) in right {
2020-06-18 18:37:57 +02:00
if attrs.contains(rpos) {
r_lookups += 1;
let mut key = word.clone();
key.extend_from_slice(&rpos.to_be_bytes());
if let Some(attrs) = self.postings_ids.get(rtxn, &key).unwrap() {
r_bitmaps += 1;
let right = RoaringBitmap::deserialize_from_slice(attrs).unwrap();
runion_docids.union_with(&right);
}
}
}
2020-06-11 11:55:03 +02:00
2020-06-18 18:37:57 +02:00
let intersect_docids = &mut lunion_docids;
intersect_docids.intersect_with(&runion_docids);
*lnblookups = l_lookups;
*lnbbitmaps = l_bitmaps;
*rnblookups = r_lookups;
*rnbbitmaps = r_bitmaps;
*nb_docs_intersect += intersect_docids.len();
2020-06-16 12:10:23 +02:00
2020-06-18 18:37:57 +02:00
!intersect_docids.is_empty()
})
};
for (proximity, mut positions) in BestProximity::new(positions, contains_documents) {
2020-06-11 11:55:03 +02:00
positions.sort_unstable();
2020-06-10 21:35:01 +02:00
let same_prox_before = Instant::now();
let mut same_proximity_union = RoaringBitmap::default();
for positions in positions {
2020-06-10 21:35:01 +02:00
let before = Instant::now();
let mut intersect_docids: Option<RoaringBitmap> = None;
for (derived_words, pos) in words.iter().zip(positions.clone()) {
let mut count = 0;
let mut union_docids = RoaringBitmap::default();
2020-06-10 21:35:01 +02:00
let before = Instant::now();
// TODO re-enable the prefixes system
for (word, attrs) in derived_words.iter() {
if attrs.contains(pos) {
let mut key = word.clone();
key.extend_from_slice(&pos.to_be_bytes());
if let Some(attrs) = self.postings_ids.get(rtxn, &key)? {
2020-06-18 18:37:57 +02:00
let right = RoaringBitmap::deserialize_from_slice(attrs)?;
union_docids.union_with(&right);
count += 1;
}
}
}
2020-06-10 21:35:01 +02:00
let before_intersect = Instant::now();
match &mut intersect_docids {
Some(left) => left.intersect_with(&union_docids),
None => intersect_docids = Some(union_docids),
}
2020-06-10 21:35:01 +02:00
eprintln!("retrieving {} word took {:.02?} and took {:.02?} to intersect",
count, before.elapsed(), before_intersect.elapsed());
2020-05-31 17:48:13 +02:00
}
2020-06-10 21:35:01 +02:00
eprintln!("for proximity {:?} {:?} we took {:.02?} to find {} documents",
proximity, positions, before.elapsed(),
intersect_docids.as_ref().map_or(0, |rb| rb.len()));
if let Some(intersect_docids) = intersect_docids {
same_proximity_union.union_with(&intersect_docids);
}
2020-06-11 11:55:03 +02:00
// We found enough documents we can stop here
if documents.iter().map(RoaringBitmap::len).sum::<u64>() + same_proximity_union.len() >= 20 {
eprintln!("proximity {} took a total of {:.02?}", proximity, same_prox_before.elapsed());
break;
}
}
2020-06-10 21:35:01 +02:00
documents.push(same_proximity_union);
2020-06-11 11:55:03 +02:00
// We remove the double occurences of documents.
for i in 0..documents.len() {
if let Some((docs, others)) = documents[..=i].split_last_mut() {
others.iter().for_each(|other| docs.difference_with(other));
}
}
documents.retain(|rb| !rb.is_empty());
eprintln!("documents: {:?}", documents);
eprintln!("proximity {} took a total of {:.02?}", proximity, same_prox_before.elapsed());
// We found enough documents we can stop here.
if documents.iter().map(RoaringBitmap::len).sum::<u64>() >= 20 {
2020-06-11 11:55:03 +02:00
break;
2020-06-04 19:13:28 +02:00
}
2020-05-31 17:48:13 +02:00
}
if cfg!(feature = "intersect-to-csv") {
debug_intersects_to_csv(debug_intersects);
}
2020-06-16 12:10:23 +02:00
eprintln!("{} candidates", documents.iter().map(RoaringBitmap::len).sum::<u64>());
Ok(documents.iter().flatten().take(20).collect())
2020-05-31 17:48:13 +02:00
}
}
2020-06-16 12:10:23 +02:00
fn debug_intersects_to_csv(intersects: HashMap<(usize, u32, usize, u32, u32), (usize, u64, usize, usize, usize, usize)>) {
let mut wrt = csv::Writer::from_path("intersects-stats.csv").unwrap();
wrt.write_record(&[
"proximity",
"lword",
"lpos",
"rword",
"rpos",
"nb_derived_words",
"nb_docs_intersect",
"lnblookups",
"lnbbitmaps",
"rnblookups",
"rnbbitmaps",
]).unwrap();
for ((lword, lpos, rword, rpos, proximity), vals) in intersects {
let (
nb_derived_words,
nb_docs_intersect,
lnblookups,
lnbbitmaps,
rnblookups,
rnbbitmaps,
) = vals;
let proximity = proximity.to_string();
let lword = lword.to_string();
let lpos = lpos.to_string();
let rword = rword.to_string();
let rpos = rpos.to_string();
let nb_derived_words = nb_derived_words.to_string();
let nb_docs_intersect = nb_docs_intersect.to_string();
let lnblookups = lnblookups.to_string();
let lnbbitmaps = lnbbitmaps.to_string();
let rnblookups = rnblookups.to_string();
let rnbbitmaps = rnbbitmaps.to_string();
wrt.write_record(&[
&proximity,
&lword,
&lpos,
&rword,
&rpos,
&nb_derived_words,
&nb_docs_intersect,
&lnblookups,
&lnbbitmaps,
&rnblookups,
&rnbbitmaps,
]).unwrap();
}
}