2020-06-09 00:05:14 +08:00
|
|
|
mod best_proximity;
|
2020-06-05 15:48:46 +08:00
|
|
|
mod query_tokens;
|
2020-06-05 02:25:51 +08:00
|
|
|
|
|
|
|
use std::borrow::Cow;
|
2020-05-31 22:09:34 +08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::hash::BuildHasherDefault;
|
|
|
|
|
2020-05-31 23:48:13 +08:00
|
|
|
use cow_utils::CowUtils;
|
|
|
|
use fst::{IntoStreamer, Streamer};
|
2020-05-31 22:09:34 +08:00
|
|
|
use fxhash::FxHasher32;
|
2020-05-31 23:48:13 +08:00
|
|
|
use heed::types::*;
|
|
|
|
use heed::{PolyDatabase, Database};
|
2020-06-01 00:21:24 +08:00
|
|
|
use levenshtein_automata::LevenshteinAutomatonBuilder as LevBuilder;
|
2020-06-05 22:32:14 +08:00
|
|
|
use once_cell::sync::Lazy;
|
2020-05-31 23:48:13 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2020-06-05 02:25:51 +08:00
|
|
|
|
2020-06-05 15:48:46 +08:00
|
|
|
use self::query_tokens::{QueryTokens, QueryToken};
|
2020-06-09 00:05:14 +08:00
|
|
|
use self::best_proximity::BestProximity;
|
2020-05-31 22:09:34 +08:00
|
|
|
|
2020-06-05 22:32:14 +08: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-06-01 00:21:24 +08:00
|
|
|
|
2020-05-31 22:09:34 +08:00
|
|
|
pub type FastMap4<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher32>>;
|
|
|
|
pub type SmallString32 = smallstr::SmallString<[u8; 32]>;
|
|
|
|
pub type SmallVec32 = smallvec::SmallVec<[u8; 32]>;
|
|
|
|
pub type BEU32 = heed::zerocopy::U32<heed::byteorder::BE>;
|
|
|
|
pub type DocumentId = u32;
|
2020-06-05 22:32:14 +08:00
|
|
|
pub type AttributeId = u32;
|
2020-05-31 22:09:34 +08:00
|
|
|
|
2020-05-31 23:48:13 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Index {
|
|
|
|
pub main: PolyDatabase,
|
2020-06-05 22:32:14 +08:00
|
|
|
pub postings_attrs: Database<Str, ByteSlice>,
|
2020-06-06 02:12:52 +08:00
|
|
|
pub prefix_postings_attrs: Database<ByteSlice, ByteSlice>,
|
2020-06-05 22:32:14 +08:00
|
|
|
pub postings_ids: Database<ByteSlice, ByteSlice>,
|
|
|
|
pub prefix_postings_ids: Database<ByteSlice, ByteSlice>,
|
2020-05-31 23:48:13 +08: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)?;
|
2020-06-05 22:32:14 +08:00
|
|
|
let postings_attrs = env.create_database(Some("postings-attrs"))?;
|
2020-06-06 02:12:52 +08:00
|
|
|
let prefix_postings_attrs = env.create_database(Some("prefix-postings-attrs"))?;
|
2020-05-31 23:48:13 +08:00
|
|
|
let postings_ids = env.create_database(Some("postings-ids"))?;
|
2020-06-01 00:20:49 +08:00
|
|
|
let prefix_postings_ids = env.create_database(Some("prefix-postings-ids"))?;
|
2020-05-31 23:48:13 +08:00
|
|
|
let documents = env.create_database(Some("documents"))?;
|
|
|
|
|
2020-06-06 02:12:52 +08:00
|
|
|
Ok(Index { main, postings_attrs, prefix_postings_attrs, postings_ids, prefix_postings_ids, documents })
|
2020-05-31 23:48:13 +08: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()),
|
|
|
|
};
|
|
|
|
|
2020-06-05 22:32:14 +08:00
|
|
|
let (lev0, lev1, lev2) = (&LEVDIST0, &LEVDIST1, &LEVDIST2);
|
2020-05-31 23:48:13 +08:00
|
|
|
|
2020-06-05 15:48:46 +08:00
|
|
|
let words: Vec<_> = QueryTokens::new(query).collect();
|
2020-06-05 00:36:30 +08:00
|
|
|
let ends_with_whitespace = query.chars().last().map_or(false, char::is_whitespace);
|
2020-05-31 23:48:13 +08:00
|
|
|
let number_of_words = words.len();
|
2020-06-06 02:12:52 +08:00
|
|
|
let dfas = words.into_iter().enumerate().map(|(i, word)| {
|
2020-06-05 02:25:51 +08:00
|
|
|
let (word, quoted) = match word {
|
2020-06-05 15:48:46 +08:00
|
|
|
QueryToken::Free(word) => (word.cow_to_lowercase(), false),
|
|
|
|
QueryToken::Quoted(word) => (Cow::Borrowed(word), true),
|
2020-06-05 02:25:51 +08:00
|
|
|
};
|
2020-05-31 23:48:13 +08:00
|
|
|
let is_last = i + 1 == number_of_words;
|
2020-06-05 02:25:51 +08:00
|
|
|
let is_prefix = is_last && !ends_with_whitespace && !quoted;
|
2020-06-05 22:32:14 +08:00
|
|
|
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 23:48:13 +08:00
|
|
|
};
|
|
|
|
|
2020-06-05 22:32:14 +08:00
|
|
|
let dfa = if is_prefix {
|
|
|
|
lev.build_prefix_dfa(&word)
|
|
|
|
} else {
|
|
|
|
lev.build_dfa(&word)
|
|
|
|
};
|
|
|
|
|
|
|
|
(word, is_prefix, dfa)
|
2020-06-06 02:12:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut words_positions = Vec::new();
|
2020-06-09 00:05:14 +08:00
|
|
|
let mut positions = Vec::new();
|
|
|
|
|
2020-06-06 02:12:52 +08:00
|
|
|
for (word, is_prefix, dfa) in dfas {
|
|
|
|
let mut count = 0;
|
|
|
|
let mut union_positions = RoaringBitmap::default();
|
2020-06-09 00:05:14 +08:00
|
|
|
if false && word.len() <= 4 && is_prefix {
|
2020-06-06 02:12:52 +08:00
|
|
|
if let Some(ids) = self.prefix_postings_attrs.get(rtxn, word.as_bytes())? {
|
|
|
|
let right = RoaringBitmap::deserialize_from(ids)?;
|
|
|
|
union_positions.union_with(&right);
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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)? {
|
|
|
|
let right = RoaringBitmap::deserialize_from(attrs)?;
|
|
|
|
union_positions.union_with(&right);
|
|
|
|
count += 1;
|
|
|
|
}
|
2020-06-01 00:20:49 +08:00
|
|
|
}
|
2020-06-05 22:32:14 +08:00
|
|
|
}
|
|
|
|
|
2020-06-06 02:12:52 +08:00
|
|
|
eprintln!("{} words for {:?} we have found positions {:?}", count, word, union_positions);
|
2020-06-09 00:05:14 +08:00
|
|
|
words_positions.push((word, is_prefix, dfa));
|
|
|
|
positions.push(union_positions.iter().collect());
|
2020-06-05 22:32:14 +08:00
|
|
|
}
|
|
|
|
|
2020-06-09 00:05:14 +08:00
|
|
|
// let positions = BestProximity::new(positions).next().unwrap_or_default();
|
|
|
|
let _positions: Vec<Vec<u32>> = positions;
|
|
|
|
let positions = vec![0u32];
|
|
|
|
eprintln!("best proximity {:?}", positions);
|
2020-06-05 22:32:14 +08:00
|
|
|
|
|
|
|
let mut intersect_docids: Option<RoaringBitmap> = None;
|
2020-06-09 00:05:14 +08:00
|
|
|
for ((word, is_prefix, dfa), pos) in words_positions.into_iter().zip(positions) {
|
2020-06-06 02:12:52 +08:00
|
|
|
let mut count = 0;
|
|
|
|
let mut union_docids = RoaringBitmap::default();
|
|
|
|
|
2020-06-09 00:05:14 +08:00
|
|
|
if false && word.len() <= 4 && is_prefix {
|
2020-06-06 02:12:52 +08:00
|
|
|
let mut key = word.as_bytes()[..word.len().min(5)].to_vec();
|
|
|
|
key.extend_from_slice(&pos.to_be_bytes());
|
|
|
|
if let Some(ids) = self.prefix_postings_ids.get(rtxn, &key)? {
|
|
|
|
let right = RoaringBitmap::deserialize_from(ids)?;
|
|
|
|
union_docids.union_with(&right);
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut stream = fst.search(dfa).into_stream();
|
|
|
|
while let Some(word) = stream.next() {
|
|
|
|
let word = std::str::from_utf8(word)?;
|
|
|
|
let mut key = word.as_bytes().to_vec();
|
|
|
|
key.extend_from_slice(&pos.to_be_bytes());
|
|
|
|
if let Some(attrs) = self.postings_ids.get(rtxn, &key)? {
|
|
|
|
let right = RoaringBitmap::deserialize_from(attrs)?;
|
|
|
|
union_docids.union_with(&right);
|
2020-06-05 22:32:14 +08:00
|
|
|
count += 1;
|
2020-06-01 00:20:49 +08:00
|
|
|
}
|
2020-05-31 23:48:13 +08:00
|
|
|
}
|
2020-06-05 22:32:14 +08:00
|
|
|
}
|
|
|
|
|
2020-06-06 02:12:52 +08:00
|
|
|
let _ = count;
|
|
|
|
|
2020-06-05 22:32:14 +08:00
|
|
|
match &mut intersect_docids {
|
2020-06-06 02:12:52 +08:00
|
|
|
Some(left) => left.intersect_with(&union_docids),
|
|
|
|
None => intersect_docids = Some(union_docids),
|
2020-06-05 01:13:28 +08:00
|
|
|
}
|
2020-05-31 23:48:13 +08:00
|
|
|
}
|
|
|
|
|
2020-06-05 22:32:14 +08:00
|
|
|
eprintln!("{} candidates", intersect_docids.as_ref().map_or(0, |r| r.len()));
|
2020-06-01 23:52:43 +08:00
|
|
|
|
2020-06-05 22:32:14 +08:00
|
|
|
Ok(intersect_docids.unwrap_or_default().iter().take(20).collect())
|
2020-05-31 23:48:13 +08:00
|
|
|
}
|
|
|
|
}
|