@@ -34,8 +34,7 @@ impl QdrantIndex {
3434 vector_size : usize ,
3535 api_key : Option < String > ,
3636 ) -> Result < Self , Error > {
37- let runtime = create_async_tokio_runtime ( )
38- . map_err ( |e| Error :: Other ( format ! ( "Failed to create async runtime: {e}" ) . into ( ) ) ) ?;
37+ let runtime = create_async_tokio_runtime ( ) . map_err ( IndexingError :: from) ?;
3938
4039 let client = Qdrant :: from_url ( url)
4140 . api_key ( api_key)
@@ -84,83 +83,82 @@ impl QdrantIndex {
8483 )
8584 . await ?;
8685
87- let mut results = Vec :: with_capacity ( search_result. result . len ( ) ) ;
88- for point in search_result. result {
89- let Some ( point_id) = point. id else {
90- warn ! ( "Qdrant returned point without ID, ignoring" ) ;
91- continue ;
92- } ;
93-
94- let Some ( point_id_options) = point_id. point_id_options else {
95- warn ! ( "Qdrant returned point ID without options, ignoring" ) ;
96- continue ;
97- } ;
98-
99- let id = match point_id_options {
100- PointIdOptions :: Num ( num) => num,
101- PointIdOptions :: Uuid ( _) => {
102- warn ! ( "Qdrant returned UUID point ID, expected numeric ID" ) ;
103- continue ;
104- }
105- } ;
106-
107- let Some ( key) = self . key_to_id_mapper . get_key_for_id ( id) else {
108- warn ! ( "Qdrant index returned a nonexistent ID {id}, ignoring" ) ;
109- continue ;
110- } ;
111-
112- results. push ( KeyScoreMatch {
113- key,
114- score : f64:: from ( point. score ) ,
115- } ) ;
116- }
86+ let results = search_result
87+ . result
88+ . into_iter ( )
89+ . filter_map ( |point| {
90+ let Some ( point_id) = point. id else {
91+ warn ! ( "Qdrant returned point without ID, ignoring" ) ;
92+ return None ;
93+ } ;
94+
95+ let Some ( point_id_options) = point_id. point_id_options else {
96+ warn ! ( "Qdrant returned point ID without options, ignoring" ) ;
97+ return None ;
98+ } ;
99+
100+ let id = match point_id_options {
101+ PointIdOptions :: Num ( num) => num,
102+ PointIdOptions :: Uuid ( _) => {
103+ warn ! ( "Qdrant returned UUID point ID, expected numeric ID" ) ;
104+ return None ;
105+ }
106+ } ;
107+
108+ let Some ( key) = self . key_to_id_mapper . get_key_for_id ( id) else {
109+ warn ! ( "Qdrant index returned a nonexistent ID {id}, ignoring" ) ;
110+ return None ;
111+ } ;
112+
113+ Some ( KeyScoreMatch {
114+ key,
115+ score : f64:: from ( point. score ) ,
116+ } )
117+ } )
118+ . collect ( ) ;
117119
118120 Ok ( results)
119121 }
120122
121123 #[ allow( clippy:: cast_possible_truncation) ]
122124 fn add_batch ( & mut self , data : Vec < ( Key , Vec < f64 > ) > ) -> Result < ( ) , IndexingError > {
123- let mut points = Vec :: with_capacity ( data. len ( ) ) ;
124-
125- for ( key, vec_data) in data {
126- if vec_data. len ( ) != self . vector_size {
127- return Err ( IndexingError :: Io ( std:: io:: Error :: new (
128- std:: io:: ErrorKind :: InvalidData ,
129- format ! (
130- "Vector size mismatch: expected {}, got {}" ,
131- self . vector_size,
132- vec_data. len( )
133- ) ,
134- ) ) ) ;
135- }
125+ let points: Result < Vec < _ > , IndexingError > = data
126+ . into_iter ( )
127+ . map ( | ( key, vec_data) | {
128+ if vec_data. len ( ) != self . vector_size {
129+ return Err ( IndexingError :: Io ( std:: io:: Error :: new (
130+ std:: io:: ErrorKind :: InvalidData ,
131+ format ! (
132+ "Vector size mismatch: expected {}, got {}" ,
133+ self . vector_size,
134+ vec_data. len( )
135+ ) ,
136+ ) ) ) ;
137+ }
136138
137- let key_id = self . key_to_id_mapper . get_next_free_u64_id ( key) ;
138- let vec_f32: Vec < f32 > = vec_data. iter ( ) . map ( |v| * v as f32 ) . collect ( ) ;
139- points. push ( PointStruct :: new (
140- key_id,
141- vec_f32,
142- HashMap :: < String , Value > :: new ( ) ,
143- ) ) ;
144- }
139+ let key_id = self . key_to_id_mapper . get_next_free_u64_id ( key) ;
140+ let vec_f32: Vec < f32 > = vec_data. iter ( ) . map ( |v| * v as f32 ) . collect ( ) ;
141+ Ok ( PointStruct :: new (
142+ key_id,
143+ vec_f32,
144+ HashMap :: < String , Value > :: new ( ) ,
145+ ) )
146+ } )
147+ . collect ( ) ;
145148
146149 self . runtime . block_on (
147150 self . client
148- . upsert_points ( UpsertPointsBuilder :: new ( & self . collection_name , points) ) ,
151+ . upsert_points ( UpsertPointsBuilder :: new ( & self . collection_name , points? ) ) ,
149152 ) ?;
150153
151154 Ok ( ( ) )
152155 }
153156
154157 fn remove_batch ( & mut self , keys : Vec < Key > ) -> Result < Vec < u64 > , IndexingError > {
155- let mut key_ids = Vec :: with_capacity ( keys. len ( ) ) ;
156- let mut missing_keys = Vec :: new ( ) ;
157-
158- for key in keys {
159- match self . key_to_id_mapper . remove_key ( key) {
160- Ok ( key_id) => key_ids. push ( key_id) ,
161- Err ( _) => missing_keys. push ( key) ,
162- }
163- }
158+ let key_ids: Vec < u64 > = keys
159+ . into_iter ( )
160+ . filter_map ( |key| self . key_to_id_mapper . remove_key ( key) . ok ( ) )
161+ . collect ( ) ;
164162
165163 if !key_ids. is_empty ( ) {
166164 self . runtime . block_on ( self . client . delete_points (
@@ -221,11 +219,10 @@ impl NonFilteringExternalIndex<Vec<f64>, Vec<f64>> for QdrantIndex {
221219 let keys: Vec < Key > = queries. iter ( ) . map ( |( k, _, _) | * k) . collect ( ) ;
222220
223221 let results = self . runtime . block_on ( async {
224- let mut futures = Vec :: with_capacity ( queries. len ( ) ) ;
225-
226- for ( _, data, limit) in queries {
227- futures. push ( self . search_one_async ( data, * limit) ) ;
228- }
222+ let futures: Vec < _ > = queries
223+ . iter ( )
224+ . map ( |( _, data, limit) | self . search_one_async ( data, * limit) )
225+ . collect ( ) ;
229226
230227 futures:: future:: join_all ( futures) . await
231228 } ) ;
0 commit comments