@@ -70,9 +70,81 @@ pub use self::weight::Weight;
70
70
71
71
#[ cfg( test) ]
72
72
mod tests {
73
+ use crate :: collector:: TopDocs ;
74
+ use crate :: query:: phrase_query:: tests:: create_index;
73
75
use crate :: query:: QueryParser ;
74
76
use crate :: schema:: { Schema , TEXT } ;
75
- use crate :: { Index , Term } ;
77
+ use crate :: { DocAddress , Index , Term } ;
78
+
79
+ #[ test]
80
+ pub fn test_mixed_intersection_and_union ( ) -> crate :: Result < ( ) > {
81
+ let index = create_index ( & [ "a b" , "a c" , "a b c" , "b" ] ) ?;
82
+ let schema = index. schema ( ) ;
83
+ let text_field = schema. get_field ( "text" ) . unwrap ( ) ;
84
+ let searcher = index. reader ( ) ?. searcher ( ) ;
85
+
86
+ let do_search = |term : & str | {
87
+ let query = QueryParser :: for_index ( & index, vec ! [ text_field] )
88
+ . parse_query ( term)
89
+ . unwrap ( ) ;
90
+ let top_docs: Vec < ( f32 , DocAddress ) > =
91
+ searcher. search ( & query, & TopDocs :: with_limit ( 10 ) ) . unwrap ( ) ;
92
+
93
+ top_docs. iter ( ) . map ( |el| el. 1 . doc_id ) . collect :: < Vec < _ > > ( )
94
+ } ;
95
+
96
+ assert_eq ! ( do_search( "a AND b" ) , vec![ 0 , 2 ] ) ;
97
+ assert_eq ! ( do_search( "(a OR b) AND C" ) , vec![ 2 , 1 ] ) ;
98
+ // The intersection code has special code for more than 2 intersections
99
+ // left, right + others
100
+ // The will place the union in the "others" insersection to that seek_into_the_danger_zone
101
+ // is called
102
+ assert_eq ! (
103
+ do_search( "(a OR b) AND (c OR a) AND (b OR c)" ) ,
104
+ vec![ 2 , 1 , 0 ]
105
+ ) ;
106
+
107
+ Ok ( ( ) )
108
+ }
109
+
110
+ #[ test]
111
+ pub fn test_mixed_intersection_and_union_with_skip ( ) -> crate :: Result < ( ) > {
112
+ // Test 4096 skip in BufferedUnionScorer
113
+ let mut data: Vec < & str > = Vec :: new ( ) ;
114
+ data. push ( "a b" ) ;
115
+ let zz_data = vec ! [ "z z" ; 5000 ] ;
116
+ data. extend_from_slice ( & zz_data) ;
117
+ data. extend_from_slice ( & [ "a c" ] ) ;
118
+ data. extend_from_slice ( & zz_data) ;
119
+ data. extend_from_slice ( & [ "a b c" , "b" ] ) ;
120
+ let index = create_index ( & data) ?;
121
+ let schema = index. schema ( ) ;
122
+ let text_field = schema. get_field ( "text" ) . unwrap ( ) ;
123
+ let searcher = index. reader ( ) ?. searcher ( ) ;
124
+
125
+ let do_search = |term : & str | {
126
+ let query = QueryParser :: for_index ( & index, vec ! [ text_field] )
127
+ . parse_query ( term)
128
+ . unwrap ( ) ;
129
+ let top_docs: Vec < ( f32 , DocAddress ) > =
130
+ searcher. search ( & query, & TopDocs :: with_limit ( 10 ) ) . unwrap ( ) ;
131
+
132
+ top_docs. iter ( ) . map ( |el| el. 1 . doc_id ) . collect :: < Vec < _ > > ( )
133
+ } ;
134
+
135
+ assert_eq ! ( do_search( "a AND b" ) , vec![ 0 , 10002 ] ) ;
136
+ assert_eq ! ( do_search( "(a OR b) AND C" ) , vec![ 10002 , 5001 ] ) ;
137
+ // The intersection code has special code for more than 2 intersections
138
+ // left, right + others
139
+ // The will place the union in the "others" insersection to that seek_into_the_danger_zone
140
+ // is called
141
+ assert_eq ! (
142
+ do_search( "(a OR b) AND (c OR a) AND (b OR c)" ) ,
143
+ vec![ 10002 , 5001 , 0 ]
144
+ ) ;
145
+
146
+ Ok ( ( ) )
147
+ }
76
148
77
149
#[ test]
78
150
fn test_query_terms ( ) {
0 commit comments