2021-01-13 18:59:16 +08:00
|
|
|
use std::collections::{HashSet, BTreeSet, 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
|
|
|
|
|
|
|
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};
|
|
|
|
|
|
|
|
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-03 19:59:16 +08:00
|
|
|
FacetDistribution { facets: None, candidates: None, max_values_by_facet: 100, rtxn, index }
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
self.max_values_by_facet = cmp::min(max, 1000);
|
2020-12-29 02:08:53 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:59:16 +08:00
|
|
|
fn facet_values(&self, field_id: FieldId, facet_type: FacetType) -> heed::Result<BTreeSet<FacetValue>> {
|
2021-01-06 22:10:30 +08:00
|
|
|
if let Some(candidates) = self.candidates.as_ref() {
|
|
|
|
if candidates.len() <= 1000 {
|
|
|
|
let mut key_buffer = vec![field_id];
|
|
|
|
match facet_type {
|
|
|
|
FacetType::Float => {
|
2021-01-13 18:59:16 +08:00
|
|
|
let mut facet_values = BTreeSet::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)?
|
|
|
|
.remap_key_type::<FieldDocIdFacetF64Codec>();
|
|
|
|
for result in iter {
|
|
|
|
let ((_, _, value), ()) = result?;
|
2021-01-13 18:59:16 +08:00
|
|
|
facet_values.insert(FacetValue::from(value));
|
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
|
|
|
},
|
|
|
|
FacetType::Integer => {
|
2021-01-13 18:59:16 +08:00
|
|
|
let mut facet_values = BTreeSet::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)?
|
|
|
|
.remap_key_type::<FieldDocIdFacetI64Codec>();
|
|
|
|
for result in iter {
|
|
|
|
let ((_, _, value), ()) = result?;
|
2021-01-13 18:59:16 +08:00
|
|
|
facet_values.insert(FacetValue::from(value));
|
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
|
|
|
},
|
|
|
|
FacetType::String => {
|
2021-01-13 18:59:16 +08:00
|
|
|
let mut facet_values = BTreeSet::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)?
|
|
|
|
.remap_key_type::<FieldDocIdFacetStringCodec>();
|
|
|
|
for result in iter {
|
|
|
|
let ((_, _, value), ()) = result?;
|
2021-01-13 18:59:16 +08:00
|
|
|
facet_values.insert(FacetValue::from(value));
|
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 {
|
|
|
|
let iter = match facet_type {
|
|
|
|
FacetType::String => {
|
|
|
|
let db = self.index.facet_field_id_value_docids;
|
|
|
|
let iter = db
|
|
|
|
.prefix_iter(self.rtxn, &[field_id])?
|
|
|
|
.remap_key_type::<FacetValueStringCodec>()
|
2021-01-13 18:59:16 +08:00
|
|
|
.map(|r| r.map(|((_, v), docids)| (FacetValue::from(v), docids)));
|
2021-01-06 22:10:30 +08:00
|
|
|
Box::new(iter) as Box::<dyn Iterator<Item=_>>
|
|
|
|
},
|
|
|
|
FacetType::Integer => {
|
|
|
|
let iter = FacetIter::<i64, FacetLevelValueI64Codec>::new_non_reducing(
|
|
|
|
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
|
|
|
},
|
|
|
|
FacetType::Float => {
|
|
|
|
let iter = FacetIter::<f64, FacetLevelValueF64Codec>::new_non_reducing(
|
|
|
|
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-13 18:59:16 +08:00
|
|
|
let mut facet_values = BTreeSet::new();
|
2021-01-06 22:10:30 +08:00
|
|
|
for result in iter {
|
|
|
|
let (value, docids) = result?;
|
|
|
|
if self.candidates.as_ref().map_or(true, |c| docids.is_disjoint(c)) {
|
2021-01-13 18:59:16 +08:00
|
|
|
facet_values.insert(value);
|
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 {
|
|
|
|
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=_>>
|
|
|
|
},
|
|
|
|
FacetType::Integer => {
|
|
|
|
let db = db.remap_key_type::<FacetLevelValueI64Codec>();
|
|
|
|
let range = FacetRange::<i64, _>::new(
|
|
|
|
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
|
|
|
},
|
|
|
|
FacetType::Float => {
|
|
|
|
let db = db.remap_key_type::<FacetLevelValueF64Codec>();
|
|
|
|
let range = FacetRange::<f64, _>::new(
|
|
|
|
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-13 18:59:16 +08:00
|
|
|
let mut facet_values = BTreeSet::new();
|
2021-01-03 21:11:00 +08:00
|
|
|
for result in iter {
|
|
|
|
let (value, docids) = result?;
|
2021-01-06 22:10:30 +08:00
|
|
|
if self.candidates.as_ref().map_or(true, |c| docids.is_disjoint(c)) {
|
2021-01-13 18:59:16 +08:00
|
|
|
facet_values.insert(value);
|
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-13 18:59:16 +08:00
|
|
|
pub fn execute(&self) -> heed::Result<BTreeMap<String, BTreeSet<FacetValue>>> {
|
2020-12-29 02:08:53 +08:00
|
|
|
let fields_ids_map = self.index.fields_ids_map(self.rtxn)?;
|
|
|
|
let faceted_fields = self.index.faceted_fields(self.rtxn)?;
|
|
|
|
let fields_ids: Vec<_> = match &self.facets {
|
|
|
|
Some(names) => {
|
|
|
|
names.iter().filter_map(|n| {
|
|
|
|
let id = fields_ids_map.id(n)?;
|
|
|
|
faceted_fields.get(&id).cloned().map(|t| (id, t))
|
|
|
|
}).collect()
|
|
|
|
},
|
|
|
|
None => faceted_fields.iter().map(|(id, t)| (*id, *t)).collect(),
|
|
|
|
};
|
|
|
|
|
2021-01-13 18:59:16 +08:00
|
|
|
let mut facets_values = BTreeMap::new();
|
2020-12-29 02:08:53 +08:00
|
|
|
for (fid, ftype) in fields_ids {
|
|
|
|
let facet_name = fields_ids_map.name(fid).unwrap();
|
|
|
|
let values = self.facet_values(fid, ftype)?;
|
|
|
|
facets_values.insert(facet_name.to_string(), values);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|