clean facet result types

This commit is contained in:
mpostma 2020-06-26 14:06:29 +02:00
parent afacdbc7a0
commit afab8a7846
2 changed files with 14 additions and 13 deletions

View File

@ -97,14 +97,14 @@ impl<'c, 'f, 'd, 'i> QueryBuilder<'c, 'f, 'd, 'i> {
.unwrap_or_default(); .unwrap_or_default();
ors.push(docids); ors.push(docids);
} }
let sets: Vec<_> = ors.iter().map(|i| &i.1).map(Cow::deref).collect(); let sets: Vec<_> = ors.iter().map(|(_, i)| i).map(Cow::deref).collect();
let or_result = sdset::multi::OpBuilder::from_vec(sets).union().into_set_buf(); let or_result = sdset::multi::OpBuilder::from_vec(sets).union().into_set_buf();
ands.push(Cow::Owned(or_result)); ands.push(Cow::Owned(or_result));
ors.clear(); ors.clear();
} }
Either::Right(key) => { Either::Right(key) => {
match self.index.facets.facet_document_ids(reader, &key)? { match self.index.facets.facet_document_ids(reader, &key)? {
Some(docids) => ands.push(docids.1), Some((_name, docids)) => ands.push(docids),
// no candidates for search, early return. // no candidates for search, early return.
None => return Ok(Some(SetBuf::default())), None => return Ok(Some(SetBuf::default())),
} }

View File

@ -2,12 +2,13 @@ use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::mem; use std::mem;
use heed::{RwTxn, RoTxn, Result as ZResult, RoRange, types::Str, BytesEncode, BytesDecode}; use heed::{RwTxn, RoTxn, RoRange, types::Str, BytesEncode, BytesDecode};
use sdset::{SetBuf, Set, SetOperation}; use sdset::{SetBuf, Set, SetOperation};
use meilisearch_types::DocumentId; use meilisearch_types::DocumentId;
use meilisearch_schema::FieldId; use meilisearch_schema::FieldId;
use crate::MResult;
use crate::database::MainT; use crate::database::MainT;
use crate::facets::FacetKey; use crate::facets::FacetKey;
use super::cow_set::CowSet; use super::cow_set::CowSet;
@ -56,21 +57,21 @@ impl<'a> BytesDecode<'a> for FacetData {
impl Facets { impl Facets {
// we use sdset::SetBuf to ensure the docids are sorted. // we use sdset::SetBuf to ensure the docids are sorted.
pub fn put_facet_document_ids(&self, writer: &mut RwTxn<MainT>, facet_key: FacetKey, doc_ids: &Set<DocumentId>, facet_value: &str) -> ZResult<()> { pub fn put_facet_document_ids(&self, writer: &mut RwTxn<MainT>, facet_key: FacetKey, doc_ids: &Set<DocumentId>, facet_value: &str) -> MResult<()> {
self.facets.put(writer, &facet_key, &(facet_value, doc_ids)) Ok(self.facets.put(writer, &facet_key, &(facet_value, doc_ids))?)
} }
pub fn field_document_ids<'txn>(&self, reader: &'txn RoTxn<MainT>, field_id: FieldId) -> ZResult<RoRange<'txn, FacetKey, FacetData>> { pub fn field_document_ids<'txn>(&self, reader: &'txn RoTxn<MainT>, field_id: FieldId) -> MResult<RoRange<'txn, FacetKey, FacetData>> {
self.facets.prefix_iter(reader, &FacetKey::new(field_id, String::new())) Ok(self.facets.prefix_iter(reader, &FacetKey::new(field_id, String::new()))?)
} }
pub fn facet_document_ids<'txn>(&self, reader: &'txn RoTxn<MainT>, facet_key: &FacetKey) -> ZResult<Option<(&'txn str,Cow<'txn, Set<DocumentId>>)>> { pub fn facet_document_ids<'txn>(&self, reader: &'txn RoTxn<MainT>, facet_key: &FacetKey) -> MResult<Option<(&'txn str,Cow<'txn, Set<DocumentId>>)>> {
self.facets.get(reader, &facet_key) Ok(self.facets.get(reader, &facet_key)?)
} }
/// updates the facets store, revmoving the documents from the facets provided in the /// updates the facets store, revmoving the documents from the facets provided in the
/// `facet_map` argument /// `facet_map` argument
pub fn remove(&self, writer: &mut RwTxn<MainT>, facet_map: HashMap<FacetKey, (String, Vec<DocumentId>)>) -> ZResult<()> { pub fn remove(&self, writer: &mut RwTxn<MainT>, facet_map: HashMap<FacetKey, (String, Vec<DocumentId>)>) -> MResult<()> {
for (key, (name, document_ids)) in facet_map { for (key, (name, document_ids)) in facet_map {
if let Some((_, old)) = self.facets.get(writer, &key)? { if let Some((_, old)) = self.facets.get(writer, &key)? {
let to_remove = SetBuf::from_dirty(document_ids); let to_remove = SetBuf::from_dirty(document_ids);
@ -81,7 +82,7 @@ impl Facets {
Ok(()) Ok(())
} }
pub fn add(&self, writer: &mut RwTxn<MainT>, facet_map: HashMap<FacetKey, (String, Vec<DocumentId>)>) -> ZResult<()> { pub fn add(&self, writer: &mut RwTxn<MainT>, facet_map: HashMap<FacetKey, (String, Vec<DocumentId>)>) -> MResult<()> {
for (key, (facet_name, document_ids)) in facet_map { for (key, (facet_name, document_ids)) in facet_map {
let set = SetBuf::from_dirty(document_ids); let set = SetBuf::from_dirty(document_ids);
self.put_facet_document_ids(writer, key, set.as_set(), &facet_name)?; self.put_facet_document_ids(writer, key, set.as_set(), &facet_name)?;
@ -89,7 +90,7 @@ impl Facets {
Ok(()) Ok(())
} }
pub fn clear(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<()> { pub fn clear(self, writer: &mut heed::RwTxn<MainT>) -> MResult<()> {
self.facets.clear(writer) Ok(self.facets.clear(writer)?)
} }
} }