2021-06-16 18:33:33 +02:00
|
|
|
use std::collections::{BTreeMap, HashSet};
|
2020-12-28 19:08:53 +01:00
|
|
|
use std::ops::Bound::Unbounded;
|
2021-01-13 11:59:16 +01:00
|
|
|
use std::{cmp, fmt};
|
2020-12-28 19:08:53 +01:00
|
|
|
|
2021-05-03 15:17:24 +02:00
|
|
|
use heed::types::{ByteSlice, Unit};
|
2021-06-16 18:33:33 +02:00
|
|
|
use heed::{BytesDecode, Database};
|
2020-12-28 19:08:53 +01:00
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2021-06-23 13:56:13 +02:00
|
|
|
use crate::error::{FieldIdMapMissingEntry, UserError};
|
2021-05-03 15:17:24 +02:00
|
|
|
use crate::facet::FacetType;
|
|
|
|
use crate::heed_codec::facet::FacetValueStringCodec;
|
2021-01-06 15:10:30 +01:00
|
|
|
use crate::search::facet::{FacetIter, FacetRange};
|
2021-06-16 18:33:33 +02:00
|
|
|
use crate::{DocumentId, FieldId, Index, Result};
|
2020-12-28 19:08:53 +01:00
|
|
|
|
2021-01-27 14:32:30 +01: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-28 19:08:53 +01:00
|
|
|
pub struct FacetDistribution<'a> {
|
|
|
|
facets: Option<HashSet<String>>,
|
|
|
|
candidates: Option<RoaringBitmap>,
|
2021-01-03 12:59:16 +01:00
|
|
|
max_values_by_facet: usize,
|
2020-12-28 19:08:53 +01: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 14:32:30 +01:00
|
|
|
FacetDistribution {
|
|
|
|
facets: None,
|
|
|
|
candidates: None,
|
|
|
|
max_values_by_facet: DEFAULT_VALUES_BY_FACET,
|
|
|
|
rtxn,
|
|
|
|
index,
|
|
|
|
}
|
2021-01-03 12:59:16 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
pub fn facets<I: IntoIterator<Item = A>, A: AsRef<str>>(&mut self, names: I) -> &mut Self {
|
2021-01-03 12:59:16 +01:00
|
|
|
self.facets = Some(names.into_iter().map(|s| s.as_ref().to_string()).collect());
|
|
|
|
self
|
2020-12-28 19:08:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn candidates(&mut self, candidates: RoaringBitmap) -> &mut Self {
|
|
|
|
self.candidates = Some(candidates);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-03 12:59:16 +01:00
|
|
|
pub fn max_values_by_facet(&mut self, max: usize) -> &mut Self {
|
2021-01-27 14:32:30 +01:00
|
|
|
self.max_values_by_facet = cmp::min(max, MAX_VALUES_BY_FACET);
|
2020-12-28 19:08:53 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:13:49 +01: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 15:17:24 +02:00
|
|
|
fn facet_distribution_from_documents(
|
2021-01-26 14:14:37 +01:00
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
facet_type: FacetType,
|
2021-01-27 16:13:49 +01:00
|
|
|
candidates: &RoaringBitmap,
|
2021-05-03 15:17:24 +02:00
|
|
|
distribution: &mut BTreeMap<String, u64>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> heed::Result<()> {
|
2021-01-27 18:31:09 +01:00
|
|
|
fn fetch_facet_values<'t, KC, K: 't>(
|
|
|
|
rtxn: &'t heed::RoTxn,
|
2021-05-03 15:17:24 +02:00
|
|
|
db: Database<KC, Unit>,
|
2021-01-27 18:31:09 +01:00
|
|
|
field_id: FieldId,
|
|
|
|
candidates: &RoaringBitmap,
|
2021-05-03 15:17:24 +02:00
|
|
|
distribution: &mut BTreeMap<String, u64>,
|
|
|
|
) -> heed::Result<()>
|
2021-01-27 18:31:09 +01:00
|
|
|
where
|
2021-05-03 15:17:24 +02:00
|
|
|
K: fmt::Display,
|
2021-01-27 18:31:09 +01:00
|
|
|
KC: BytesDecode<'t, DItem = (FieldId, DocumentId, K)>,
|
|
|
|
{
|
|
|
|
let mut key_buffer = vec![field_id];
|
|
|
|
|
|
|
|
for docid in candidates.into_iter().take(CANDIDATES_THRESHOLD as usize) {
|
|
|
|
key_buffer.truncate(1);
|
|
|
|
key_buffer.extend_from_slice(&docid.to_be_bytes());
|
2021-05-03 15:17:24 +02:00
|
|
|
let iter = db
|
|
|
|
.remap_key_type::<ByteSlice>()
|
2021-01-27 18:31:09 +01:00
|
|
|
.prefix_iter(rtxn, &key_buffer)?
|
|
|
|
.remap_key_type::<KC>();
|
|
|
|
|
|
|
|
for result in iter {
|
|
|
|
let ((_, _, value), ()) = result?;
|
2021-05-03 15:17:24 +02:00
|
|
|
*distribution.entry(value.to_string()).or_insert(0) += 1;
|
2021-01-27 18:31:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 15:17:24 +02:00
|
|
|
Ok(())
|
2021-01-27 18:31:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:13:49 +01:00
|
|
|
match facet_type {
|
2021-04-07 11:57:16 +02:00
|
|
|
FacetType::Number => {
|
2021-05-03 15:17:24 +02:00
|
|
|
let db = self.index.field_id_docid_facet_f64s;
|
|
|
|
fetch_facet_values(self.rtxn, db, field_id, candidates, distribution)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-05-03 15:17:24 +02:00
|
|
|
FacetType::String => {
|
|
|
|
let db = self.index.field_id_docid_facet_strings;
|
|
|
|
fetch_facet_values(self.rtxn, db, field_id, candidates, distribution)
|
|
|
|
}
|
2021-01-27 16:13:49 +01: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 15:17:24 +02:00
|
|
|
fn facet_numbers_distribution_from_facet_levels(
|
2021-01-27 16:13:49 +01:00
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
candidates: &RoaringBitmap,
|
2021-05-03 15:17:24 +02:00
|
|
|
distribution: &mut BTreeMap<String, u64>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> heed::Result<()> {
|
|
|
|
let iter =
|
|
|
|
FacetIter::new_non_reducing(self.rtxn, self.index, field_id, candidates.clone())?;
|
2021-01-27 16:13:49 +01:00
|
|
|
|
|
|
|
for result in iter {
|
|
|
|
let (value, mut docids) = result?;
|
2021-06-30 14:12:56 +02:00
|
|
|
docids &= candidates;
|
2021-01-27 16:13:49 +01:00
|
|
|
if !docids.is_empty() {
|
2021-05-03 15:17:24 +02:00
|
|
|
distribution.insert(value.to_string(), docids.len());
|
2021-01-03 12:59:16 +01:00
|
|
|
}
|
2021-05-03 15:17:24 +02:00
|
|
|
if distribution.len() == self.max_values_by_facet {
|
2021-01-27 16:13:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 15:17:24 +02:00
|
|
|
Ok(())
|
2021-01-27 16:13:49 +01: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-16 18:33:33 +02:00
|
|
|
) -> heed::Result<BTreeMap<String, u64>> {
|
2021-05-03 15:17:24 +02:00
|
|
|
let mut distribution = BTreeMap::new();
|
|
|
|
|
|
|
|
let db = self.index.facet_id_f64_docids;
|
|
|
|
let range = FacetRange::new(self.rtxn, db, field_id, 0, Unbounded, Unbounded)?;
|
|
|
|
|
|
|
|
for result in range {
|
|
|
|
let ((_, _, value, _), docids) = result?;
|
|
|
|
distribution.insert(value.to_string(), docids.len());
|
|
|
|
if distribution.len() == self.max_values_by_facet {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
let iter = self
|
|
|
|
.index
|
2021-05-03 15:17:24 +02:00
|
|
|
.facet_id_string_docids
|
|
|
|
.remap_key_type::<ByteSlice>()
|
|
|
|
.prefix_iter(self.rtxn, &[field_id])?
|
|
|
|
.remap_key_type::<FacetValueStringCodec>();
|
2021-01-27 16:13:49 +01:00
|
|
|
|
|
|
|
for result in iter {
|
2021-05-03 15:17:24 +02:00
|
|
|
let ((_, value), docids) = result?;
|
|
|
|
distribution.insert(value.to_string(), docids.len());
|
|
|
|
if distribution.len() == self.max_values_by_facet {
|
2021-01-27 16:13:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 15:17:24 +02:00
|
|
|
Ok(distribution)
|
2021-01-27 16:13:49 +01:00
|
|
|
}
|
2021-01-03 12:59:16 +01:00
|
|
|
|
2021-05-03 15:17:24 +02:00
|
|
|
fn facet_values(&self, field_id: FieldId) -> heed::Result<BTreeMap<String, u64>> {
|
|
|
|
use FacetType::{Number, String};
|
|
|
|
|
2021-06-23 11:33:30 +02: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,
|
|
|
|
)?;
|
|
|
|
self.facet_distribution_from_documents(
|
|
|
|
field_id,
|
|
|
|
String,
|
|
|
|
candidates,
|
|
|
|
&mut distribution,
|
|
|
|
)?;
|
|
|
|
}
|
2021-05-03 15:17:24 +02:00
|
|
|
|
2021-06-23 11:33:30 +02:00
|
|
|
Ok(distribution)
|
|
|
|
}
|
|
|
|
None => self.facet_values_from_raw_facet_database(field_id),
|
2021-01-03 14:11:00 +01:00
|
|
|
}
|
2020-12-28 19:08:53 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:46:19 +02:00
|
|
|
pub fn execute(&self) -> Result<BTreeMap<String, BTreeMap<String, u64>>> {
|
2020-12-28 19:08:53 +01:00
|
|
|
let fields_ids_map = self.index.fields_ids_map(self.rtxn)?;
|
2021-06-01 12:19:55 +02:00
|
|
|
let filterable_fields = self.index.filterable_fields(self.rtxn)?;
|
2021-06-23 11:50:49 +02:00
|
|
|
let fields = match self.facets {
|
|
|
|
Some(ref facets) => {
|
|
|
|
let invalid_fields: HashSet<_> = facets.difference(&filterable_fields).collect();
|
|
|
|
if !invalid_fields.is_empty() {
|
2021-06-23 13:56:13 +02:00
|
|
|
return Err(UserError::InvalidFacetsDistribution {
|
|
|
|
invalid_facets_name: invalid_fields.into_iter().cloned().collect(),
|
|
|
|
}
|
|
|
|
.into());
|
2021-06-23 11:50:49 +02:00
|
|
|
} else {
|
|
|
|
facets.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => filterable_fields,
|
|
|
|
};
|
2021-05-03 15:17:24 +02:00
|
|
|
|
|
|
|
let mut distribution = BTreeMap::new();
|
2021-06-23 11:50:49 +02:00
|
|
|
for name in fields {
|
2021-06-16 18:33:33 +02:00
|
|
|
let fid =
|
|
|
|
fields_ids_map.id(&name).ok_or_else(|| FieldIdMapMissingEntry::FieldName {
|
|
|
|
field_name: name.clone(),
|
|
|
|
process: "FacetDistribution::execute",
|
|
|
|
})?;
|
2021-05-03 15:17:24 +02:00
|
|
|
let values = self.facet_values(fid)?;
|
|
|
|
distribution.insert(name, values);
|
2020-12-28 19:08:53 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 15:17:24 +02:00
|
|
|
Ok(distribution)
|
2020-12-28 19:08:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for FacetDistribution<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-06-16 18:33:33 +02:00
|
|
|
let FacetDistribution { facets, candidates, max_values_by_facet, rtxn: _, index: _ } = self;
|
2021-01-03 12:59:16 +01:00
|
|
|
|
2020-12-28 19:08:53 +01:00
|
|
|
f.debug_struct("FacetDistribution")
|
|
|
|
.field("facets", facets)
|
|
|
|
.field("candidates", candidates)
|
2021-01-03 12:59:16 +01:00
|
|
|
.field("max_values_by_facet", max_values_by_facet)
|
2020-12-28 19:08:53 +01:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|