2021-02-17 15:27:35 +01:00
|
|
|
use std::borrow::Cow;
|
2021-06-16 18:33:33 +02:00
|
|
|
use std::collections::HashMap;
|
2021-02-17 15:27:35 +01:00
|
|
|
|
2021-02-25 16:34:29 +01:00
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2021-03-02 11:58:32 +01:00
|
|
|
use self::asc_desc::AscDesc;
|
2021-03-11 11:48:55 +01:00
|
|
|
use self::attribute::Attribute;
|
2021-05-04 13:44:55 +02:00
|
|
|
use self::exactness::Exactness;
|
2021-03-23 15:25:46 +01:00
|
|
|
use self::initial::Initial;
|
|
|
|
use self::proximity::Proximity;
|
2021-06-16 18:33:33 +02:00
|
|
|
use self::r#final::Final;
|
2021-03-23 15:25:46 +01:00
|
|
|
use self::typo::Typo;
|
|
|
|
use self::words::Words;
|
2021-06-16 18:33:33 +02:00
|
|
|
use super::query_tree::{Operation, PrimitiveQueryPart, Query, QueryKind};
|
2021-09-01 15:14:23 +02:00
|
|
|
use crate::search::criteria::geo::Geo;
|
2022-07-18 16:52:45 +02:00
|
|
|
use crate::search::{word_derivations, Distinct, WordDerivationsCache};
|
2021-10-05 11:18:42 +02:00
|
|
|
use crate::{AscDesc as AscDescName, DocumentId, FieldId, Index, Member, Result};
|
2021-02-19 15:14:00 +01:00
|
|
|
|
2021-03-09 12:04:52 +01:00
|
|
|
mod asc_desc;
|
2021-03-11 11:48:55 +01:00
|
|
|
mod attribute;
|
2021-05-04 13:44:55 +02:00
|
|
|
mod exactness;
|
2021-06-16 18:33:33 +02:00
|
|
|
pub mod r#final;
|
2021-09-01 17:03:42 +02:00
|
|
|
mod geo;
|
2021-03-23 15:25:46 +01:00
|
|
|
mod initial;
|
|
|
|
mod proximity;
|
|
|
|
mod typo;
|
|
|
|
mod words;
|
2021-02-19 15:14:00 +01:00
|
|
|
|
|
|
|
pub trait Criterion {
|
2021-06-14 16:46:19 +02:00
|
|
|
fn next(&mut self, params: &mut CriterionParameters) -> Result<Option<CriterionResult>>;
|
2021-02-17 17:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The result of a call to the parent criterion.
|
2021-02-24 10:25:22 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2021-02-17 17:34:22 +01:00
|
|
|
pub struct CriterionResult {
|
|
|
|
/// The query tree that must be used by the children criterion to fetch candidates.
|
2021-03-09 12:04:52 +01:00
|
|
|
query_tree: Option<Operation>,
|
|
|
|
/// The candidates that this criterion is allowed to return subsets of,
|
|
|
|
/// if None, it is up to the child to compute the candidates itself.
|
|
|
|
candidates: Option<RoaringBitmap>,
|
2021-05-10 12:33:37 +02:00
|
|
|
/// The candidates, coming from facet filters, that this criterion is allowed to return subsets of.
|
|
|
|
filtered_candidates: Option<RoaringBitmap>,
|
2021-02-17 17:34:22 +01:00
|
|
|
/// Candidates that comes from the current bucket of the initial criterion.
|
2021-05-05 20:46:56 +02:00
|
|
|
bucket_candidates: Option<RoaringBitmap>,
|
2021-02-19 15:14:00 +01:00
|
|
|
}
|
|
|
|
|
2021-04-28 18:01:23 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct CriterionParameters<'a> {
|
|
|
|
wdcache: &'a mut WordDerivationsCache,
|
|
|
|
excluded_candidates: &'a RoaringBitmap,
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:14:00 +01:00
|
|
|
/// Either a set of candidates that defines the candidates
|
|
|
|
/// that are allowed to be returned,
|
|
|
|
/// or the candidates that must never be returned.
|
2021-02-19 15:45:15 +01:00
|
|
|
#[derive(Debug)]
|
2021-02-19 15:14:00 +01:00
|
|
|
enum Candidates {
|
|
|
|
Allowed(RoaringBitmap),
|
2021-06-16 18:33:33 +02:00
|
|
|
Forbidden(RoaringBitmap),
|
2021-02-19 15:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Candidates {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Forbidden(RoaringBitmap::new())
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 15:25:46 +01:00
|
|
|
|
2021-03-31 19:23:02 +02:00
|
|
|
pub trait Context<'c> {
|
2021-02-25 16:34:29 +01:00
|
|
|
fn documents_ids(&self) -> heed::Result<RoaringBitmap>;
|
2021-02-17 18:16:21 +01:00
|
|
|
fn word_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>>;
|
2022-03-24 19:25:11 +01:00
|
|
|
fn exact_word_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>>;
|
2021-02-17 18:16:21 +01:00
|
|
|
fn word_prefix_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>>;
|
2022-03-25 16:27:48 +01:00
|
|
|
fn exact_word_prefix_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>>;
|
2022-09-15 09:34:35 +02:00
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn word_pair_proximity_docids(
|
|
|
|
&self,
|
|
|
|
left: &str,
|
|
|
|
right: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>>;
|
|
|
|
fn word_prefix_pair_proximity_docids(
|
|
|
|
&self,
|
|
|
|
left: &str,
|
|
|
|
right: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>>;
|
2022-09-15 09:34:35 +02:00
|
|
|
fn prefix_word_pair_proximity_docids(
|
|
|
|
&self,
|
|
|
|
prefix: &str,
|
|
|
|
right: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>>;
|
2021-02-17 15:27:35 +01:00
|
|
|
fn words_fst<'t>(&self) -> &'t fst::Set<Cow<[u8]>>;
|
2021-02-17 18:16:21 +01:00
|
|
|
fn in_prefix_cache(&self, word: &str) -> bool;
|
2021-06-16 18:33:33 +02:00
|
|
|
fn docid_words_positions(
|
|
|
|
&self,
|
|
|
|
docid: DocumentId,
|
|
|
|
) -> heed::Result<HashMap<String, RoaringBitmap>>;
|
|
|
|
fn word_position_iterator(
|
|
|
|
&self,
|
|
|
|
word: &str,
|
|
|
|
in_prefix_cache: bool,
|
2021-10-05 11:18:42 +02:00
|
|
|
) -> heed::Result<Box<dyn Iterator<Item = heed::Result<((&'c str, u32), RoaringBitmap)>> + 'c>>;
|
2021-05-04 13:44:55 +02:00
|
|
|
fn synonyms(&self, word: &str) -> heed::Result<Option<Vec<Vec<String>>>>;
|
2021-06-16 18:33:33 +02:00
|
|
|
fn searchable_fields_ids(&self) -> Result<Vec<FieldId>>;
|
|
|
|
fn field_id_word_count_docids(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
word_count: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>>;
|
2021-10-05 11:18:42 +02:00
|
|
|
fn word_position_docids(&self, word: &str, pos: u32) -> heed::Result<Option<RoaringBitmap>>;
|
2021-02-17 15:27:35 +01:00
|
|
|
}
|
2021-06-14 16:46:19 +02:00
|
|
|
|
2021-03-02 11:58:32 +01:00
|
|
|
pub struct CriteriaBuilder<'t> {
|
2021-02-17 15:27:35 +01:00
|
|
|
rtxn: &'t heed::RoTxn<'t>,
|
|
|
|
index: &'t Index,
|
|
|
|
words_fst: fst::Set<Cow<'t, [u8]>>,
|
2021-02-17 15:49:44 +01:00
|
|
|
words_prefixes_fst: fst::Set<Cow<'t, [u8]>>,
|
2021-02-17 15:27:35 +01:00
|
|
|
}
|
|
|
|
|
2022-09-15 09:34:35 +02:00
|
|
|
/// Return the docids for the following word pairs and proximities using [`Context::word_pair_proximity_docids`].
|
|
|
|
/// * `left, right, prox` (leftward proximity)
|
|
|
|
/// * `right, left, prox-1` (rightward proximity)
|
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
/// For a document with the text `the good fox eats the apple`, we have:
|
|
|
|
/// * `rightward_proximity(the, eats) = 3`
|
|
|
|
/// * `leftward_proximity(eats, the) = 1`
|
|
|
|
///
|
|
|
|
/// So both the expressions `word_pair_overall_proximity_docids(ctx, the, eats, 3)`
|
|
|
|
/// and `word_pair_overall_proximity_docids(ctx, the, eats, 2)` would return a bitmap containing
|
|
|
|
/// the id of this document.
|
|
|
|
fn word_pair_overall_proximity_docids(
|
|
|
|
ctx: &dyn Context,
|
|
|
|
left: &str,
|
|
|
|
right: &str,
|
|
|
|
prox: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
let rightward = ctx.word_pair_proximity_docids(left, right, prox)?;
|
|
|
|
let leftward =
|
|
|
|
if prox > 1 { ctx.word_pair_proximity_docids(right, left, prox - 1)? } else { None };
|
|
|
|
if let Some(mut all) = rightward {
|
|
|
|
if let Some(leftward) = leftward {
|
|
|
|
all |= leftward;
|
|
|
|
}
|
|
|
|
Ok(Some(all))
|
|
|
|
} else {
|
|
|
|
Ok(leftward)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function works identically to [`word_pair_overall_proximity_docids`] except that the
|
|
|
|
/// right word is replaced by a prefix string.
|
|
|
|
///
|
|
|
|
/// It will return None if no documents were found or if the prefix does not exist in the
|
|
|
|
/// `word_prefix_pair_proximity_docids` database.
|
|
|
|
fn word_prefix_pair_overall_proximity_docids(
|
|
|
|
ctx: &dyn Context,
|
|
|
|
left: &str,
|
|
|
|
prefix: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
// We retrieve the docids for the original and swapped word pairs:
|
|
|
|
// A: word1 prefix2 proximity
|
|
|
|
// B: prefix2 word1 proximity-1
|
|
|
|
let rightward = ctx.word_prefix_pair_proximity_docids(left, prefix, proximity)?;
|
|
|
|
|
|
|
|
let leftward = if proximity > 1 {
|
|
|
|
ctx.prefix_word_pair_proximity_docids(prefix, left, proximity - 1)?
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some(mut all) = rightward {
|
|
|
|
if let Some(leftward) = leftward {
|
|
|
|
all |= leftward;
|
|
|
|
}
|
|
|
|
Ok(Some(all))
|
|
|
|
} else {
|
|
|
|
Ok(leftward)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:23:02 +02:00
|
|
|
impl<'c> Context<'c> for CriteriaBuilder<'c> {
|
2021-02-25 16:34:29 +01:00
|
|
|
fn documents_ids(&self) -> heed::Result<RoaringBitmap> {
|
|
|
|
self.index.documents_ids(self.rtxn)
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:16:21 +01:00
|
|
|
fn word_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
self.index.word_docids.get(self.rtxn, &word)
|
2021-02-17 15:27:35 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 19:25:11 +01:00
|
|
|
fn exact_word_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
self.index.exact_word_docids.get(self.rtxn, &word)
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:16:21 +01:00
|
|
|
fn word_prefix_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
self.index.word_prefix_docids.get(self.rtxn, &word)
|
|
|
|
}
|
|
|
|
|
2022-03-25 16:27:48 +01:00
|
|
|
fn exact_word_prefix_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
self.index.exact_word_prefix_docids.get(self.rtxn, &word)
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn word_pair_proximity_docids(
|
|
|
|
&self,
|
|
|
|
left: &str,
|
|
|
|
right: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
2022-09-15 09:34:35 +02:00
|
|
|
self.index.word_pair_proximity_docids.get(self.rtxn, &(proximity, left, right))
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn word_prefix_pair_proximity_docids(
|
|
|
|
&self,
|
|
|
|
left: &str,
|
2022-09-15 09:34:35 +02:00
|
|
|
prefix: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
self.index.word_prefix_pair_proximity_docids.get(self.rtxn, &(proximity, left, prefix))
|
|
|
|
}
|
|
|
|
fn prefix_word_pair_proximity_docids(
|
|
|
|
&self,
|
|
|
|
prefix: &str,
|
2021-06-16 18:33:33 +02:00
|
|
|
right: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
2022-09-15 09:34:35 +02:00
|
|
|
self.index.prefix_word_pair_proximity_docids.get(self.rtxn, &(proximity, prefix, right))
|
2021-02-17 15:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn words_fst<'t>(&self) -> &'t fst::Set<Cow<[u8]>> {
|
|
|
|
&self.words_fst
|
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
|
|
|
|
fn in_prefix_cache(&self, word: &str) -> bool {
|
|
|
|
self.words_prefixes_fst.contains(word)
|
|
|
|
}
|
2021-03-03 15:41:09 +01:00
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn docid_words_positions(
|
|
|
|
&self,
|
|
|
|
docid: DocumentId,
|
|
|
|
) -> heed::Result<HashMap<String, RoaringBitmap>> {
|
2021-03-09 17:48:05 +01:00
|
|
|
let mut words_positions = HashMap::new();
|
|
|
|
for result in self.index.docid_word_positions.prefix_iter(self.rtxn, &(docid, ""))? {
|
|
|
|
let ((_, word), positions) = result?;
|
|
|
|
words_positions.insert(word.to_string(), positions);
|
|
|
|
}
|
|
|
|
Ok(words_positions)
|
2021-03-03 15:41:09 +01:00
|
|
|
}
|
2021-03-31 19:23:02 +02:00
|
|
|
|
2021-04-26 11:30:42 +02:00
|
|
|
fn word_position_iterator(
|
|
|
|
&self,
|
|
|
|
word: &str,
|
|
|
|
in_prefix_cache: bool,
|
2021-10-05 11:18:42 +02:00
|
|
|
) -> heed::Result<Box<dyn Iterator<Item = heed::Result<((&'c str, u32), RoaringBitmap)>> + 'c>>
|
|
|
|
{
|
2021-03-31 19:23:02 +02:00
|
|
|
let range = {
|
2021-10-05 11:18:42 +02:00
|
|
|
let left = u32::min_value();
|
|
|
|
let right = u32::max_value();
|
|
|
|
let left = (word, left);
|
|
|
|
let right = (word, right);
|
2021-03-31 19:23:02 +02:00
|
|
|
left..=right
|
|
|
|
};
|
|
|
|
let db = match in_prefix_cache {
|
2021-10-05 11:18:42 +02:00
|
|
|
true => self.index.word_prefix_position_docids,
|
|
|
|
false => self.index.word_position_docids,
|
2021-03-31 19:23:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Box::new(db.range(self.rtxn, &range)?))
|
|
|
|
}
|
|
|
|
|
2021-05-04 13:44:55 +02:00
|
|
|
fn synonyms(&self, word: &str) -> heed::Result<Option<Vec<Vec<String>>>> {
|
|
|
|
self.index.words_synonyms(self.rtxn, &[word])
|
|
|
|
}
|
|
|
|
|
2021-06-15 11:51:32 +02:00
|
|
|
fn searchable_fields_ids(&self) -> Result<Vec<FieldId>> {
|
2021-05-04 13:44:55 +02:00
|
|
|
match self.index.searchable_fields_ids(self.rtxn)? {
|
|
|
|
Some(searchable_fields_ids) => Ok(searchable_fields_ids),
|
|
|
|
None => Ok(self.index.fields_ids_map(self.rtxn)?.ids().collect()),
|
|
|
|
}
|
2021-05-04 15:28:05 +02:00
|
|
|
}
|
2021-05-04 13:44:55 +02:00
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn field_id_word_count_docids(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
word_count: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
2021-05-27 15:32:14 +02:00
|
|
|
let key = (field_id, word_count);
|
|
|
|
self.index.field_id_word_count_docids.get(self.rtxn, &key)
|
2021-05-04 13:44:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 11:18:42 +02:00
|
|
|
fn word_position_docids(&self, word: &str, pos: u32) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
let key = (word, pos);
|
|
|
|
self.index.word_position_docids.get(self.rtxn, &key)
|
2021-05-04 13:44:55 +02:00
|
|
|
}
|
2021-02-17 15:27:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 11:58:32 +01:00
|
|
|
impl<'t> CriteriaBuilder<'t> {
|
2021-06-14 16:46:19 +02:00
|
|
|
pub fn new(rtxn: &'t heed::RoTxn<'t>, index: &'t Index) -> Result<Self> {
|
2021-02-17 15:27:35 +01:00
|
|
|
let words_fst = index.words_fst(rtxn)?;
|
2021-02-17 15:49:44 +01:00
|
|
|
let words_prefixes_fst = index.words_prefixes_fst(rtxn)?;
|
2021-03-02 11:58:32 +01:00
|
|
|
Ok(Self { rtxn, index, words_fst, words_prefixes_fst })
|
|
|
|
}
|
2021-02-17 15:27:35 +01:00
|
|
|
|
2022-07-18 16:52:45 +02:00
|
|
|
pub fn build<D: 't + Distinct>(
|
2021-03-02 11:58:32 +01:00
|
|
|
&'t self,
|
2021-03-23 15:25:46 +01:00
|
|
|
query_tree: Option<Operation>,
|
2021-05-04 13:44:55 +02:00
|
|
|
primitive_query: Option<Vec<PrimitiveQueryPart>>,
|
2021-06-02 16:30:56 +02:00
|
|
|
filtered_candidates: Option<RoaringBitmap>,
|
2021-08-23 11:37:18 +02:00
|
|
|
sort_criteria: Option<Vec<AscDescName>>,
|
2022-07-12 17:56:50 +02:00
|
|
|
exhaustive_number_hits: bool,
|
2022-07-18 16:52:45 +02:00
|
|
|
distinct: Option<D>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<Final<'t>> {
|
2021-08-17 15:36:03 +02:00
|
|
|
use crate::criterion::Criterion as Name;
|
2021-03-02 11:58:32 +01:00
|
|
|
|
2021-05-04 13:44:55 +02:00
|
|
|
let primitive_query = primitive_query.unwrap_or_default();
|
|
|
|
|
2022-07-18 16:52:45 +02:00
|
|
|
let mut criterion = Box::new(Initial::new(
|
|
|
|
self,
|
|
|
|
query_tree,
|
|
|
|
filtered_candidates,
|
|
|
|
exhaustive_number_hits,
|
|
|
|
distinct,
|
|
|
|
)) as Box<dyn Criterion>;
|
2021-03-02 11:58:32 +01:00
|
|
|
for name in self.index.criteria(&self.rtxn)? {
|
2021-03-23 15:25:46 +01:00
|
|
|
criterion = match name {
|
2021-08-20 18:09:17 +02:00
|
|
|
Name::Words => Box::new(Words::new(self, criterion)),
|
2021-08-23 11:37:18 +02:00
|
|
|
Name::Typo => Box::new(Typo::new(self, criterion)),
|
|
|
|
Name::Sort => match sort_criteria {
|
|
|
|
Some(ref sort_criteria) => {
|
|
|
|
for asc_desc in sort_criteria {
|
|
|
|
criterion = match asc_desc {
|
2021-09-01 17:03:42 +02:00
|
|
|
AscDescName::Asc(Member::Field(field)) => Box::new(AscDesc::asc(
|
|
|
|
&self.index,
|
|
|
|
&self.rtxn,
|
|
|
|
criterion,
|
|
|
|
field.to_string(),
|
|
|
|
)?),
|
|
|
|
AscDescName::Desc(Member::Field(field)) => Box::new(AscDesc::desc(
|
|
|
|
&self.index,
|
|
|
|
&self.rtxn,
|
|
|
|
criterion,
|
|
|
|
field.to_string(),
|
|
|
|
)?),
|
2021-09-13 14:27:14 +02:00
|
|
|
AscDescName::Asc(Member::Geo(point)) => Box::new(Geo::asc(
|
|
|
|
&self.index,
|
|
|
|
&self.rtxn,
|
|
|
|
criterion,
|
|
|
|
point.clone(),
|
|
|
|
)?),
|
|
|
|
AscDescName::Desc(Member::Geo(point)) => Box::new(Geo::desc(
|
2021-09-01 17:03:42 +02:00
|
|
|
&self.index,
|
|
|
|
&self.rtxn,
|
|
|
|
criterion,
|
|
|
|
point.clone(),
|
|
|
|
)?),
|
2021-08-23 11:37:18 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
criterion
|
|
|
|
}
|
|
|
|
None => criterion,
|
|
|
|
},
|
2021-03-23 15:25:46 +01:00
|
|
|
Name::Proximity => Box::new(Proximity::new(self, criterion)),
|
|
|
|
Name::Attribute => Box::new(Attribute::new(self, criterion)),
|
2021-05-04 13:44:55 +02:00
|
|
|
Name::Exactness => Box::new(Exactness::new(self, criterion, &primitive_query)?),
|
2021-06-16 18:33:33 +02:00
|
|
|
Name::Asc(field) => {
|
2021-09-01 15:14:23 +02:00
|
|
|
Box::new(AscDesc::asc(&self.index, &self.rtxn, criterion, field)?)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
|
|
|
Name::Desc(field) => {
|
2021-09-01 15:14:23 +02:00
|
|
|
Box::new(AscDesc::desc(&self.index, &self.rtxn, criterion, field)?)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-03-23 15:25:46 +01:00
|
|
|
};
|
2021-03-02 11:58:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 15:25:46 +01:00
|
|
|
Ok(Final::new(self, criterion))
|
2021-02-17 15:27:35 +01:00
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
2021-02-17 15:49:44 +01:00
|
|
|
|
2022-08-17 15:20:43 +02:00
|
|
|
pub fn resolve_query_tree(
|
|
|
|
ctx: &dyn Context,
|
2021-02-25 16:34:29 +01:00
|
|
|
query_tree: &Operation,
|
2021-03-05 11:02:24 +01:00
|
|
|
wdcache: &mut WordDerivationsCache,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<RoaringBitmap> {
|
2022-08-17 15:20:43 +02:00
|
|
|
fn resolve_operation(
|
|
|
|
ctx: &dyn Context,
|
2021-02-25 16:34:29 +01:00
|
|
|
query_tree: &Operation,
|
2021-03-05 11:02:24 +01:00
|
|
|
wdcache: &mut WordDerivationsCache,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<RoaringBitmap> {
|
|
|
|
use Operation::{And, Or, Phrase, Query};
|
2021-02-25 16:34:29 +01:00
|
|
|
|
|
|
|
match query_tree {
|
|
|
|
And(ops) => {
|
2021-06-16 18:33:33 +02:00
|
|
|
let mut ops = ops
|
|
|
|
.iter()
|
|
|
|
.map(|op| resolve_operation(ctx, op, wdcache))
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
2021-02-25 16:34:29 +01:00
|
|
|
|
|
|
|
ops.sort_unstable_by_key(|cds| cds.len());
|
|
|
|
|
|
|
|
let mut candidates = RoaringBitmap::new();
|
|
|
|
let mut first_loop = true;
|
|
|
|
for docids in ops {
|
|
|
|
if first_loop {
|
|
|
|
candidates = docids;
|
|
|
|
first_loop = false;
|
|
|
|
} else {
|
2021-06-30 14:12:56 +02:00
|
|
|
candidates &= &docids;
|
2021-02-25 16:34:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(candidates)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2022-08-08 10:37:31 +02:00
|
|
|
Phrase(words) => resolve_phrase(ctx, &words),
|
2021-02-25 16:34:29 +01:00
|
|
|
Or(_, ops) => {
|
|
|
|
let mut candidates = RoaringBitmap::new();
|
|
|
|
for op in ops {
|
2021-05-05 20:46:56 +02:00
|
|
|
let docids = resolve_operation(ctx, op, wdcache)?;
|
2021-06-30 14:12:56 +02:00
|
|
|
candidates |= docids;
|
2021-02-25 16:34:29 +01:00
|
|
|
}
|
|
|
|
Ok(candidates)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-03-05 11:02:24 +01:00
|
|
|
Query(q) => Ok(query_docids(ctx, q, wdcache)?),
|
2021-02-25 16:34:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:46:56 +02:00
|
|
|
resolve_operation(ctx, query_tree, wdcache)
|
2021-02-25 16:34:29 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 15:20:43 +02:00
|
|
|
pub fn resolve_phrase(ctx: &dyn Context, phrase: &[String]) -> Result<RoaringBitmap> {
|
2022-08-08 10:37:31 +02:00
|
|
|
let mut candidates = RoaringBitmap::new();
|
|
|
|
let mut first_iter = true;
|
2022-09-19 15:59:05 +02:00
|
|
|
let winsize = phrase.len().min(3);
|
2022-08-08 10:37:31 +02:00
|
|
|
for win in phrase.windows(winsize) {
|
|
|
|
// Get all the documents with the matching distance for each word pairs.
|
|
|
|
let mut bitmaps = Vec::with_capacity(winsize.pow(2));
|
|
|
|
for (offset, s1) in win.iter().enumerate() {
|
|
|
|
for (dist, s2) in win.iter().skip(offset + 1).enumerate() {
|
2022-09-15 09:34:35 +02:00
|
|
|
if dist == 0 {
|
|
|
|
match ctx.word_pair_proximity_docids(s1, s2, 1)? {
|
|
|
|
Some(m) => bitmaps.push(m),
|
|
|
|
// If there are no document for this pair, there will be no
|
|
|
|
// results for the phrase query.
|
|
|
|
None => return Ok(RoaringBitmap::new()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut bitmap = RoaringBitmap::new();
|
|
|
|
for dist in 0..=dist {
|
2022-10-20 09:49:37 +02:00
|
|
|
if let Some(m) = ctx.word_pair_proximity_docids(s1, s2, dist as u8 + 1)? {
|
|
|
|
bitmap |= m
|
2022-09-15 09:34:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if bitmap.is_empty() {
|
|
|
|
return Ok(bitmap);
|
|
|
|
} else {
|
|
|
|
bitmaps.push(bitmap);
|
|
|
|
}
|
2022-08-08 10:37:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We sort the bitmaps so that we perform the small intersections first, which is faster.
|
|
|
|
bitmaps.sort_unstable_by(|a, b| a.len().cmp(&b.len()));
|
|
|
|
|
|
|
|
for bitmap in bitmaps {
|
|
|
|
if first_iter {
|
|
|
|
candidates = bitmap;
|
|
|
|
first_iter = false;
|
|
|
|
} else {
|
|
|
|
candidates &= bitmap;
|
|
|
|
}
|
|
|
|
// There will be no match, return early
|
|
|
|
if candidates.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(candidates)
|
|
|
|
}
|
|
|
|
|
2022-09-15 09:34:35 +02:00
|
|
|
fn all_word_pair_overall_proximity_docids<T: AsRef<str>, U: AsRef<str>>(
|
2021-02-17 18:16:21 +01:00
|
|
|
ctx: &dyn Context,
|
|
|
|
left_words: &[(T, u8)],
|
|
|
|
right_words: &[(U, u8)],
|
2021-06-16 18:33:33 +02:00
|
|
|
proximity: u8,
|
|
|
|
) -> Result<RoaringBitmap> {
|
2021-02-17 18:16:21 +01:00
|
|
|
let mut docids = RoaringBitmap::new();
|
|
|
|
for (left, _l_typo) in left_words {
|
|
|
|
for (right, _r_typo) in right_words {
|
2022-09-15 09:34:35 +02:00
|
|
|
let current_docids =
|
|
|
|
word_pair_overall_proximity_docids(ctx, left.as_ref(), right.as_ref(), proximity)?
|
|
|
|
.unwrap_or_default();
|
2021-06-30 14:12:56 +02:00
|
|
|
docids |= current_docids;
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
2021-02-17 15:49:44 +01:00
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
Ok(docids)
|
|
|
|
}
|
2021-02-17 16:21:21 +01:00
|
|
|
|
2021-03-05 11:02:24 +01:00
|
|
|
fn query_docids(
|
|
|
|
ctx: &dyn Context,
|
|
|
|
query: &Query,
|
|
|
|
wdcache: &mut WordDerivationsCache,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<RoaringBitmap> {
|
2021-02-17 18:16:21 +01:00
|
|
|
match &query.kind {
|
2022-03-30 16:07:59 +02:00
|
|
|
QueryKind::Exact { word, original_typo } => {
|
2021-02-17 18:16:21 +01:00
|
|
|
if query.prefix && ctx.in_prefix_cache(&word) {
|
2022-03-30 16:07:59 +02:00
|
|
|
let mut docids = ctx.word_prefix_docids(&word)?.unwrap_or_default();
|
|
|
|
// only add the exact docids if the word hasn't been derived
|
|
|
|
if *original_typo == 0 {
|
|
|
|
docids |= ctx.exact_word_prefix_docids(&word)?.unwrap_or_default();
|
|
|
|
}
|
|
|
|
Ok(docids)
|
2021-02-17 18:16:21 +01:00
|
|
|
} else if query.prefix {
|
2021-03-05 11:02:24 +01:00
|
|
|
let words = word_derivations(&word, true, 0, ctx.words_fst(), wdcache)?;
|
2021-02-17 18:16:21 +01:00
|
|
|
let mut docids = RoaringBitmap::new();
|
|
|
|
for (word, _typo) in words {
|
2022-03-30 16:07:59 +02:00
|
|
|
docids |= ctx.word_docids(&word)?.unwrap_or_default();
|
|
|
|
// only add the exact docids if the word hasn't been derived
|
|
|
|
if *original_typo == 0 {
|
|
|
|
docids |= ctx.exact_word_docids(&word)?.unwrap_or_default();
|
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
|
|
|
Ok(docids)
|
|
|
|
} else {
|
2022-03-30 16:07:59 +02:00
|
|
|
let mut docids = ctx.word_docids(&word)?.unwrap_or_default();
|
|
|
|
// only add the exact docids if the word hasn't been derived
|
|
|
|
if *original_typo == 0 {
|
|
|
|
docids |= ctx.exact_word_docids(&word)?.unwrap_or_default();
|
|
|
|
}
|
|
|
|
Ok(docids)
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
QueryKind::Tolerant { typo, word } => {
|
2021-03-05 11:02:24 +01:00
|
|
|
let words = word_derivations(&word, query.prefix, *typo, ctx.words_fst(), wdcache)?;
|
2021-02-17 18:16:21 +01:00
|
|
|
let mut docids = RoaringBitmap::new();
|
2022-03-30 16:07:59 +02:00
|
|
|
for (word, typo) in words {
|
|
|
|
let mut current_docids = ctx.word_docids(&word)?.unwrap_or_default();
|
|
|
|
if *typo == 0 {
|
|
|
|
current_docids |= ctx.exact_word_docids(&word)?.unwrap_or_default()
|
|
|
|
}
|
2021-06-30 14:12:56 +02:00
|
|
|
docids |= current_docids;
|
2021-02-17 16:21:21 +01:00
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
Ok(docids)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:02:24 +01:00
|
|
|
fn query_pair_proximity_docids(
|
|
|
|
ctx: &dyn Context,
|
|
|
|
left: &Query,
|
|
|
|
right: &Query,
|
|
|
|
proximity: u8,
|
|
|
|
wdcache: &mut WordDerivationsCache,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<RoaringBitmap> {
|
2021-02-24 15:36:57 +01:00
|
|
|
if proximity >= 8 {
|
2021-03-05 11:02:24 +01:00
|
|
|
let mut candidates = query_docids(ctx, left, wdcache)?;
|
|
|
|
let right_candidates = query_docids(ctx, right, wdcache)?;
|
2021-06-30 14:12:56 +02:00
|
|
|
candidates &= right_candidates;
|
2021-02-24 15:36:57 +01:00
|
|
|
return Ok(candidates);
|
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
|
2021-02-24 15:36:57 +01:00
|
|
|
let prefix = right.prefix;
|
2021-02-17 18:16:21 +01:00
|
|
|
match (&left.kind, &right.kind) {
|
|
|
|
(QueryKind::Exact { word: left, .. }, QueryKind::Exact { word: right, .. }) => {
|
2021-08-18 18:04:24 +02:00
|
|
|
if prefix {
|
2022-09-15 09:34:35 +02:00
|
|
|
match word_prefix_pair_overall_proximity_docids(
|
|
|
|
ctx,
|
2021-08-18 18:04:24 +02:00
|
|
|
left.as_str(),
|
|
|
|
right.as_str(),
|
|
|
|
proximity,
|
|
|
|
)? {
|
|
|
|
Some(docids) => Ok(docids),
|
|
|
|
None => {
|
|
|
|
let r_words = word_derivations(&right, true, 0, ctx.words_fst(), wdcache)?;
|
2022-09-15 09:34:35 +02:00
|
|
|
all_word_pair_overall_proximity_docids(
|
|
|
|
ctx,
|
|
|
|
&[(left, 0)],
|
|
|
|
&r_words,
|
|
|
|
proximity,
|
|
|
|
)
|
2021-08-18 18:04:24 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
} else {
|
2021-06-16 18:33:33 +02:00
|
|
|
Ok(ctx
|
|
|
|
.word_pair_proximity_docids(left.as_str(), right.as_str(), proximity)?
|
|
|
|
.unwrap_or_default())
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
(QueryKind::Tolerant { typo, word: left }, QueryKind::Exact { word: right, .. }) => {
|
2021-06-16 18:33:33 +02:00
|
|
|
let l_words =
|
|
|
|
word_derivations(&left, false, *typo, ctx.words_fst(), wdcache)?.to_owned();
|
2021-08-18 18:04:24 +02:00
|
|
|
if prefix {
|
2021-02-17 18:16:21 +01:00
|
|
|
let mut docids = RoaringBitmap::new();
|
|
|
|
for (left, _) in l_words {
|
2022-09-15 09:34:35 +02:00
|
|
|
let current_docids = match word_prefix_pair_overall_proximity_docids(
|
|
|
|
ctx,
|
2021-08-18 18:04:24 +02:00
|
|
|
left.as_str(),
|
|
|
|
right.as_str(),
|
|
|
|
proximity,
|
|
|
|
)? {
|
|
|
|
Some(docids) => Ok(docids),
|
|
|
|
None => {
|
|
|
|
let r_words =
|
|
|
|
word_derivations(&right, true, 0, ctx.words_fst(), wdcache)?;
|
2022-09-15 09:34:35 +02:00
|
|
|
all_word_pair_overall_proximity_docids(
|
|
|
|
ctx,
|
|
|
|
&[(left, 0)],
|
|
|
|
&r_words,
|
|
|
|
proximity,
|
|
|
|
)
|
2021-08-18 18:04:24 +02:00
|
|
|
}
|
|
|
|
}?;
|
2021-06-30 14:12:56 +02:00
|
|
|
docids |= current_docids;
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
|
|
|
Ok(docids)
|
|
|
|
} else {
|
2022-09-15 09:34:35 +02:00
|
|
|
all_word_pair_overall_proximity_docids(ctx, &l_words, &[(right, 0)], proximity)
|
2021-02-17 18:16:21 +01:00
|
|
|
}
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-02-17 18:16:21 +01:00
|
|
|
(QueryKind::Exact { word: left, .. }, QueryKind::Tolerant { typo, word: right }) => {
|
2021-03-05 11:02:24 +01:00
|
|
|
let r_words = word_derivations(&right, prefix, *typo, ctx.words_fst(), wdcache)?;
|
2022-09-15 09:34:35 +02:00
|
|
|
all_word_pair_overall_proximity_docids(ctx, &[(left, 0)], &r_words, proximity)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
|
|
|
(
|
|
|
|
QueryKind::Tolerant { typo: l_typo, word: left },
|
|
|
|
QueryKind::Tolerant { typo: r_typo, word: right },
|
|
|
|
) => {
|
|
|
|
let l_words =
|
|
|
|
word_derivations(&left, false, *l_typo, ctx.words_fst(), wdcache)?.to_owned();
|
2021-03-05 11:02:24 +01:00
|
|
|
let r_words = word_derivations(&right, prefix, *r_typo, ctx.words_fst(), wdcache)?;
|
2022-09-15 09:34:35 +02:00
|
|
|
all_word_pair_overall_proximity_docids(ctx, &l_words, &r_words, proximity)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-02-17 16:21:21 +01:00
|
|
|
}
|
2021-02-17 15:27:35 +01:00
|
|
|
}
|
2021-02-24 10:25:22 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod test {
|
2021-06-16 18:33:33 +02:00
|
|
|
use std::collections::HashMap;
|
2022-03-14 17:13:07 +01:00
|
|
|
use std::iter;
|
2021-06-16 18:33:33 +02:00
|
|
|
|
2021-02-24 10:25:22 +01:00
|
|
|
use maplit::hashmap;
|
2021-06-16 18:33:33 +02:00
|
|
|
use rand::rngs::StdRng;
|
|
|
|
use rand::{Rng, SeedableRng};
|
2021-02-24 10:25:22 +01:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn s(s: &str) -> String {
|
|
|
|
s.to_string()
|
|
|
|
}
|
2021-02-24 10:25:22 +01:00
|
|
|
pub struct TestContext<'t> {
|
|
|
|
words_fst: fst::Set<Cow<'t, [u8]>>,
|
|
|
|
word_docids: HashMap<String, RoaringBitmap>,
|
2022-03-24 19:25:11 +01:00
|
|
|
exact_word_docids: HashMap<String, RoaringBitmap>,
|
2021-02-24 10:25:22 +01:00
|
|
|
word_prefix_docids: HashMap<String, RoaringBitmap>,
|
2022-03-25 16:27:48 +01:00
|
|
|
exact_word_prefix_docids: HashMap<String, RoaringBitmap>,
|
2021-02-24 10:25:22 +01:00
|
|
|
word_pair_proximity_docids: HashMap<(String, String, i32), RoaringBitmap>,
|
|
|
|
word_prefix_pair_proximity_docids: HashMap<(String, String, i32), RoaringBitmap>,
|
2022-09-15 09:34:35 +02:00
|
|
|
prefix_word_pair_proximity_docids: HashMap<(String, String, i32), RoaringBitmap>,
|
2021-03-18 13:49:55 +01:00
|
|
|
docid_words: HashMap<u32, Vec<String>>,
|
2021-02-24 10:25:22 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 19:23:02 +02:00
|
|
|
impl<'c> Context<'c> for TestContext<'c> {
|
2021-02-25 16:34:29 +01:00
|
|
|
fn documents_ids(&self) -> heed::Result<RoaringBitmap> {
|
|
|
|
Ok(self.word_docids.iter().fold(RoaringBitmap::new(), |acc, (_, docids)| acc | docids))
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:25:22 +01:00
|
|
|
fn word_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
Ok(self.word_docids.get(&word.to_string()).cloned())
|
|
|
|
}
|
|
|
|
|
2022-03-24 19:25:11 +01:00
|
|
|
fn exact_word_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
Ok(self.exact_word_docids.get(&word.to_string()).cloned())
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:25:22 +01:00
|
|
|
fn word_prefix_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
Ok(self.word_prefix_docids.get(&word.to_string()).cloned())
|
|
|
|
}
|
|
|
|
|
2022-03-25 16:27:48 +01:00
|
|
|
fn exact_word_prefix_docids(&self, word: &str) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
Ok(self.exact_word_prefix_docids.get(&word.to_string()).cloned())
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn word_pair_proximity_docids(
|
|
|
|
&self,
|
|
|
|
left: &str,
|
|
|
|
right: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
2021-02-24 10:25:22 +01:00
|
|
|
let key = (left.to_string(), right.to_string(), proximity.into());
|
|
|
|
Ok(self.word_pair_proximity_docids.get(&key).cloned())
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn word_prefix_pair_proximity_docids(
|
|
|
|
&self,
|
2022-09-15 09:34:35 +02:00
|
|
|
word: &str,
|
|
|
|
prefix: &str,
|
2021-06-16 18:33:33 +02:00
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
2022-09-15 09:34:35 +02:00
|
|
|
let key = (word.to_string(), prefix.to_string(), proximity.into());
|
2021-02-24 10:25:22 +01:00
|
|
|
Ok(self.word_prefix_pair_proximity_docids.get(&key).cloned())
|
|
|
|
}
|
2022-09-15 09:34:35 +02:00
|
|
|
fn prefix_word_pair_proximity_docids(
|
|
|
|
&self,
|
|
|
|
prefix: &str,
|
|
|
|
word: &str,
|
|
|
|
proximity: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
|
|
|
let key = (prefix.to_string(), word.to_string(), proximity.into());
|
|
|
|
Ok(self.prefix_word_pair_proximity_docids.get(&key).cloned())
|
|
|
|
}
|
2021-02-24 10:25:22 +01:00
|
|
|
|
|
|
|
fn words_fst<'t>(&self) -> &'t fst::Set<Cow<[u8]>> {
|
|
|
|
&self.words_fst
|
|
|
|
}
|
|
|
|
|
|
|
|
fn in_prefix_cache(&self, word: &str) -> bool {
|
|
|
|
self.word_prefix_docids.contains_key(&word.to_string())
|
|
|
|
}
|
2021-03-03 15:41:09 +01:00
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn docid_words_positions(
|
|
|
|
&self,
|
|
|
|
docid: DocumentId,
|
|
|
|
) -> heed::Result<HashMap<String, RoaringBitmap>> {
|
2021-03-18 13:49:55 +01:00
|
|
|
if let Some(docid_words) = self.docid_words.get(&docid) {
|
|
|
|
Ok(docid_words
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2021-06-16 18:33:33 +02:00
|
|
|
.map(|(i, w)| {
|
2022-03-14 17:13:07 +01:00
|
|
|
let bitmap = RoaringBitmap::from_sorted_iter(iter::once(i as u32)).unwrap();
|
|
|
|
(w.clone(), bitmap)
|
2021-06-16 18:33:33 +02:00
|
|
|
})
|
|
|
|
.collect())
|
2021-03-18 13:49:55 +01:00
|
|
|
} else {
|
|
|
|
Ok(HashMap::new())
|
|
|
|
}
|
2021-03-03 15:41:09 +01:00
|
|
|
}
|
2021-03-31 19:23:02 +02:00
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn word_position_iterator(
|
|
|
|
&self,
|
|
|
|
_word: &str,
|
|
|
|
_in_prefix_cache: bool,
|
|
|
|
) -> heed::Result<
|
2021-10-05 11:18:42 +02:00
|
|
|
Box<dyn Iterator<Item = heed::Result<((&'c str, u32), RoaringBitmap)>> + 'c>,
|
2021-06-16 18:33:33 +02:00
|
|
|
> {
|
2021-03-31 19:23:02 +02:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:24:46 +02:00
|
|
|
fn synonyms(&self, _word: &str) -> heed::Result<Option<Vec<Vec<String>>>> {
|
2021-05-04 13:44:55 +02:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn searchable_fields_ids(&self) -> Result<Vec<FieldId>> {
|
2021-05-04 13:44:55 +02:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2021-10-05 11:18:42 +02:00
|
|
|
fn word_position_docids(
|
2021-06-16 18:33:33 +02:00
|
|
|
&self,
|
|
|
|
_word: &str,
|
2021-10-05 11:18:42 +02:00
|
|
|
_pos: u32,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
2021-05-04 13:44:55 +02:00
|
|
|
todo!()
|
|
|
|
}
|
2021-05-04 15:28:05 +02:00
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
fn field_id_word_count_docids(
|
|
|
|
&self,
|
|
|
|
_field_id: FieldId,
|
|
|
|
_word_count: u8,
|
|
|
|
) -> heed::Result<Option<RoaringBitmap>> {
|
2021-05-04 15:28:05 +02:00
|
|
|
todo!()
|
|
|
|
}
|
2021-02-24 10:25:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Default for TestContext<'a> {
|
|
|
|
fn default() -> TestContext<'a> {
|
|
|
|
let mut rng = StdRng::seed_from_u64(102);
|
|
|
|
let rng = &mut rng;
|
|
|
|
|
|
|
|
fn random_postings<R: Rng>(rng: &mut R, len: usize) -> RoaringBitmap {
|
|
|
|
let mut values = Vec::<u32>::with_capacity(len);
|
|
|
|
while values.len() != len {
|
|
|
|
values.push(rng.gen());
|
|
|
|
}
|
|
|
|
values.sort_unstable();
|
|
|
|
|
2022-03-14 17:13:07 +01:00
|
|
|
RoaringBitmap::from_sorted_iter(values.into_iter()).unwrap()
|
2021-02-24 10:25:22 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
let word_docids = hashmap! {
|
2021-02-24 10:25:22 +01:00
|
|
|
s("hello") => random_postings(rng, 1500),
|
|
|
|
s("hi") => random_postings(rng, 4000),
|
|
|
|
s("word") => random_postings(rng, 2500),
|
|
|
|
s("split") => random_postings(rng, 400),
|
|
|
|
s("ngrams") => random_postings(rng, 1400),
|
|
|
|
s("world") => random_postings(rng, 15_000),
|
|
|
|
s("earth") => random_postings(rng, 8000),
|
|
|
|
s("2021") => random_postings(rng, 100),
|
|
|
|
s("2020") => random_postings(rng, 500),
|
|
|
|
s("is") => random_postings(rng, 50_000),
|
|
|
|
s("this") => random_postings(rng, 50_000),
|
|
|
|
s("good") => random_postings(rng, 1250),
|
|
|
|
s("morning") => random_postings(rng, 125),
|
|
|
|
};
|
|
|
|
|
2022-03-24 19:25:11 +01:00
|
|
|
let exact_word_docids = HashMap::new();
|
|
|
|
|
2021-03-18 13:49:55 +01:00
|
|
|
let mut docid_words = HashMap::new();
|
|
|
|
for (word, docids) in word_docids.iter() {
|
|
|
|
for docid in docids {
|
|
|
|
let words = docid_words.entry(docid).or_insert(vec![]);
|
|
|
|
words.push(word.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
let word_prefix_docids = hashmap! {
|
2021-02-24 10:25:22 +01:00
|
|
|
s("h") => &word_docids[&s("hello")] | &word_docids[&s("hi")],
|
|
|
|
s("wor") => &word_docids[&s("word")] | &word_docids[&s("world")],
|
|
|
|
s("20") => &word_docids[&s("2020")] | &word_docids[&s("2021")],
|
|
|
|
};
|
|
|
|
|
2022-03-25 16:27:48 +01:00
|
|
|
let exact_word_prefix_docids = HashMap::new();
|
|
|
|
|
2021-03-18 13:49:55 +01:00
|
|
|
let mut word_pair_proximity_docids = HashMap::new();
|
|
|
|
let mut word_prefix_pair_proximity_docids = HashMap::new();
|
2022-09-15 09:34:35 +02:00
|
|
|
let mut prefix_word_pair_proximity_docids = HashMap::new();
|
|
|
|
|
2021-03-18 13:49:55 +01:00
|
|
|
for (lword, lcandidates) in &word_docids {
|
|
|
|
for (rword, rcandidates) in &word_docids {
|
2021-06-16 18:33:33 +02:00
|
|
|
if lword == rword {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-18 13:49:55 +01:00
|
|
|
let candidates = lcandidates & rcandidates;
|
|
|
|
for candidate in candidates {
|
|
|
|
if let Some(docid_words) = docid_words.get(&candidate) {
|
|
|
|
let lposition = docid_words.iter().position(|w| w == lword).unwrap();
|
|
|
|
let rposition = docid_words.iter().position(|w| w == rword).unwrap();
|
|
|
|
let key = if lposition < rposition {
|
|
|
|
(s(lword), s(rword), (rposition - lposition) as i32)
|
|
|
|
} else {
|
|
|
|
(s(lword), s(rword), (lposition - rposition + 1) as i32)
|
|
|
|
};
|
2021-06-16 18:33:33 +02:00
|
|
|
let docids = word_pair_proximity_docids
|
|
|
|
.entry(key)
|
|
|
|
.or_insert(RoaringBitmap::new());
|
2021-03-18 13:49:55 +01:00
|
|
|
docids.push(candidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (pword, pcandidates) in &word_prefix_docids {
|
2021-06-16 18:33:33 +02:00
|
|
|
if lword.starts_with(pword) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-18 13:49:55 +01:00
|
|
|
let candidates = lcandidates & pcandidates;
|
|
|
|
for candidate in candidates {
|
|
|
|
if let Some(docid_words) = docid_words.get(&candidate) {
|
|
|
|
let lposition = docid_words.iter().position(|w| w == lword).unwrap();
|
2021-06-16 18:33:33 +02:00
|
|
|
let rposition =
|
|
|
|
docid_words.iter().position(|w| w.starts_with(pword)).unwrap();
|
2022-09-15 09:34:35 +02:00
|
|
|
if lposition < rposition {
|
|
|
|
let key = (s(lword), s(pword), (rposition - lposition) as i32);
|
|
|
|
let docids = word_prefix_pair_proximity_docids
|
|
|
|
.entry(key)
|
|
|
|
.or_insert(RoaringBitmap::new());
|
|
|
|
docids.push(candidate);
|
2021-03-18 13:49:55 +01:00
|
|
|
} else {
|
2022-09-15 09:34:35 +02:00
|
|
|
let key = (s(lword), s(pword), (lposition - rposition) as i32);
|
|
|
|
let docids = prefix_word_pair_proximity_docids
|
|
|
|
.entry(key)
|
|
|
|
.or_insert(RoaringBitmap::new());
|
|
|
|
docids.push(candidate);
|
2021-03-18 13:49:55 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 10:25:22 +01:00
|
|
|
|
|
|
|
let mut keys = word_docids.keys().collect::<Vec<_>>();
|
|
|
|
keys.sort_unstable();
|
|
|
|
let words_fst = fst::Set::from_iter(keys).unwrap().map_data(|v| Cow::Owned(v)).unwrap();
|
|
|
|
|
|
|
|
TestContext {
|
|
|
|
words_fst,
|
|
|
|
word_docids,
|
2022-03-24 19:25:11 +01:00
|
|
|
exact_word_docids,
|
2021-02-24 10:25:22 +01:00
|
|
|
word_prefix_docids,
|
2022-03-25 16:27:48 +01:00
|
|
|
exact_word_prefix_docids,
|
2021-02-24 10:25:22 +01:00
|
|
|
word_pair_proximity_docids,
|
|
|
|
word_prefix_pair_proximity_docids,
|
2022-09-15 09:34:35 +02:00
|
|
|
prefix_word_pair_proximity_docids,
|
2021-03-18 13:49:55 +01:00
|
|
|
docid_words,
|
2021-02-24 10:25:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|