2023-06-22 23:13:40 +08:00
|
|
|
use std::collections::{BTreeMap, HashMap, 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;
|
2023-05-29 21:47:45 +08:00
|
|
|
use indexmap::IndexMap;
|
2020-12-29 02:08:53 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2023-06-22 23:13:40 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-12-29 02:08:53 +08:00
|
|
|
|
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 23:31:26 +08:00
|
|
|
use crate::heed_codec::facet::{
|
2023-05-29 19:39:06 +08:00
|
|
|
FacetGroupKeyCodec, FieldDocIdFacetF64Codec, FieldDocIdFacetStringCodec, OrderedF64Codec,
|
2022-09-05 23:31:26 +08:00
|
|
|
};
|
2022-10-12 15:42:55 +08:00
|
|
|
use crate::heed_codec::{ByteSliceRefCodec, StrRefCodec};
|
2023-05-29 21:32:09 +08:00
|
|
|
use crate::search::facet::facet_distribution_iter::{
|
2023-05-29 19:39:06 +08:00
|
|
|
count_iterate_over_facet_distribution, lexicographically_iterate_over_facet_distribution,
|
|
|
|
};
|
2023-05-29 21:32:09 +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
|
|
|
|
2023-05-29 19:39:06 +08:00
|
|
|
/// How should we fetch the facets?
|
2023-06-22 23:13:40 +08:00
|
|
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
2023-05-29 19:39:06 +08:00
|
|
|
pub enum OrderBy {
|
|
|
|
/// By lexicographic order...
|
2023-05-29 21:32:09 +08:00
|
|
|
#[default]
|
2023-05-29 19:39:06 +08:00
|
|
|
Lexicographic,
|
|
|
|
/// Or by number of docids in common?
|
|
|
|
Count,
|
|
|
|
}
|
|
|
|
|
2020-12-29 02:08:53 +08:00
|
|
|
pub struct FacetDistribution<'a> {
|
2023-06-22 23:13:40 +08:00
|
|
|
facets: Option<HashMap<String, OrderBy>>,
|
2020-12-29 02:08:53 +08:00
|
|
|
candidates: Option<RoaringBitmap>,
|
2022-06-08 23:28:23 +08:00
|
|
|
max_values_per_facet: usize,
|
2023-06-22 23:13:40 +08:00
|
|
|
default_order_by: OrderBy,
|
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,
|
2023-06-22 23:13:40 +08:00
|
|
|
default_order_by: OrderBy::default(),
|
2022-05-18 20:51:00 +08:00
|
|
|
rtxn,
|
|
|
|
index,
|
|
|
|
}
|
2021-01-03 19:59:16 +08:00
|
|
|
}
|
|
|
|
|
2023-06-22 23:13:40 +08:00
|
|
|
pub fn facets<I: IntoIterator<Item = (A, OrderBy)>, A: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
names_ordered_by: I,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.facets = Some(
|
|
|
|
names_ordered_by
|
|
|
|
.into_iter()
|
|
|
|
.map(|(name, order_by)| (name.as_ref().to_string(), order_by))
|
|
|
|
.collect(),
|
|
|
|
);
|
2021-01-03 19:59:16 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-22 23:13:40 +08:00
|
|
|
pub fn default_order_by(&mut self, order_by: OrderBy) -> &mut Self {
|
|
|
|
self.default_order_by = order_by;
|
2023-05-29 19:39:06 +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,
|
2023-05-29 21:47:45 +08:00
|
|
|
distribution: &mut IndexMap<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 => {
|
2022-10-25 03:34:13 +08:00
|
|
|
let mut key_buffer: Vec<_> = field_id.to_be_bytes().to_vec();
|
2021-07-15 16:19:35 +08:00
|
|
|
|
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;
|
2023-03-23 16:39:16 +08:00
|
|
|
for docid in candidates {
|
2021-07-15 16:19:35 +08:00
|
|
|
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();
|
2022-10-25 03:34:13 +08:00
|
|
|
let mut key_buffer: Vec<_> = field_id.to_be_bytes().to_vec();
|
2021-07-15 16:19:35 +08:00
|
|
|
|
2021-05-03 21:17:24 +08:00
|
|
|
let db = self.index.field_id_docid_facet_strings;
|
2023-03-23 16:39:16 +08:00
|
|
|
'outer: for docid in candidates {
|
2021-07-15 16:19:35 +08:00
|
|
|
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-09-08 00:00:00 +08:00
|
|
|
break 'outer;
|
2022-05-18 20:51:00 +08:00
|
|
|
}
|
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,
|
2023-05-29 19:39:06 +08:00
|
|
|
order_by: OrderBy,
|
2023-05-29 21:47:45 +08:00
|
|
|
distribution: &mut IndexMap<String, u64>,
|
2021-06-17 00:33:33 +08:00
|
|
|
) -> heed::Result<()> {
|
2023-05-29 19:39:06 +08:00
|
|
|
let search_function = match order_by {
|
|
|
|
OrderBy::Lexicographic => lexicographically_iterate_over_facet_distribution,
|
|
|
|
OrderBy::Count => count_iterate_over_facet_distribution,
|
|
|
|
};
|
|
|
|
|
|
|
|
search_function(
|
2022-08-31 13:50:18 +08:00
|
|
|
self.rtxn,
|
2022-10-12 15:42:55 +08:00
|
|
|
self.index
|
|
|
|
.facet_id_f64_docids
|
|
|
|
.remap_key_type::<FacetGroupKeyCodec<ByteSliceRefCodec>>(),
|
2022-08-31 13:50:18 +08:00
|
|
|
field_id,
|
|
|
|
candidates,
|
2022-09-07 23:56:38 +08:00
|
|
|
|facet_key, nbr_docids, _| {
|
2022-08-31 13:50:18 +08:00
|
|
|
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 {
|
2022-09-07 23:56:38 +08:00
|
|
|
Ok(ControlFlow::Break(()))
|
2022-08-31 13:50:18 +08:00
|
|
|
} else {
|
2022-09-07 23:56:38 +08:00
|
|
|
Ok(ControlFlow::Continue(()))
|
2022-08-31 13:50:18 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
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,
|
2023-05-29 19:39:06 +08:00
|
|
|
order_by: OrderBy,
|
2023-05-29 21:47:45 +08:00
|
|
|
distribution: &mut IndexMap<String, u64>,
|
2021-07-05 00:11:26 +08:00
|
|
|
) -> heed::Result<()> {
|
2023-05-29 19:39:06 +08:00
|
|
|
let search_function = match order_by {
|
|
|
|
OrderBy::Lexicographic => lexicographically_iterate_over_facet_distribution,
|
|
|
|
OrderBy::Count => count_iterate_over_facet_distribution,
|
|
|
|
};
|
|
|
|
|
|
|
|
search_function(
|
2022-08-31 13:50:18 +08:00
|
|
|
self.rtxn,
|
2022-10-12 15:42:55 +08:00
|
|
|
self.index
|
|
|
|
.facet_id_string_docids
|
|
|
|
.remap_key_type::<FacetGroupKeyCodec<ByteSliceRefCodec>>(),
|
2022-08-31 13:50:18 +08:00
|
|
|
field_id,
|
|
|
|
candidates,
|
2022-09-07 23:56:38 +08:00
|
|
|
|facet_key, nbr_docids, any_docid| {
|
2022-08-31 13:50:18 +08:00
|
|
|
let facet_key = StrRefCodec::bytes_decode(facet_key).unwrap();
|
2022-09-07 23:56:38 +08:00
|
|
|
|
|
|
|
let key: (FieldId, _, &str) = (field_id, any_docid, facet_key);
|
|
|
|
let original_string = self
|
|
|
|
.index
|
|
|
|
.field_id_docid_facet_strings
|
|
|
|
.get(self.rtxn, &key)?
|
|
|
|
.unwrap()
|
|
|
|
.to_owned();
|
|
|
|
|
|
|
|
distribution.insert(original_string, nbr_docids);
|
2022-08-31 13:50:18 +08:00
|
|
|
if distribution.len() == self.max_values_per_facet {
|
2022-09-07 23:56:38 +08:00
|
|
|
Ok(ControlFlow::Break(()))
|
2022-08-31 13:50:18 +08:00
|
|
|
} else {
|
2022-09-07 23:56:38 +08:00
|
|
|
Ok(ControlFlow::Continue(()))
|
2022-08-31 13:50:18 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2021-07-05 00:11:26 +08:00
|
|
|
}
|
|
|
|
|
2023-06-22 23:13:40 +08:00
|
|
|
fn facet_values(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
order_by: OrderBy,
|
|
|
|
) -> heed::Result<IndexMap<String, u64>> {
|
2023-05-29 19:39:06 +08:00
|
|
|
use FacetType::{Number, String};
|
2022-08-31 13:50:18 +08:00
|
|
|
|
2023-05-29 21:47:45 +08:00
|
|
|
let mut distribution = IndexMap::new();
|
2023-06-22 23:13:40 +08:00
|
|
|
match (order_by, &self.candidates) {
|
2023-05-29 19:39:06 +08:00
|
|
|
(OrderBy::Lexicographic, Some(cnd)) if cnd.len() <= CANDIDATES_THRESHOLD => {
|
|
|
|
// 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.
|
|
|
|
self.facet_distribution_from_documents(field_id, Number, cnd, &mut distribution)?;
|
|
|
|
self.facet_distribution_from_documents(field_id, String, cnd, &mut distribution)?;
|
2022-08-31 13:50:18 +08:00
|
|
|
}
|
2023-05-29 19:39:06 +08:00
|
|
|
_ => {
|
|
|
|
let universe;
|
|
|
|
let candidates;
|
|
|
|
match &self.candidates {
|
|
|
|
Some(cnd) => candidates = cnd,
|
|
|
|
None => {
|
|
|
|
universe = self.index.documents_ids(self.rtxn)?;
|
|
|
|
candidates = &universe;
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 23:56:38 +08:00
|
|
|
|
2023-05-29 19:39:06 +08:00
|
|
|
self.facet_numbers_distribution_from_facet_levels(
|
|
|
|
field_id,
|
|
|
|
candidates,
|
2023-06-22 23:13:40 +08:00
|
|
|
order_by,
|
2023-05-29 19:39:06 +08:00
|
|
|
&mut distribution,
|
|
|
|
)?;
|
|
|
|
self.facet_strings_distribution_from_facet_levels(
|
|
|
|
field_id,
|
|
|
|
candidates,
|
2023-06-22 23:13:40 +08:00
|
|
|
order_by,
|
2023-05-29 19:39:06 +08:00
|
|
|
&mut distribution,
|
|
|
|
)?;
|
2022-08-31 13:50:18 +08:00
|
|
|
}
|
2023-05-29 17:52:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(distribution)
|
2020-12-29 02:08:53 +08:00
|
|
|
}
|
|
|
|
|
2023-01-25 22:04:08 +08:00
|
|
|
pub fn compute_stats(&self) -> Result<BTreeMap<String, (f64, f64)>> {
|
|
|
|
let fields_ids_map = self.index.fields_ids_map(self.rtxn)?;
|
|
|
|
let filterable_fields = self.index.filterable_fields(self.rtxn)?;
|
|
|
|
let candidates = if let Some(candidates) = self.candidates.clone() {
|
|
|
|
candidates
|
|
|
|
} else {
|
|
|
|
return Ok(Default::default());
|
|
|
|
};
|
|
|
|
|
|
|
|
let fields = match &self.facets {
|
|
|
|
Some(facets) => {
|
|
|
|
let invalid_fields: HashSet<_> = facets
|
|
|
|
.iter()
|
2023-06-22 23:13:40 +08:00
|
|
|
.map(|(name, _)| name)
|
2023-01-25 22:04:08 +08:00
|
|
|
.filter(|facet| !crate::is_faceted(facet, &filterable_fields))
|
|
|
|
.collect();
|
|
|
|
if !invalid_fields.is_empty() {
|
|
|
|
return Err(UserError::InvalidFacetsDistribution {
|
|
|
|
invalid_facets_name: invalid_fields.into_iter().cloned().collect(),
|
|
|
|
valid_facets_name: filterable_fields.into_iter().collect(),
|
|
|
|
}
|
|
|
|
.into());
|
|
|
|
} else {
|
2023-06-23 00:26:38 +08:00
|
|
|
facets.iter().map(|(name, _)| name).cloned().collect()
|
2023-01-25 22:04:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => filterable_fields,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut distribution = BTreeMap::new();
|
|
|
|
for (fid, name) in fields_ids_map.iter() {
|
|
|
|
if crate::is_faceted(name, &fields) {
|
2023-03-23 16:35:53 +08:00
|
|
|
let min_value = if let Some(min_value) = crate::search::facet::facet_min_value(
|
2023-01-25 22:04:08 +08:00
|
|
|
self.index,
|
|
|
|
self.rtxn,
|
|
|
|
fid,
|
|
|
|
candidates.clone(),
|
|
|
|
)? {
|
|
|
|
min_value
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
2023-03-23 16:35:53 +08:00
|
|
|
let max_value = if let Some(max_value) = crate::search::facet::facet_max_value(
|
2023-01-25 22:04:08 +08:00
|
|
|
self.index,
|
|
|
|
self.rtxn,
|
|
|
|
fid,
|
|
|
|
candidates.clone(),
|
|
|
|
)? {
|
|
|
|
max_value
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
distribution.insert(name.to_string(), (min_value, max_value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(distribution)
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:47:45 +08:00
|
|
|
pub fn execute(&self) -> Result<BTreeMap<String, IndexMap<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()
|
2023-06-22 23:13:40 +08:00
|
|
|
.map(|(name, _)| name)
|
2022-03-24 00:28:41 +08:00
|
|
|
.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(),
|
2023-01-19 19:25:18 +08:00
|
|
|
valid_facets_name: filterable_fields.into_iter().collect(),
|
2021-06-23 19:56:13 +08:00
|
|
|
}
|
|
|
|
.into());
|
2021-06-23 17:50:49 +08:00
|
|
|
} else {
|
2023-06-23 00:26:38 +08:00
|
|
|
facets.iter().map(|(name, _)| name).cloned().collect()
|
2021-06-23 17:50:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2023-06-22 23:13:40 +08:00
|
|
|
let order_by = self
|
|
|
|
.facets
|
|
|
|
.as_ref()
|
2023-06-23 00:26:38 +08:00
|
|
|
.and_then(|facets| facets.get(name).copied())
|
2023-06-22 23:13:40 +08:00
|
|
|
.unwrap_or(self.default_order_by);
|
|
|
|
let values = self.facet_values(fid, order_by)?;
|
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 {
|
2023-05-29 19:39:06 +08:00
|
|
|
let FacetDistribution {
|
|
|
|
facets,
|
|
|
|
candidates,
|
|
|
|
max_values_per_facet,
|
2023-06-22 23:13:40 +08:00
|
|
|
default_order_by,
|
2023-05-29 19:39:06 +08:00
|
|
|
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)
|
2023-06-22 23:13:40 +08:00
|
|
|
.field("default_order_by", default_order_by)
|
2020-12-29 02:08:53 +08:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 23:56:38 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-06-27 21:42:17 +08:00
|
|
|
use std::iter;
|
|
|
|
|
2022-09-07 23:56:38 +08:00
|
|
|
use big_s::S;
|
|
|
|
use maplit::hashset;
|
|
|
|
|
2022-09-08 00:04:07 +08:00
|
|
|
use crate::documents::documents_batch_reader_from_objects;
|
|
|
|
use crate::index::tests::TempIndex;
|
2023-06-22 23:13:40 +08:00
|
|
|
use crate::{milli_snap, FacetDistribution, OrderBy};
|
2022-09-07 23:56:38 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn few_candidates_few_facet_values() {
|
|
|
|
// All the tests here avoid using the code in `facet_distribution_iter` because there aren't
|
|
|
|
// enough candidates.
|
|
|
|
|
|
|
|
let mut index = TempIndex::new();
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| settings.set_filterable_fields(hashset! { S("colour") }))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let documents = documents!([
|
|
|
|
{ "colour": "Blue" },
|
|
|
|
{ "colour": " blue" },
|
|
|
|
{ "colour": "RED" }
|
|
|
|
]);
|
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 2, "RED": 1}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.candidates([0, 1, 2].iter().copied().collect())
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 2, "RED": 1}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.candidates([1, 2].iter().copied().collect())
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// I think it would be fine if " blue" was "Blue" instead.
|
|
|
|
// We just need to get any non-normalised string I think, even if it's not in
|
|
|
|
// the candidates
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {" blue": 1, "RED": 1}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.candidates([2].iter().copied().collect())
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"RED": 1}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.candidates([0, 1, 2].iter().copied().collect())
|
|
|
|
.max_values_per_facet(1)
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
2022-09-08 00:00:00 +08:00
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 1}}"###);
|
2023-06-27 21:42:17 +08:00
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
|
|
|
.facets(iter::once(("colour", OrderBy::Count)))
|
|
|
|
.candidates([0, 1, 2].iter().copied().collect())
|
|
|
|
.max_values_per_facet(1)
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 2}}"###);
|
2022-09-07 23:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn many_candidates_few_facet_values() {
|
|
|
|
let mut index = TempIndex::new_with_map_size(4096 * 10_000);
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| settings.set_filterable_fields(hashset! { S("colour") }))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let facet_values = ["Red", "RED", " red ", "Blue", "BLUE"];
|
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..10_000 {
|
|
|
|
let document = serde_json::json!({
|
|
|
|
"colour": facet_values[i % 5],
|
|
|
|
})
|
|
|
|
.as_object()
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
|
|
|
documents.push(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 4000, "Red": 6000}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.max_values_per_facet(1)
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 4000}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..10_000).collect())
|
2022-09-07 23:56:38 +08:00
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 4000, "Red": 6000}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..5_000).collect())
|
2022-09-07 23:56:38 +08:00
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 2000, "Red": 3000}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..5_000).collect())
|
2022-09-07 23:56:38 +08:00
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 2000, "Red": 3000}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..5_000).collect())
|
2022-09-07 23:56:38 +08:00
|
|
|
.max_values_per_facet(1)
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Blue": 2000}}"###);
|
2023-06-27 21:42:17 +08:00
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
|
|
|
.facets(iter::once(("colour", OrderBy::Count)))
|
|
|
|
.candidates((0..5_000).collect())
|
|
|
|
.max_values_per_facet(1)
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), @r###"{"colour": {"Red": 3000}}"###);
|
2022-09-07 23:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn many_candidates_many_facet_values() {
|
|
|
|
let mut index = TempIndex::new_with_map_size(4096 * 10_000);
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| settings.set_filterable_fields(hashset! { S("colour") }))
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-23 16:39:16 +08:00
|
|
|
let facet_values = (0..1000).map(|x| format!("{x:x}")).collect::<Vec<_>>();
|
2022-09-07 23:56:38 +08:00
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..10_000 {
|
|
|
|
let document = serde_json::json!({
|
|
|
|
"colour": facet_values[i % 1000],
|
|
|
|
})
|
|
|
|
.as_object()
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
|
|
|
documents.push(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "no_candidates", @"ac9229ed5964d893af96a7076e2f8af5");
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2022-09-07 23:56:38 +08:00
|
|
|
.max_values_per_facet(2)
|
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "no_candidates_with_max_2", @r###"{"colour": {"0": 10, "1": 10}}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..10_000).collect())
|
2022-09-07 23:56:38 +08:00
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_0_10_000", @"ac9229ed5964d893af96a7076e2f8af5");
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..5_000).collect())
|
2022-09-07 23:56:38 +08:00
|
|
|
.execute()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_0_5_000", @"825f23a4090d05756f46176987b7d992");
|
|
|
|
}
|
2023-01-30 23:02:50 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn facet_stats() {
|
|
|
|
let mut index = TempIndex::new_with_map_size(4096 * 10_000);
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| settings.set_filterable_fields(hashset! { S("colour") }))
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-23 16:39:16 +08:00
|
|
|
let facet_values = (0..1000).collect::<Vec<_>>();
|
2023-01-30 23:02:50 +08:00
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..1000 {
|
|
|
|
let document = serde_json::json!({
|
|
|
|
"colour": facet_values[i % 1000],
|
|
|
|
})
|
|
|
|
.as_object()
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
|
|
|
documents.push(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "no_candidates", @"{}");
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..1000).collect())
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_0_1000", @r###"{"colour": (0.0, 999.0)}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((217..777).collect())
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_217_777", @r###"{"colour": (217.0, 776.0)}"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn facet_stats_array() {
|
|
|
|
let mut index = TempIndex::new_with_map_size(4096 * 10_000);
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| settings.set_filterable_fields(hashset! { S("colour") }))
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-23 16:39:16 +08:00
|
|
|
let facet_values = (0..1000).collect::<Vec<_>>();
|
2023-01-30 23:02:50 +08:00
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..1000 {
|
|
|
|
let document = serde_json::json!({
|
|
|
|
"colour": [facet_values[i % 1000], facet_values[i % 1000] + 1000],
|
|
|
|
})
|
|
|
|
.as_object()
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
|
|
|
documents.push(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "no_candidates", @"{}");
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..1000).collect())
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_0_1000", @r###"{"colour": (0.0, 1999.0)}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((217..777).collect())
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_217_777", @r###"{"colour": (217.0, 1776.0)}"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn facet_stats_mixed_array() {
|
|
|
|
let mut index = TempIndex::new_with_map_size(4096 * 10_000);
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| settings.set_filterable_fields(hashset! { S("colour") }))
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-23 16:39:16 +08:00
|
|
|
let facet_values = (0..1000).collect::<Vec<_>>();
|
2023-01-30 23:02:50 +08:00
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..1000 {
|
|
|
|
let document = serde_json::json!({
|
|
|
|
"colour": [facet_values[i % 1000], format!("{}", facet_values[i % 1000] + 1000)],
|
|
|
|
})
|
|
|
|
.as_object()
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
|
|
|
documents.push(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "no_candidates", @"{}");
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..1000).collect())
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_0_1000", @r###"{"colour": (0.0, 999.0)}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((217..777).collect())
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_217_777", @r###"{"colour": (217.0, 776.0)}"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn facet_mixed_values() {
|
|
|
|
let mut index = TempIndex::new_with_map_size(4096 * 10_000);
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| settings.set_filterable_fields(hashset! { S("colour") }))
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-23 16:39:16 +08:00
|
|
|
let facet_values = (0..1000).collect::<Vec<_>>();
|
2023-01-30 23:02:50 +08:00
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..1000 {
|
|
|
|
let document = if i % 2 == 0 {
|
|
|
|
serde_json::json!({
|
|
|
|
"colour": [facet_values[i % 1000], facet_values[i % 1000] + 1000],
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
serde_json::json!({
|
|
|
|
"colour": format!("{}", facet_values[i % 1000] + 10000),
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let document = document.as_object().unwrap().clone();
|
|
|
|
documents.push(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "no_candidates", @"{}");
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((0..1000).collect())
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_0_1000", @r###"{"colour": (0.0, 1998.0)}"###);
|
|
|
|
|
|
|
|
let map = FacetDistribution::new(&txn, &index)
|
2023-06-27 21:42:17 +08:00
|
|
|
.facets(iter::once(("colour", OrderBy::default())))
|
2023-03-23 16:39:16 +08:00
|
|
|
.candidates((217..777).collect())
|
2023-01-30 23:02:50 +08:00
|
|
|
.compute_stats()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{map:?}"), "candidates_217_777", @r###"{"colour": (218.0, 1776.0)}"###);
|
|
|
|
}
|
2022-09-07 23:56:38 +08:00
|
|
|
}
|