2023-02-21 16:49:43 +08:00
|
|
|
use heed::RoTxn;
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2023-02-21 20:57:34 +08:00
|
|
|
use super::db_cache::DatabaseCache;
|
2023-02-22 22:34:37 +08:00
|
|
|
use super::logger::SearchLogger;
|
2023-02-21 16:49:43 +08:00
|
|
|
use super::{
|
2023-02-21 20:57:34 +08:00
|
|
|
RankingRule, RankingRuleOutput, RankingRuleOutputIter, RankingRuleOutputIterWrapper,
|
|
|
|
RankingRuleQueryTrait,
|
2023-02-21 16:49:43 +08:00
|
|
|
};
|
|
|
|
use crate::{
|
|
|
|
// facet::FacetType,
|
|
|
|
heed_codec::{facet::FacetGroupKeyCodec, ByteSliceRefCodec},
|
|
|
|
search::facet::{ascending_facet_sort, descending_facet_sort},
|
|
|
|
FieldId,
|
|
|
|
Index,
|
|
|
|
Result,
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: The implementation of Sort is not correct:
|
|
|
|
// (1) it should not return documents it has already returned (does the current implementation have the same bug?)
|
|
|
|
// (2) at the end, it should return all the remaining documents (this could be ensured at the trait level?)
|
|
|
|
|
|
|
|
pub struct Sort<'transaction, Query> {
|
2023-02-22 22:34:37 +08:00
|
|
|
field_name: String,
|
2023-02-21 16:49:43 +08:00
|
|
|
field_id: Option<FieldId>,
|
|
|
|
is_ascending: bool,
|
|
|
|
iter: Option<RankingRuleOutputIterWrapper<'transaction, Query>>,
|
|
|
|
}
|
|
|
|
impl<'transaction, Query> Sort<'transaction, Query> {
|
|
|
|
pub fn new(
|
|
|
|
index: &'transaction Index,
|
|
|
|
rtxn: &'transaction heed::RoTxn,
|
|
|
|
field_name: String,
|
|
|
|
is_ascending: bool,
|
|
|
|
) -> Result<Self> {
|
|
|
|
let fields_ids_map = index.fields_ids_map(rtxn)?;
|
|
|
|
let field_id = fields_ids_map.id(&field_name);
|
|
|
|
|
2023-02-22 22:34:37 +08:00
|
|
|
Ok(Self { field_name, field_id, is_ascending, iter: None })
|
2023-02-21 16:49:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'transaction, Query: RankingRuleQueryTrait> RankingRule<'transaction, Query>
|
|
|
|
for Sort<'transaction, Query>
|
|
|
|
{
|
2023-02-22 22:34:37 +08:00
|
|
|
fn id(&self) -> String {
|
|
|
|
let Self { field_name, is_ascending, .. } = self;
|
|
|
|
format!("{field_name}:{}", if *is_ascending { "asc" } else { "desc " })
|
|
|
|
}
|
2023-02-21 16:49:43 +08:00
|
|
|
fn start_iteration(
|
|
|
|
&mut self,
|
|
|
|
index: &Index,
|
|
|
|
txn: &'transaction RoTxn,
|
|
|
|
_db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-22 22:34:37 +08:00
|
|
|
_logger: &mut dyn SearchLogger<Query>,
|
2023-02-21 16:49:43 +08:00
|
|
|
parent_candidates: &RoaringBitmap,
|
|
|
|
parent_query_graph: &Query,
|
|
|
|
) -> Result<()> {
|
|
|
|
let iter: RankingRuleOutputIterWrapper<Query> = match self.field_id {
|
|
|
|
Some(field_id) => {
|
|
|
|
let make_iter =
|
|
|
|
if self.is_ascending { ascending_facet_sort } else { descending_facet_sort };
|
|
|
|
|
|
|
|
let number_iter = make_iter(
|
|
|
|
txn,
|
|
|
|
index
|
|
|
|
.facet_id_f64_docids
|
|
|
|
.remap_key_type::<FacetGroupKeyCodec<ByteSliceRefCodec>>(),
|
|
|
|
field_id,
|
|
|
|
parent_candidates.clone(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let string_iter = make_iter(
|
|
|
|
txn,
|
|
|
|
index
|
|
|
|
.facet_id_string_docids
|
|
|
|
.remap_key_type::<FacetGroupKeyCodec<ByteSliceRefCodec>>(),
|
|
|
|
field_id,
|
|
|
|
parent_candidates.clone(),
|
|
|
|
)?;
|
|
|
|
let query_graph = parent_query_graph.clone();
|
|
|
|
RankingRuleOutputIterWrapper::new(Box::new(number_iter.chain(string_iter).map(
|
|
|
|
move |docids| {
|
|
|
|
Ok(RankingRuleOutput { query: query_graph.clone(), candidates: docids? })
|
|
|
|
},
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
None => RankingRuleOutputIterWrapper::new(Box::new(std::iter::empty())),
|
|
|
|
};
|
|
|
|
self.iter = Some(iter);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_bucket(
|
|
|
|
&mut self,
|
|
|
|
_index: &Index,
|
|
|
|
_txn: &'transaction RoTxn,
|
|
|
|
_db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-22 22:34:37 +08:00
|
|
|
_logger: &mut dyn SearchLogger<Query>,
|
2023-02-21 16:49:43 +08:00
|
|
|
_universe: &RoaringBitmap,
|
|
|
|
) -> Result<Option<RankingRuleOutput<Query>>> {
|
|
|
|
let iter = self.iter.as_mut().unwrap();
|
|
|
|
// TODO: we should make use of the universe in the function below
|
|
|
|
iter.next_bucket()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_iteration(
|
|
|
|
&mut self,
|
|
|
|
_index: &Index,
|
|
|
|
_txn: &'transaction RoTxn,
|
|
|
|
_db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-22 22:34:37 +08:00
|
|
|
_logger: &mut dyn SearchLogger<Query>,
|
2023-02-21 16:49:43 +08:00
|
|
|
) {
|
|
|
|
self.iter = None;
|
|
|
|
}
|
|
|
|
}
|