1
+ use std:: path:: PathBuf ;
1
2
use std:: sync:: Arc ;
2
3
4
+ use anyhow:: Result ;
3
5
use futures_util:: future:: join_all;
4
6
use geo:: Rect ;
5
7
use itertools:: Itertools ;
@@ -24,6 +26,7 @@ use tantivy_uffd::RemoteDirectory;
24
26
use tokio:: task:: spawn_blocking;
25
27
use unicode_segmentation:: UnicodeSegmentation ;
26
28
29
+ use crate :: error:: AirmailError ;
27
30
use crate :: {
28
31
poi:: { AirmailPoi , SchemafiedPoi } ,
29
32
query:: all_subsequences,
@@ -63,8 +66,8 @@ impl AirmailIndex {
63
66
. set_indexed ( )
64
67
. set_stored ( )
65
68
. set_fast ( ) ;
66
- assert_eq ! ( s2cell_parent_index_options. fieldnorms( ) , false ) ;
67
- assert_eq ! ( s2cell_index_options. fieldnorms( ) , false ) ;
69
+ assert ! ( ! s2cell_parent_index_options. fieldnorms( ) ) ;
70
+ assert ! ( ! s2cell_index_options. fieldnorms( ) ) ;
68
71
69
72
let _ = schema_builder. add_text_field ( FIELD_CONTENT , text_options. clone ( ) ) ;
70
73
let _ = schema_builder. add_text_field ( FIELD_INDEXED_TAG , tag_options) ;
@@ -109,7 +112,7 @@ impl AirmailIndex {
109
112
self . tantivy_index . schema ( ) . get_field ( FIELD_TAGS ) . unwrap ( )
110
113
}
111
114
112
- pub fn create ( index_dir : & str ) -> Result < Self , Box < dyn std :: error :: Error > > {
115
+ pub fn create ( index_dir : & PathBuf ) -> Result < Self > {
113
116
let schema = Self :: schema ( ) ;
114
117
let tantivy_index =
115
118
tantivy:: Index :: open_or_create ( MmapDirectory :: open ( index_dir) ?, schema) ?;
@@ -119,15 +122,15 @@ impl AirmailIndex {
119
122
} )
120
123
}
121
124
122
- pub fn new ( index_dir : & str ) -> Result < Self , Box < dyn std :: error :: Error > > {
125
+ pub fn new ( index_dir : & str ) -> Result < Self > {
123
126
let tantivy_index = tantivy:: Index :: open_in_dir ( index_dir) ?;
124
127
Ok ( Self {
125
128
tantivy_index : Arc :: new ( tantivy_index) ,
126
129
is_remote : false ,
127
130
} )
128
131
}
129
132
130
- pub fn new_remote ( base_url : & str ) -> Result < Self , Box < dyn std :: error :: Error > > {
133
+ pub fn new_remote ( base_url : & str ) -> Result < Self > {
131
134
let tantivy_index =
132
135
tantivy:: Index :: open ( RemoteDirectory :: < { 2 * 1024 * 1024 } > :: new ( base_url) ) ?;
133
136
Ok ( Self {
@@ -136,7 +139,7 @@ impl AirmailIndex {
136
139
} )
137
140
}
138
141
139
- pub fn writer ( & mut self ) -> Result < AirmailIndexWriter , Box < dyn std :: error :: Error > > {
142
+ pub fn writer ( & mut self ) -> Result < AirmailIndexWriter > {
140
143
let tantivy_writer = self
141
144
. tantivy_index
142
145
. writer :: < TantivyDocument > ( 2_000_000_000 ) ?;
@@ -147,7 +150,7 @@ impl AirmailIndex {
147
150
Ok ( writer)
148
151
}
149
152
150
- pub async fn merge ( & mut self ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
153
+ pub async fn merge ( & mut self ) -> Result < ( ) > {
151
154
let ids = self . tantivy_index . searchable_segment_ids ( ) ?;
152
155
self . tantivy_index
153
156
. writer :: < TantivyDocument > ( 2_000_000_000 ) ?
@@ -156,7 +159,7 @@ impl AirmailIndex {
156
159
Ok ( ( ) )
157
160
}
158
161
159
- pub async fn num_docs ( & self ) -> Result < u64 , Box < dyn std :: error :: Error > > {
162
+ pub async fn num_docs ( & self ) -> Result < u64 > {
160
163
let index = self . tantivy_index . clone ( ) ;
161
164
let count = spawn_blocking ( move || {
162
165
if let Ok ( tantivy_reader) = index. reader ( ) {
@@ -165,7 +168,7 @@ impl AirmailIndex {
165
168
None
166
169
}
167
170
} ) ;
168
- Ok ( count. await ?. ok_or ( "Error getting count" ) ?)
171
+ Ok ( count. await ?. ok_or ( AirmailError :: UnableToCount ) ?)
169
172
}
170
173
171
174
async fn construct_query (
@@ -244,37 +247,35 @@ impl AirmailIndex {
244
247
boost,
245
248
) ) ) ;
246
249
}
250
+ } else if possible_query. len ( ) >= 8 && lenient {
251
+ let query = if tokens. ends_with ( & [ possible_query] ) {
252
+ FuzzyTermQuery :: new_prefix ( term, 1 , true )
253
+ } else {
254
+ FuzzyTermQuery :: new ( term, 1 , true )
255
+ } ;
256
+ if self . is_remote {
257
+ let searcher = searcher. clone ( ) ;
258
+ let query = query. clone ( ) ;
259
+ spawn_blocking ( move || {
260
+ let _ = searcher. search ( & query, & Count ) ;
261
+ } ) ;
262
+ }
263
+ mandatory_queries. push ( Box :: new ( BoostQuery :: new ( Box :: new ( query) , boost) ) ) ;
247
264
} else {
248
- if possible_query . len ( ) >= 8 && lenient {
249
- let query = if tokens. ends_with ( & [ possible_query] ) {
250
- FuzzyTermQuery :: new_prefix ( term, 1 , true )
265
+ let query : Box < dyn Query > =
266
+ if self . is_remote || !lenient || ! tokens. ends_with ( & [ possible_query] ) {
267
+ Box :: new ( TermQuery :: new ( term, IndexRecordOption :: Basic ) )
251
268
} else {
252
- FuzzyTermQuery :: new ( term, 1 , true )
269
+ Box :: new ( FuzzyTermQuery :: new_prefix ( term, 0 , false ) )
253
270
} ;
254
- if self . is_remote {
255
- let searcher = searcher. clone ( ) ;
256
- let query = query. clone ( ) ;
257
- spawn_blocking ( move || {
258
- let _ = searcher. search ( & query, & Count ) ;
259
- } ) ;
260
- }
261
- mandatory_queries. push ( Box :: new ( BoostQuery :: new ( Box :: new ( query) , boost) ) ) ;
262
- } else {
263
- let query: Box < dyn Query > =
264
- if self . is_remote || !lenient || !tokens. ends_with ( & [ possible_query] ) {
265
- Box :: new ( TermQuery :: new ( term, IndexRecordOption :: Basic ) )
266
- } else {
267
- Box :: new ( FuzzyTermQuery :: new_prefix ( term, 0 , false ) )
268
- } ;
269
- if self . is_remote {
270
- let searcher = searcher. clone ( ) ;
271
- let query = query. box_clone ( ) ;
272
- spawn_blocking ( move || {
273
- let _ = searcher. search ( & query, & Count ) ;
274
- } ) ;
275
- }
276
- mandatory_queries. push ( Box :: new ( BoostQuery :: new ( query, boost) ) ) ;
277
- } ;
271
+ if self . is_remote {
272
+ let searcher = searcher. clone ( ) ;
273
+ let query = query. box_clone ( ) ;
274
+ spawn_blocking ( move || {
275
+ let _ = searcher. search ( & query, & Count ) ;
276
+ } ) ;
277
+ }
278
+ mandatory_queries. push ( Box :: new ( BoostQuery :: new ( query, boost) ) ) ;
278
279
}
279
280
}
280
281
@@ -328,7 +329,7 @@ impl AirmailIndex {
328
329
] ) ) ;
329
330
}
330
331
331
- return Box :: new ( final_query) ;
332
+ Box :: new ( final_query)
332
333
}
333
334
334
335
/// This is public because I don't want one big mega-crate but its API should not be considered even remotely stable.
@@ -339,7 +340,7 @@ impl AirmailIndex {
339
340
tags : Option < Vec < String > > ,
340
341
bbox : Option < Rect < f64 > > ,
341
342
boost_regions : & [ ( f32 , Rect < f64 > ) ] ,
342
- ) -> Result < Vec < ( AirmailPoi , f32 ) > , Box < dyn std :: error :: Error > > {
343
+ ) -> Result < Vec < ( AirmailPoi , f32 ) > > {
343
344
let tantivy_reader = self . tantivy_index . reader ( ) ?;
344
345
let searcher = tantivy_reader. searcher ( ) ;
345
346
let query_string = query. trim ( ) . replace ( "'s" , "s" ) ;
@@ -352,7 +353,7 @@ impl AirmailIndex {
352
353
& query_string,
353
354
tags,
354
355
bbox,
355
- & boost_regions,
356
+ boost_regions,
356
357
request_leniency,
357
358
)
358
359
. await ;
@@ -424,11 +425,7 @@ impl AirmailIndexWriter {
424
425
doc. add_text ( self . schema . get_field ( FIELD_CONTENT ) . unwrap ( ) , value) ;
425
426
}
426
427
427
- pub async fn add_poi (
428
- & mut self ,
429
- poi : SchemafiedPoi ,
430
- source : & str ,
431
- ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
428
+ pub fn add_poi ( & mut self , poi : SchemafiedPoi , source : & str ) -> Result < ( ) > {
432
429
let mut doc = TantivyDocument :: default ( ) ;
433
430
for content in poi. content {
434
431
self . process_field ( & mut doc, & content) ;
@@ -468,7 +465,7 @@ impl AirmailIndexWriter {
468
465
Ok ( ( ) )
469
466
}
470
467
471
- pub fn commit ( mut self ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
468
+ pub fn commit ( mut self ) -> Result < ( ) > {
472
469
self . tantivy_writer . commit ( ) ?;
473
470
Ok ( ( ) )
474
471
}
0 commit comments