2021-06-17 00:33:33 +08:00
|
|
|
use std::collections::{BTreeMap, HashSet};
|
2022-08-31 13:50:18 +08:00
|
|
|
use std::ops::ControlFlow;
|
2022-06-08 21:42:29 +08:00
|
|
|
use std::{fmt, mem};
|
2020-12-29 02:08:53 +08:00
|
|
|
|
2021-07-15 16:19:35 +08:00
|
|
|
use heed::types::ByteSlice;
|
2022-08-31 13:50:18 +08:00
|
|
|
use heed::BytesDecode;
|
2020-12-29 02:08:53 +08:00
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2021-07-22 23:11:17 +08:00
|
|
|
use crate::error::UserError;
|
2021-05-03 21:17:24 +08:00
|
|
|
use crate::facet::FacetType;
|
2022-09-05 19:01:36 +08:00
|
|
|
use crate::heed_codec::facet::OrderedF64Codec;
|
|
|
|
use crate::heed_codec::facet::StrRefCodec;
|
|
|
|
use crate::heed_codec::facet::{ByteSliceRef, FacetGroupKeyCodec, FacetGroupValueCodec};
|
2022-08-29 22:01:54 +08:00
|
|
|
use crate::heed_codec::facet::{FieldDocIdFacetF64Codec, FieldDocIdFacetStringCodec};
|
2022-08-31 13:50:18 +08:00
|
|
|
use crate::search::facet::facet_distribution_iter;
|
2021-07-15 16:19:35 +08:00
|
|
|
use crate::{FieldId, Index, Result};
|
2020-12-29 02:08:53 +08:00
|
|
|
|
2022-05-18 20:51:00 +08:00
|
|
|
/// The default number of values by facets that will
|
|
|
|
/// be fetched from the key-value store.
|
2022-06-08 23:28:23 +08:00
|
|
|
pub const DEFAULT_VALUES_PER_FACET: usize = 100;
|
2022-05-18 20:51:00 +08:00
|
|
|
|
2021-01-27 21:32:30 +08:00
|
|
|
/// Threshold on the number of candidates that will make
|
|
|
|
/// the system to choose between one algorithm or another.
|
2021-07-17 18:50:01 +08:00
|
|
|
const CANDIDATES_THRESHOLD: u64 = 3000;
|
2021-01-27 21:32:30 +08:00
|
|
|
|
2020-12-29 02:08:53 +08:00
|
|
|
pub struct FacetDistribution<'a> {
|
|
|
|
facets: Option<HashSet<String>>,
|
|
|
|
candidates: Option<RoaringBitmap>,
|
2022-06-08 23:28:23 +08:00
|
|
|
max_values_per_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> {
|
2022-05-18 20:51:00 +08:00
|
|
|
FacetDistribution {
|
|
|
|
facets: None,
|
|
|
|
candidates: None,
|
2022-06-08 23:28:23 +08:00
|
|
|
max_values_per_facet: DEFAULT_VALUES_PER_FACET,
|
2022-05-18 20:51:00 +08:00
|
|
|
rtxn,
|
|
|
|
index,
|
|
|
|
}
|
2021-01-03 19:59:16 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 00:33:33 +08:00
|
|
|
pub fn facets<I: IntoIterator<Item = A>, A: AsRef<str>>(&mut self, names: I) -> &mut Self {
|
2021-01-03 19:59:16 +08:00
|
|
|
self.facets = Some(names.into_iter().map(|s| s.as_ref().to_string()).collect());
|
|
|
|
self
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:28:23 +08:00
|
|
|
pub fn max_values_per_facet(&mut self, max: usize) -> &mut Self {
|
|
|
|
self.max_values_per_facet = max;
|
2022-05-18 20:51:00 +08:00
|
|
|
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-27 23:13:49 +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-05-03 21:17:24 +08:00
|
|
|
fn facet_distribution_from_documents(
|
2021-01-26 21:14:37 +08:00
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
facet_type: FacetType,
|
2021-01-27 23:13:49 +08:00
|
|
|
candidates: &RoaringBitmap,
|
2021-05-03 21:17:24 +08:00
|
|
|
distribution: &mut BTreeMap<String, u64>,
|
2021-06-17 00:33:33 +08:00
|
|
|
) -> heed::Result<()> {
|
2021-01-27 23:13:49 +08:00
|
|
|
match facet_type {
|
2021-04-07 17:57:16 +08:00
|
|
|
FacetType::Number => {
|
2021-07-15 16:19:35 +08:00
|
|
|
let mut key_buffer: Vec<_> = field_id.to_be_bytes().iter().copied().collect();
|
|
|
|
|
2022-05-18 20:51:00 +08:00
|
|
|
let distribution_prelength = distribution.len();
|
2021-05-03 21:17:24 +08:00
|
|
|
let db = self.index.field_id_docid_facet_f64s;
|
2021-07-15 16:19:35 +08:00
|
|
|
for docid in candidates.into_iter() {
|
|
|
|
key_buffer.truncate(mem::size_of::<FieldId>());
|
|
|
|
key_buffer.extend_from_slice(&docid.to_be_bytes());
|
|
|
|
let iter = db
|
|
|
|
.remap_key_type::<ByteSlice>()
|
|
|
|
.prefix_iter(self.rtxn, &key_buffer)?
|
|
|
|
.remap_key_type::<FieldDocIdFacetF64Codec>();
|
|
|
|
|
|
|
|
for result in iter {
|
|
|
|
let ((_, _, value), ()) = result?;
|
|
|
|
*distribution.entry(value.to_string()).or_insert(0) += 1;
|
2022-05-18 20:51:00 +08:00
|
|
|
|
2022-06-08 23:28:23 +08:00
|
|
|
if distribution.len() - distribution_prelength == self.max_values_per_facet
|
|
|
|
{
|
2022-05-18 20:51:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-07-15 16:19:35 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2021-05-03 21:17:24 +08:00
|
|
|
FacetType::String => {
|
2021-07-15 16:19:35 +08:00
|
|
|
let mut normalized_distribution = BTreeMap::new();
|
|
|
|
let mut key_buffer: Vec<_> = field_id.to_be_bytes().iter().copied().collect();
|
|
|
|
|
2021-05-03 21:17:24 +08:00
|
|
|
let db = self.index.field_id_docid_facet_strings;
|
2021-07-15 16:19:35 +08:00
|
|
|
for docid in candidates.into_iter() {
|
|
|
|
key_buffer.truncate(mem::size_of::<FieldId>());
|
|
|
|
key_buffer.extend_from_slice(&docid.to_be_bytes());
|
|
|
|
let iter = db
|
|
|
|
.remap_key_type::<ByteSlice>()
|
|
|
|
.prefix_iter(self.rtxn, &key_buffer)?
|
|
|
|
.remap_key_type::<FieldDocIdFacetStringCodec>();
|
|
|
|
|
|
|
|
for result in iter {
|
|
|
|
let ((_, _, normalized_value), original_value) = result?;
|
|
|
|
let (_, count) = normalized_distribution
|
|
|
|
.entry(normalized_value)
|
|
|
|
.or_insert_with(|| (original_value, 0));
|
|
|
|
*count += 1;
|
2022-05-18 20:51:00 +08:00
|
|
|
|
2022-06-08 23:28:23 +08:00
|
|
|
if normalized_distribution.len() == self.max_values_per_facet {
|
2022-05-18 20:51:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-07-15 16:19:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let iter = normalized_distribution
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_normalized, (original, count))| (original.to_string(), count));
|
|
|
|
distribution.extend(iter);
|
2021-05-03 21:17:24 +08:00
|
|
|
}
|
2021-01-27 23:13:49 +08:00
|
|
|
}
|
2021-07-15 16:19:35 +08:00
|
|
|
|
|
|
|
Ok(())
|
2021-01-27 23:13:49 +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-05-03 21:17:24 +08:00
|
|
|
fn facet_numbers_distribution_from_facet_levels(
|
2021-01-27 23:13:49 +08:00
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
candidates: &RoaringBitmap,
|
2021-05-03 21:17:24 +08:00
|
|
|
distribution: &mut BTreeMap<String, u64>,
|
2021-06-17 00:33:33 +08:00
|
|
|
) -> heed::Result<()> {
|
2022-08-31 13:50:18 +08:00
|
|
|
facet_distribution_iter::iterate_over_facet_distribution(
|
|
|
|
self.rtxn,
|
2022-09-05 19:01:36 +08:00
|
|
|
self.index.facet_id_f64_docids.remap_key_type::<FacetGroupKeyCodec<ByteSliceRef>>(),
|
2022-08-31 13:50:18 +08:00
|
|
|
field_id,
|
|
|
|
candidates,
|
|
|
|
|facet_key, nbr_docids| {
|
|
|
|
let facet_key = OrderedF64Codec::bytes_decode(facet_key).unwrap();
|
|
|
|
distribution.insert(facet_key.to_string(), nbr_docids);
|
|
|
|
if distribution.len() == self.max_values_per_facet {
|
|
|
|
ControlFlow::Break(())
|
|
|
|
} else {
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2021-01-27 23:13:49 +08:00
|
|
|
}
|
|
|
|
|
2021-07-05 00:11:26 +08:00
|
|
|
fn facet_strings_distribution_from_facet_levels(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
candidates: &RoaringBitmap,
|
|
|
|
distribution: &mut BTreeMap<String, u64>,
|
|
|
|
) -> heed::Result<()> {
|
2022-08-31 13:50:18 +08:00
|
|
|
facet_distribution_iter::iterate_over_facet_distribution(
|
|
|
|
self.rtxn,
|
2022-09-05 19:01:36 +08:00
|
|
|
self.index.facet_id_string_docids.remap_key_type::<FacetGroupKeyCodec<ByteSliceRef>>(),
|
2022-08-31 13:50:18 +08:00
|
|
|
field_id,
|
|
|
|
candidates,
|
|
|
|
|facet_key, nbr_docids| {
|
|
|
|
let facet_key = StrRefCodec::bytes_decode(facet_key).unwrap();
|
|
|
|
distribution.insert(facet_key.to_string(), nbr_docids);
|
|
|
|
if distribution.len() == self.max_values_per_facet {
|
|
|
|
ControlFlow::Break(())
|
|
|
|
} else {
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2021-07-05 00:11:26 +08:00
|
|
|
}
|
|
|
|
|
2021-01-27 23:13:49 +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.
|
|
|
|
fn facet_values_from_raw_facet_database(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
2021-06-17 00:33:33 +08:00
|
|
|
) -> heed::Result<BTreeMap<String, u64>> {
|
2022-08-31 13:50:18 +08:00
|
|
|
let mut distribution = BTreeMap::new();
|
|
|
|
|
|
|
|
let db = self.index.facet_id_f64_docids;
|
|
|
|
let mut prefix = vec![];
|
|
|
|
prefix.extend_from_slice(&field_id.to_be_bytes());
|
|
|
|
prefix.push(0);
|
|
|
|
let iter = db
|
|
|
|
.as_polymorph()
|
|
|
|
.prefix_iter::<_, ByteSlice, ByteSlice>(self.rtxn, prefix.as_slice())?
|
2022-09-05 19:01:36 +08:00
|
|
|
.remap_types::<FacetGroupKeyCodec<OrderedF64Codec>, FacetGroupValueCodec>();
|
2022-08-31 13:50:18 +08:00
|
|
|
|
|
|
|
for result in iter {
|
|
|
|
let (key, value) = result?;
|
|
|
|
distribution.insert(key.left_bound.to_string(), value.bitmap.len());
|
|
|
|
if distribution.len() == self.max_values_per_facet {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let iter = self
|
|
|
|
.index
|
|
|
|
.facet_id_string_docids
|
|
|
|
.as_polymorph()
|
|
|
|
.prefix_iter::<_, ByteSlice, ByteSlice>(self.rtxn, prefix.as_slice())?
|
2022-09-05 19:01:36 +08:00
|
|
|
.remap_types::<FacetGroupKeyCodec<StrRefCodec>, FacetGroupValueCodec>();
|
2022-08-31 13:50:18 +08:00
|
|
|
|
|
|
|
// TODO: get the original value of the facet somewhere (in the documents DB?)
|
|
|
|
for result in iter {
|
|
|
|
let (key, value) = result?;
|
|
|
|
distribution.insert(key.left_bound.to_owned(), value.bitmap.len());
|
|
|
|
if distribution.len() == self.max_values_per_facet {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(distribution)
|
2021-01-27 23:13:49 +08:00
|
|
|
}
|
2021-01-03 19:59:16 +08:00
|
|
|
|
2021-05-03 21:17:24 +08:00
|
|
|
fn facet_values(&self, field_id: FieldId) -> heed::Result<BTreeMap<String, u64>> {
|
|
|
|
use FacetType::{Number, String};
|
|
|
|
|
2021-06-23 17:33:30 +08:00
|
|
|
match self.candidates {
|
|
|
|
Some(ref candidates) => {
|
|
|
|
// Classic search, candidates were specified, we must return facet values only related
|
|
|
|
// to those candidates. We also enter here for facet strings for performance reasons.
|
|
|
|
let mut distribution = BTreeMap::new();
|
|
|
|
if candidates.len() <= CANDIDATES_THRESHOLD {
|
|
|
|
self.facet_distribution_from_documents(
|
|
|
|
field_id,
|
|
|
|
Number,
|
|
|
|
candidates,
|
|
|
|
&mut distribution,
|
|
|
|
)?;
|
|
|
|
self.facet_distribution_from_documents(
|
|
|
|
field_id,
|
|
|
|
String,
|
|
|
|
candidates,
|
|
|
|
&mut distribution,
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
self.facet_numbers_distribution_from_facet_levels(
|
|
|
|
field_id,
|
|
|
|
candidates,
|
|
|
|
&mut distribution,
|
|
|
|
)?;
|
2021-07-05 00:11:26 +08:00
|
|
|
self.facet_strings_distribution_from_facet_levels(
|
2021-06-23 17:33:30 +08:00
|
|
|
field_id,
|
|
|
|
candidates,
|
|
|
|
&mut distribution,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(distribution)
|
|
|
|
}
|
|
|
|
None => self.facet_values_from_raw_facet_database(field_id),
|
2021-01-03 21:11:00 +08:00
|
|
|
}
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 22:46:19 +08:00
|
|
|
pub fn execute(&self) -> Result<BTreeMap<String, BTreeMap<String, u64>>> {
|
2020-12-29 02:08:53 +08:00
|
|
|
let fields_ids_map = self.index.fields_ids_map(self.rtxn)?;
|
2021-06-01 18:19:55 +08:00
|
|
|
let filterable_fields = self.index.filterable_fields(self.rtxn)?;
|
2022-03-24 00:28:41 +08:00
|
|
|
|
2021-06-23 17:50:49 +08:00
|
|
|
let fields = match self.facets {
|
|
|
|
Some(ref facets) => {
|
2022-03-24 00:28:41 +08:00
|
|
|
let invalid_fields: HashSet<_> = facets
|
|
|
|
.iter()
|
|
|
|
.filter(|facet| !crate::is_faceted(facet, &filterable_fields))
|
|
|
|
.collect();
|
2021-06-23 17:50:49 +08:00
|
|
|
if !invalid_fields.is_empty() {
|
2021-06-23 19:56:13 +08:00
|
|
|
return Err(UserError::InvalidFacetsDistribution {
|
|
|
|
invalid_facets_name: invalid_fields.into_iter().cloned().collect(),
|
|
|
|
}
|
|
|
|
.into());
|
2021-06-23 17:50:49 +08:00
|
|
|
} else {
|
|
|
|
facets.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => filterable_fields,
|
|
|
|
};
|
2021-05-03 21:17:24 +08:00
|
|
|
|
|
|
|
let mut distribution = BTreeMap::new();
|
2022-03-24 00:28:41 +08:00
|
|
|
for (fid, name) in fields_ids_map.iter() {
|
|
|
|
if crate::is_faceted(name, &fields) {
|
2021-07-22 23:11:17 +08:00
|
|
|
let values = self.facet_values(fid)?;
|
2022-04-26 23:59:53 +08:00
|
|
|
distribution.insert(name.to_string(), values);
|
2021-07-22 23:11:17 +08:00
|
|
|
}
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:17:24 +08:00
|
|
|
Ok(distribution)
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for FacetDistribution<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-06-08 23:28:23 +08:00
|
|
|
let FacetDistribution { facets, candidates, max_values_per_facet, rtxn: _, index: _ } =
|
|
|
|
self;
|
2021-01-03 19:59:16 +08:00
|
|
|
|
2020-12-29 02:08:53 +08:00
|
|
|
f.debug_struct("FacetDistribution")
|
|
|
|
.field("facets", facets)
|
|
|
|
.field("candidates", candidates)
|
2022-06-08 23:28:23 +08:00
|
|
|
.field("max_values_per_facet", max_values_per_facet)
|
2020-12-29 02:08:53 +08:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|