2021-01-26 21:14:37 +08:00
|
|
|
use std::collections::{HashSet, BTreeMap};
|
2020-12-29 02:08:53 +08:00
|
|
|
use std::ops::Bound::Unbounded;
|
2021-01-13 18:59:16 +08:00
|
|
|
use std::{cmp, fmt};
|
2020-12-29 02:08:53 +08:00
|
|
|
|
2021-01-28 01:29:54 +08:00
|
|
|
use anyhow::Context;
|
2020-12-29 02:08:53 +08:00
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2021-01-13 18:59:16 +08:00
|
|
|
use crate::facet::{FacetType, FacetValue};
|
2020-12-29 02:08:53 +08:00
|
|
|
use crate::heed_codec::facet::{FacetValueStringCodec, FacetLevelValueF64Codec, FacetLevelValueI64Codec};
|
2021-01-03 21:11:00 +08:00
|
|
|
use crate::heed_codec::facet::{FieldDocIdFacetStringCodec, FieldDocIdFacetF64Codec, FieldDocIdFacetI64Codec};
|
2021-01-06 22:10:30 +08:00
|
|
|
use crate::search::facet::{FacetIter, FacetRange};
|
2020-12-29 02:08:53 +08:00
|
|
|
use crate::{Index, FieldId};
|
|
|
|
|
2021-01-27 21:32:30 +08:00
|
|
|
/// The default number of values by facets that will
|
|
|
|
/// be fetched from the key-value store.
|
|
|
|
const DEFAULT_VALUES_BY_FACET: usize = 100;
|
|
|
|
|
|
|
|
/// The hard limit in the number of values by facets that will be fetched from
|
|
|
|
/// the key-value store. Searching for more values could slow down the engine.
|
|
|
|
const MAX_VALUES_BY_FACET: usize = 1000;
|
|
|
|
|
|
|
|
/// Threshold on the number of candidates that will make
|
|
|
|
/// the system to choose between one algorithm or another.
|
|
|
|
const CANDIDATES_THRESHOLD: u64 = 1000;
|
|
|
|
|
2020-12-29 02:08:53 +08:00
|
|
|
pub struct FacetDistribution<'a> {
|
|
|
|
facets: Option<HashSet<String>>,
|
|
|
|
candidates: Option<RoaringBitmap>,
|
2021-01-03 19:59:16 +08:00
|
|
|
max_values_by_facet: usize,
|
2020-12-29 02:08:53 +08:00
|
|
|
rtxn: &'a heed::RoTxn<'a>,
|
|
|
|
index: &'a Index,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FacetDistribution<'a> {
|
|
|
|
pub fn new(rtxn: &'a heed::RoTxn, index: &'a Index) -> FacetDistribution<'a> {
|
2021-01-27 21:32:30 +08:00
|
|
|
FacetDistribution {
|
|
|
|
facets: None,
|
|
|
|
candidates: None,
|
|
|
|
max_values_by_facet: DEFAULT_VALUES_BY_FACET,
|
|
|
|
rtxn,
|
|
|
|
index,
|
|
|
|
}
|
2021-01-03 19:59:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn facets<I: IntoIterator<Item=A>, A: AsRef<str>>(&mut self, names: I) -> &mut Self {
|
|
|
|
self.facets = Some(names.into_iter().map(|s| s.as_ref().to_string()).collect());
|
|
|
|
self
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn candidates(&mut self, candidates: RoaringBitmap) -> &mut Self {
|
|
|
|
self.candidates = Some(candidates);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-03 19:59:16 +08:00
|
|
|
pub fn max_values_by_facet(&mut self, max: usize) -> &mut Self {
|
2021-01-27 21:32:30 +08:00
|
|
|
self.max_values_by_facet = cmp::min(max, MAX_VALUES_BY_FACET);
|
2020-12-29 02:08:53 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:14:37 +08:00
|
|
|
fn facet_values(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
facet_type: FacetType,
|
|
|
|
) -> heed::Result<BTreeMap<FacetValue, u64>>
|
|
|
|
{
|
2021-01-06 22:10:30 +08:00
|
|
|
if let Some(candidates) = self.candidates.as_ref() {
|
2021-01-27 21:44:16 +08:00
|
|
|
// Classic search, candidates were specified, we must return
|
|
|
|
// facet values only related to those candidates.
|
2021-01-27 21:41:36 +08:00
|
|
|
if candidates.len() <= CANDIDATES_THRESHOLD || facet_type == FacetType::String {
|
2021-01-27 21:44:16 +08:00
|
|
|
// There is a small amount of candidates OR we ask for facet string values so we
|
|
|
|
// decide to iterate over the facet values of each one of them, one by one.
|
2021-01-06 22:10:30 +08:00
|
|
|
let mut key_buffer = vec![field_id];
|
|
|
|
match facet_type {
|
2021-01-27 21:36:49 +08:00
|
|
|
FacetType::String => {
|
2021-01-26 21:14:37 +08:00
|
|
|
let mut facet_values = BTreeMap::new();
|
2021-01-27 21:41:36 +08:00
|
|
|
for docid in candidates.into_iter().take(CANDIDATES_THRESHOLD as usize) {
|
2021-01-06 22:10:30 +08:00
|
|
|
key_buffer.truncate(1);
|
|
|
|
key_buffer.extend_from_slice(&docid.to_be_bytes());
|
|
|
|
let iter = self.index.field_id_docid_facet_values
|
|
|
|
.prefix_iter(self.rtxn, &key_buffer)?
|
2021-01-27 21:36:49 +08:00
|
|
|
.remap_key_type::<FieldDocIdFacetStringCodec>();
|
2021-01-27 21:41:36 +08:00
|
|
|
|
2021-01-06 22:10:30 +08:00
|
|
|
for result in iter {
|
|
|
|
let ((_, _, value), ()) = result?;
|
2021-01-26 21:14:37 +08:00
|
|
|
*facet_values.entry(FacetValue::from(value)).or_insert(0) += 1;
|
2021-01-06 22:10:30 +08:00
|
|
|
}
|
2021-01-03 21:11:00 +08:00
|
|
|
}
|
2021-01-13 18:59:16 +08:00
|
|
|
Ok(facet_values)
|
2021-01-06 22:10:30 +08:00
|
|
|
},
|
2021-01-27 21:36:49 +08:00
|
|
|
FacetType::Float => {
|
2021-01-26 21:14:37 +08:00
|
|
|
let mut facet_values = BTreeMap::new();
|
2021-01-06 22:10:30 +08:00
|
|
|
for docid in candidates {
|
|
|
|
key_buffer.truncate(1);
|
|
|
|
key_buffer.extend_from_slice(&docid.to_be_bytes());
|
|
|
|
let iter = self.index.field_id_docid_facet_values
|
|
|
|
.prefix_iter(self.rtxn, &key_buffer)?
|
2021-01-27 21:36:49 +08:00
|
|
|
.remap_key_type::<FieldDocIdFacetF64Codec>();
|
2021-01-27 21:41:36 +08:00
|
|
|
|
2021-01-06 22:10:30 +08:00
|
|
|
for result in iter {
|
|
|
|
let ((_, _, value), ()) = result?;
|
2021-01-26 21:14:37 +08:00
|
|
|
*facet_values.entry(FacetValue::from(value)).or_insert(0) += 1;
|
2021-01-06 22:10:30 +08:00
|
|
|
}
|
2021-01-03 21:11:00 +08:00
|
|
|
}
|
2021-01-13 18:59:16 +08:00
|
|
|
Ok(facet_values)
|
2021-01-06 22:10:30 +08:00
|
|
|
},
|
2021-01-27 21:36:49 +08:00
|
|
|
FacetType::Integer => {
|
2021-01-26 21:14:37 +08:00
|
|
|
let mut facet_values = BTreeMap::new();
|
2021-01-06 22:10:30 +08:00
|
|
|
for docid in candidates {
|
|
|
|
key_buffer.truncate(1);
|
|
|
|
key_buffer.extend_from_slice(&docid.to_be_bytes());
|
|
|
|
let iter = self.index.field_id_docid_facet_values
|
|
|
|
.prefix_iter(self.rtxn, &key_buffer)?
|
2021-01-27 21:36:49 +08:00
|
|
|
.remap_key_type::<FieldDocIdFacetI64Codec>();
|
2021-01-27 21:41:36 +08:00
|
|
|
|
2021-01-06 22:10:30 +08:00
|
|
|
for result in iter {
|
|
|
|
let ((_, _, value), ()) = result?;
|
2021-01-26 21:14:37 +08:00
|
|
|
*facet_values.entry(FacetValue::from(value)).or_insert(0) += 1;
|
2021-01-06 22:10:30 +08:00
|
|
|
}
|
2021-01-03 21:11:00 +08:00
|
|
|
}
|
2021-01-13 18:59:16 +08:00
|
|
|
Ok(facet_values)
|
2021-01-06 22:10:30 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-27 21:44:16 +08:00
|
|
|
// There is too much documents, we use the facet levels to move throught
|
|
|
|
// the facet values, to find the candidates and values associated.
|
2021-01-06 22:10:30 +08:00
|
|
|
let iter = match facet_type {
|
2021-01-27 21:41:36 +08:00
|
|
|
FacetType::String => unreachable!(),
|
2021-01-27 21:36:49 +08:00
|
|
|
FacetType::Float => {
|
|
|
|
let iter = FacetIter::<f64, FacetLevelValueF64Codec>::new_non_reducing(
|
2021-01-06 22:10:30 +08:00
|
|
|
self.rtxn, self.index, field_id, candidates.clone(),
|
|
|
|
)?;
|
2021-01-27 21:41:36 +08:00
|
|
|
let iter = iter.map(|r| r.map(|(v, docids)| (FacetValue::from(v), docids)));
|
|
|
|
Box::new(iter) as Box::<dyn Iterator<Item=_>>
|
2021-01-06 22:10:30 +08:00
|
|
|
},
|
2021-01-27 21:36:49 +08:00
|
|
|
FacetType::Integer => {
|
|
|
|
let iter = FacetIter::<i64, FacetLevelValueI64Codec>::new_non_reducing(
|
2021-01-06 22:10:30 +08:00
|
|
|
self.rtxn, self.index, field_id, candidates.clone(),
|
|
|
|
)?;
|
2021-01-13 18:59:16 +08:00
|
|
|
Box::new(iter.map(|r| r.map(|(v, docids)| (FacetValue::from(v), docids))))
|
2021-01-06 22:10:30 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-26 21:14:37 +08:00
|
|
|
let mut facet_values = BTreeMap::new();
|
2021-01-06 22:10:30 +08:00
|
|
|
for result in iter {
|
2021-01-26 21:14:37 +08:00
|
|
|
let (value, mut docids) = result?;
|
|
|
|
docids.intersect_with(candidates);
|
|
|
|
if !docids.is_empty() {
|
|
|
|
facet_values.insert(value, docids.len());
|
2021-01-03 21:11:00 +08:00
|
|
|
}
|
2021-01-06 22:10:30 +08:00
|
|
|
if facet_values.len() == self.max_values_by_facet {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(facet_values)
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
2021-01-03 21:11:00 +08:00
|
|
|
} else {
|
2021-01-27 21:44:16 +08:00
|
|
|
// Placeholder search, a.k.a. no candidates were specified. We iterate throught the
|
|
|
|
// facet values one by one and iterate on the facet level 0 for numbers.
|
2021-01-03 21:11:00 +08:00
|
|
|
let db = self.index.facet_field_id_value_docids;
|
|
|
|
let iter = match facet_type {
|
|
|
|
FacetType::String => {
|
|
|
|
let iter = db
|
2021-01-06 22:10:30 +08:00
|
|
|
.prefix_iter(self.rtxn, &[field_id])?
|
2021-01-03 21:11:00 +08:00
|
|
|
.remap_key_type::<FacetValueStringCodec>()
|
2021-01-13 18:59:16 +08:00
|
|
|
.map(|r| r.map(|((_, v), docids)| (FacetValue::from(v), docids)));
|
2021-01-03 21:11:00 +08:00
|
|
|
Box::new(iter) as Box::<dyn Iterator<Item=_>>
|
|
|
|
},
|
2021-01-27 21:36:49 +08:00
|
|
|
FacetType::Float => {
|
|
|
|
let db = db.remap_key_type::<FacetLevelValueF64Codec>();
|
|
|
|
let range = FacetRange::<f64, _>::new(
|
2021-01-03 21:11:00 +08:00
|
|
|
self.rtxn, db, field_id, 0, Unbounded, Unbounded,
|
|
|
|
)?;
|
2021-01-13 18:59:16 +08:00
|
|
|
Box::new(range.map(|r| r.map(|((_, _, v, _), docids)| (FacetValue::from(v), docids))))
|
2021-01-03 21:11:00 +08:00
|
|
|
},
|
2021-01-27 21:36:49 +08:00
|
|
|
FacetType::Integer => {
|
|
|
|
let db = db.remap_key_type::<FacetLevelValueI64Codec>();
|
|
|
|
let range = FacetRange::<i64, _>::new(
|
2021-01-03 21:11:00 +08:00
|
|
|
self.rtxn, db, field_id, 0, Unbounded, Unbounded,
|
|
|
|
)?;
|
2021-01-13 18:59:16 +08:00
|
|
|
Box::new(range.map(|r| r.map(|((_, _, v, _), docids)| (FacetValue::from(v), docids))))
|
2021-01-03 21:11:00 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-26 21:14:37 +08:00
|
|
|
let mut facet_values = BTreeMap::new();
|
2021-01-03 21:11:00 +08:00
|
|
|
for result in iter {
|
|
|
|
let (value, docids) = result?;
|
2021-01-26 21:14:37 +08:00
|
|
|
facet_values.insert(value, docids.len());
|
2021-01-03 21:11:00 +08:00
|
|
|
if facet_values.len() == self.max_values_by_facet {
|
|
|
|
break;
|
|
|
|
}
|
2021-01-03 19:59:16 +08:00
|
|
|
}
|
|
|
|
|
2021-01-03 21:11:00 +08:00
|
|
|
Ok(facet_values)
|
|
|
|
}
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 01:29:54 +08:00
|
|
|
pub fn execute(&self) -> anyhow::Result<BTreeMap<String, BTreeMap<FacetValue, u64>>> {
|
2020-12-29 02:08:53 +08:00
|
|
|
let fields_ids_map = self.index.fields_ids_map(self.rtxn)?;
|
2021-01-27 21:53:50 +08:00
|
|
|
let faceted_fields = self.index.faceted_fields(self.rtxn)?;
|
2020-12-29 02:08:53 +08:00
|
|
|
let fields_ids: Vec<_> = match &self.facets {
|
2021-01-27 21:53:50 +08:00
|
|
|
Some(names) => names
|
|
|
|
.iter()
|
|
|
|
.filter_map(|n| faceted_fields.get(n).map(|t| (n.to_string(), *t)))
|
|
|
|
.collect(),
|
|
|
|
None => faceted_fields.into_iter().collect(),
|
2020-12-29 02:08:53 +08:00
|
|
|
};
|
|
|
|
|
2021-01-13 18:59:16 +08:00
|
|
|
let mut facets_values = BTreeMap::new();
|
2021-01-27 21:53:50 +08:00
|
|
|
for (name, ftype) in fields_ids {
|
2021-01-28 01:29:54 +08:00
|
|
|
let fid = fields_ids_map.id(&name).with_context(|| {
|
|
|
|
format!("missing field name {:?} from the fields id map", name)
|
|
|
|
})?;
|
2020-12-29 02:08:53 +08:00
|
|
|
let values = self.facet_values(fid, ftype)?;
|
2021-01-27 21:53:50 +08:00
|
|
|
facets_values.insert(name, values);
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(facets_values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for FacetDistribution<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-01-03 19:59:16 +08:00
|
|
|
let FacetDistribution {
|
|
|
|
facets,
|
|
|
|
candidates,
|
|
|
|
max_values_by_facet,
|
|
|
|
rtxn: _,
|
|
|
|
index: _,
|
|
|
|
} = self;
|
|
|
|
|
2020-12-29 02:08:53 +08:00
|
|
|
f.debug_struct("FacetDistribution")
|
|
|
|
.field("facets", facets)
|
|
|
|
.field("candidates", candidates)
|
2021-01-03 19:59:16 +08:00
|
|
|
.field("max_values_by_facet", max_values_by_facet)
|
2020-12-29 02:08:53 +08:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|