@@ -970,7 +970,7 @@ impl<Score, D, const R: bool> From<TopNComputerDeser<Score, D, R>> for TopNCompu
970
970
}
971
971
}
972
972
973
- impl < Score , D , const R : bool > TopNComputer < Score , D , R >
973
+ impl < Score , D , const REVERSE_ORDER : bool > TopNComputer < Score , D , REVERSE_ORDER >
974
974
where
975
975
Score : PartialOrd + Clone ,
976
976
D : Ord ,
@@ -991,7 +991,10 @@ where
991
991
#[ inline]
992
992
pub fn push ( & mut self , feature : Score , doc : D ) {
993
993
if let Some ( last_median) = self . threshold . clone ( ) {
994
- if feature < last_median {
994
+ if !REVERSE_ORDER && feature > last_median {
995
+ return ;
996
+ }
997
+ if REVERSE_ORDER && feature < last_median {
995
998
return ;
996
999
}
997
1000
}
@@ -1026,7 +1029,7 @@ where
1026
1029
}
1027
1030
1028
1031
/// Returns the top n elements in sorted order.
1029
- pub fn into_sorted_vec ( mut self ) -> Vec < ComparableDoc < Score , D , R > > {
1032
+ pub fn into_sorted_vec ( mut self ) -> Vec < ComparableDoc < Score , D , REVERSE_ORDER > > {
1030
1033
if self . buffer . len ( ) > self . top_n {
1031
1034
self . truncate_top_n ( ) ;
1032
1035
}
@@ -1037,7 +1040,7 @@ where
1037
1040
/// Returns the top n elements in stored order.
1038
1041
/// Useful if you do not need the elements in sorted order,
1039
1042
/// for example when merging the results of multiple segments.
1040
- pub fn into_vec ( mut self ) -> Vec < ComparableDoc < Score , D , R > > {
1043
+ pub fn into_vec ( mut self ) -> Vec < ComparableDoc < Score , D , REVERSE_ORDER > > {
1041
1044
if self . buffer . len ( ) > self . top_n {
1042
1045
self . truncate_top_n ( ) ;
1043
1046
}
@@ -1047,9 +1050,11 @@ where
1047
1050
1048
1051
#[ cfg( test) ]
1049
1052
mod tests {
1053
+ use proptest:: prelude:: * ;
1054
+
1050
1055
use super :: { TopDocs , TopNComputer } ;
1051
1056
use crate :: collector:: top_collector:: ComparableDoc ;
1052
- use crate :: collector:: Collector ;
1057
+ use crate :: collector:: { Collector , DocSetCollector } ;
1053
1058
use crate :: query:: { AllQuery , Query , QueryParser } ;
1054
1059
use crate :: schema:: { Field , Schema , FAST , STORED , TEXT } ;
1055
1060
use crate :: time:: format_description:: well_known:: Rfc3339 ;
@@ -1144,6 +1149,44 @@ mod tests {
1144
1149
}
1145
1150
}
1146
1151
1152
+ proptest ! {
1153
+ #[ test]
1154
+ fn test_topn_computer_asc_prop(
1155
+ limit in 0 ..10_usize ,
1156
+ docs in proptest:: collection:: vec( ( 0 ..100_u64 , 0 ..100_u64 ) , 0 ..100_usize ) ,
1157
+ ) {
1158
+ let mut computer: TopNComputer <_, _, false > = TopNComputer :: new( limit) ;
1159
+ for ( feature, doc) in & docs {
1160
+ computer. push( * feature, * doc) ;
1161
+ }
1162
+ let mut comparable_docs = docs. into_iter( ) . map( |( feature, doc) | ComparableDoc { feature, doc } ) . collect:: <Vec <_>>( ) ;
1163
+ comparable_docs. sort( ) ;
1164
+ comparable_docs. truncate( limit) ;
1165
+ prop_assert_eq!(
1166
+ computer. into_sorted_vec( ) ,
1167
+ comparable_docs,
1168
+ ) ;
1169
+ }
1170
+
1171
+ #[ test]
1172
+ fn test_topn_computer_desc_prop(
1173
+ limit in 0 ..10_usize ,
1174
+ docs in proptest:: collection:: vec( ( 0 ..100_u64 , 0 ..100_u64 ) , 0 ..100_usize ) ,
1175
+ ) {
1176
+ let mut computer: TopNComputer <_, _, true > = TopNComputer :: new( limit) ;
1177
+ for ( feature, doc) in & docs {
1178
+ computer. push( * feature, * doc) ;
1179
+ }
1180
+ let mut comparable_docs = docs. into_iter( ) . map( |( feature, doc) | ComparableDoc { feature, doc } ) . collect:: <Vec <_>>( ) ;
1181
+ comparable_docs. sort( ) ;
1182
+ comparable_docs. truncate( limit) ;
1183
+ prop_assert_eq!(
1184
+ computer. into_sorted_vec( ) ,
1185
+ comparable_docs,
1186
+ ) ;
1187
+ }
1188
+ }
1189
+
1147
1190
#[ test]
1148
1191
fn test_top_collector_not_at_capacity_without_offset ( ) -> crate :: Result < ( ) > {
1149
1192
let index = make_index ( ) ?;
@@ -1645,4 +1688,29 @@ mod tests {
1645
1688
) ;
1646
1689
Ok ( ( ) )
1647
1690
}
1691
+
1692
+ #[ test]
1693
+ fn test_topn_computer_asc ( ) {
1694
+ let mut computer: TopNComputer < u32 , u32 , false > = TopNComputer :: new ( 2 ) ;
1695
+
1696
+ computer. push ( 1u32 , 1u32 ) ;
1697
+ computer. push ( 2u32 , 2u32 ) ;
1698
+ computer. push ( 3u32 , 3u32 ) ;
1699
+ computer. push ( 2u32 , 4u32 ) ;
1700
+ computer. push ( 4u32 , 5u32 ) ;
1701
+ computer. push ( 1u32 , 6u32 ) ;
1702
+ assert_eq ! (
1703
+ computer. into_sorted_vec( ) ,
1704
+ & [
1705
+ ComparableDoc {
1706
+ feature: 1u32 ,
1707
+ doc: 1u32 ,
1708
+ } ,
1709
+ ComparableDoc {
1710
+ feature: 1u32 ,
1711
+ doc: 6u32 ,
1712
+ }
1713
+ ]
1714
+ ) ;
1715
+ }
1648
1716
}
0 commit comments