fix(facet): string fields sorting

This commit is contained in:
Alexey Shekhirin 2021-09-02 19:41:19 +03:00
parent 5cbe879325
commit c2517e7d5f
No known key found for this signature in database
GPG Key ID: AF9A26AA133B5B98

View File

@ -243,6 +243,7 @@ impl<'t> Iterator for FacetStringGroupRevRange<'t> {
type Item = heed::Result<((NonZeroU8, u32, u32), (Option<(&'t str, &'t str)>, RoaringBitmap))>; type Item = heed::Result<((NonZeroU8, u32, u32), (Option<(&'t str, &'t str)>, RoaringBitmap))>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() { match self.iter.next() {
Some(Ok(((_fid, level, left, right), docids))) => { Some(Ok(((_fid, level, left, right), docids))) => {
let must_be_returned = match self.end { let must_be_returned = match self.end {
@ -252,15 +253,17 @@ impl<'t> Iterator for FacetStringGroupRevRange<'t> {
}; };
if must_be_returned { if must_be_returned {
match docids.decode() { match docids.decode() {
Ok((bounds, docids)) => Some(Ok(((level, left, right), (bounds, docids)))), Ok((bounds, docids)) => {
Err(e) => Some(Err(e)), return Some(Ok(((level, left, right), (bounds, docids))))
} }
} else { Err(e) => return Some(Err(e)),
None
} }
} }
Some(Err(e)) => Some(Err(e)), continue;
None => None, }
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
} }
} }
} }
@ -545,18 +548,18 @@ impl<'t> Iterator for FacetStringIter<'t> {
// the algorithm less complex to understand. // the algorithm less complex to understand.
let last = match last { let last = match last {
Left(ascending) => match ascending { Left(ascending) => match ascending {
Left(last) => Left(Left(last)), Left(group) => Left(Left(group)),
Right(last) => Right(Left(last)), Right(zero_level) => Right(Left(zero_level)),
}, },
Right(descending) => match descending { Right(descending) => match descending {
Left(last) => Left(Right(last)), Left(group) => Left(Right(group)),
Right(last) => Right(Right(last)), Right(zero_level) => Right(Right(zero_level)),
}, },
}; };
match last { match last {
Left(last) => { Left(group) => {
for result in last { for result in group {
match result { match result {
Ok(((level, left, right), (string_bounds, mut docids))) => { Ok(((level, left, right), (string_bounds, mut docids))) => {
docids &= &*documents_ids; docids &= &*documents_ids;
@ -566,6 +569,27 @@ impl<'t> Iterator for FacetStringIter<'t> {
} }
let result = if is_ascending { let result = if is_ascending {
match string_bounds {
Some((left, right)) => FacetStringLevelZeroRange::new(
self.rtxn,
self.db,
self.field_id,
Included(left),
Included(right),
)
.map(Right),
None => FacetStringGroupRange::new(
self.rtxn,
self.db,
self.field_id,
NonZeroU8::new(level.get() - 1).unwrap(),
Included(left),
Included(right),
)
.map(Left),
}
.map(Left)
} else {
match string_bounds { match string_bounds {
Some((left, right)) => { Some((left, right)) => {
FacetStringLevelZeroRevRange::new( FacetStringLevelZeroRevRange::new(
@ -588,27 +612,6 @@ impl<'t> Iterator for FacetStringIter<'t> {
.map(Left), .map(Left),
} }
.map(Right) .map(Right)
} else {
match string_bounds {
Some((left, right)) => FacetStringLevelZeroRange::new(
self.rtxn,
self.db,
self.field_id,
Included(left),
Included(right),
)
.map(Right),
None => FacetStringGroupRange::new(
self.rtxn,
self.db,
self.field_id,
NonZeroU8::new(level.get() - 1).unwrap(),
Included(left),
Included(right),
)
.map(Left),
}
.map(Left)
}; };
match result { match result {
@ -624,9 +627,9 @@ impl<'t> Iterator for FacetStringIter<'t> {
} }
} }
} }
Right(last) => { Right(zero_level) => {
// level zero only // level zero only
for result in last { for result in zero_level {
match result { match result {
Ok((normalized, original, mut docids)) => { Ok((normalized, original, mut docids)) => {
docids &= &*documents_ids; docids &= &*documents_ids;