2022-04-26 18:12:58 +02:00
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use big_s::S;
|
|
|
|
use heed::EnvOpenOptions;
|
|
|
|
use maplit::hashset;
|
2022-06-14 16:04:27 +02:00
|
|
|
use milli::documents::{DocumentsBatchBuilder, DocumentsBatchReader};
|
2022-04-26 18:12:58 +02:00
|
|
|
use milli::update::{IndexDocuments, IndexDocumentsConfig, IndexerConfig, Settings};
|
2022-06-15 15:36:27 +02:00
|
|
|
use milli::{FacetDistribution, Index, Object};
|
|
|
|
use serde_json::Deserializer;
|
2022-04-26 18:12:58 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_facet_distribution_with_no_facet_values() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, &config);
|
|
|
|
|
|
|
|
builder.set_filterable_fields(hashset! {
|
|
|
|
S("genres"),
|
|
|
|
S("tags"),
|
|
|
|
});
|
2022-10-05 17:41:07 +02:00
|
|
|
builder.execute(|_| (), || false).unwrap();
|
2022-04-26 18:12:58 +02:00
|
|
|
|
|
|
|
// index documents
|
|
|
|
let config = IndexerConfig { max_memory: Some(10 * 1024 * 1024), ..Default::default() };
|
|
|
|
let indexing_config = IndexDocumentsConfig { autogenerate_docids: true, ..Default::default() };
|
|
|
|
|
2022-10-05 17:41:07 +02:00
|
|
|
let builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| (), || false).unwrap();
|
2022-06-14 16:04:27 +02:00
|
|
|
let mut documents_builder = DocumentsBatchBuilder::new(Vec::new());
|
2022-04-26 18:12:58 +02:00
|
|
|
let reader = Cursor::new(
|
2022-06-14 16:04:27 +02:00
|
|
|
r#"{
|
2022-04-26 18:12:58 +02:00
|
|
|
"id": 123,
|
|
|
|
"title": "What a week, hu...",
|
|
|
|
"genres": [],
|
|
|
|
"tags": ["blue"]
|
2022-06-14 16:04:27 +02:00
|
|
|
}
|
2022-04-26 18:12:58 +02:00
|
|
|
{
|
|
|
|
"id": 345,
|
|
|
|
"title": "I am the pig!",
|
|
|
|
"tags": ["red"]
|
2022-06-14 16:04:27 +02:00
|
|
|
}"#,
|
2022-04-26 18:12:58 +02:00
|
|
|
);
|
|
|
|
|
2022-06-15 15:36:27 +02:00
|
|
|
for result in Deserializer::from_reader(reader).into_iter::<Object>() {
|
2022-06-14 16:04:27 +02:00
|
|
|
let object = result.unwrap();
|
|
|
|
documents_builder.append_json_object(&object).unwrap();
|
2022-04-26 18:12:58 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 16:04:27 +02:00
|
|
|
let vector = documents_builder.into_inner().unwrap();
|
2022-04-26 18:12:58 +02:00
|
|
|
|
|
|
|
// index documents
|
2022-06-14 16:04:27 +02:00
|
|
|
let content = DocumentsBatchReader::from_reader(Cursor::new(vector)).unwrap();
|
2022-06-15 14:35:19 +02:00
|
|
|
let (builder, user_error) = builder.add_documents(content).unwrap();
|
|
|
|
user_error.unwrap();
|
2022-04-26 18:12:58 +02:00
|
|
|
builder.execute().unwrap();
|
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
let mut distrib = FacetDistribution::new(&txn, &index);
|
|
|
|
distrib.facets(vec!["genres"]);
|
|
|
|
let result = distrib.execute().unwrap();
|
|
|
|
assert_eq!(result["genres"].len(), 0);
|
|
|
|
|
|
|
|
let mut distrib = FacetDistribution::new(&txn, &index);
|
|
|
|
distrib.facets(vec!["tags"]);
|
|
|
|
let result = distrib.execute().unwrap();
|
|
|
|
assert_eq!(result["tags"].len(), 2);
|
|
|
|
}
|