1
+ use crate :: task_info:: TaskInfo ;
1
2
use async_trait:: async_trait;
2
3
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
3
4
@@ -301,11 +302,36 @@ impl<'a> DocumentsQuery<'a> {
301
302
}
302
303
}
303
304
305
+ #[ derive( Debug , Clone , Serialize ) ]
306
+ pub struct DocumentDeletionQuery < ' a > {
307
+ #[ serde( skip_serializing) ]
308
+ pub index : & ' a Index ,
309
+
310
+ /// Filters to apply.
311
+ ///
312
+ /// Read the [dedicated guide](https://docs.meilisearch.com/reference/features/filtering.html) to learn the syntax.
313
+ pub filter : & ' a str ,
314
+ }
315
+
316
+ impl < ' a > DocumentDeletionQuery < ' a > {
317
+ pub fn new ( index : & Index ) -> DocumentDeletionQuery {
318
+ DocumentDeletionQuery { index, filter : "" }
319
+ }
320
+
321
+ pub fn with_filter < ' b > ( & ' b mut self , filter : & ' a str ) -> & ' b mut DocumentDeletionQuery < ' a > {
322
+ self . filter = filter;
323
+ self
324
+ }
325
+
326
+ pub async fn execute < T : DeserializeOwned + ' static > ( & self ) -> Result < TaskInfo , Error > {
327
+ self . index . delete_documents_with ( self ) . await
328
+ }
329
+ }
330
+
304
331
#[ cfg( test) ]
305
332
mod tests {
306
333
use super :: * ;
307
- use crate :: { client:: * , indexes:: * } ;
308
- use :: meilisearch_sdk:: documents:: IndexConfig ;
334
+ use crate :: { client:: * , errors:: * , indexes:: * } ;
309
335
use meilisearch_test_macro:: meilisearch_test;
310
336
use serde:: { Deserialize , Serialize } ;
311
337
@@ -371,7 +397,6 @@ mod tests {
371
397
#[ meilisearch_test]
372
398
async fn test_get_documents_with_execute ( client : Client , index : Index ) -> Result < ( ) , Error > {
373
399
setup_test_index ( & client, & index) . await ?;
374
- // let documents = index.get_documents(None, None, None).await.unwrap();
375
400
let documents = DocumentsQuery :: new ( & index)
376
401
. with_limit ( 1 )
377
402
. with_offset ( 1 )
@@ -387,6 +412,41 @@ mod tests {
387
412
Ok ( ( ) )
388
413
}
389
414
415
+ #[ meilisearch_test]
416
+ async fn test_delete_documents_with ( client : Client , index : Index ) -> Result < ( ) , Error > {
417
+ setup_test_index ( & client, & index) . await ?;
418
+ index
419
+ . set_filterable_attributes ( [ "id" ] )
420
+ . await
421
+ . unwrap ( )
422
+ . wait_for_completion ( & client, None , None )
423
+ . await
424
+ . unwrap ( ) ;
425
+ let mut query = DocumentDeletionQuery :: new ( & index) ;
426
+ query. with_filter ( "id = 1" ) ;
427
+
428
+ index
429
+ . delete_documents_with ( & query)
430
+ . await
431
+ . unwrap ( )
432
+ . wait_for_completion ( & client, None , None )
433
+ . await
434
+ . unwrap ( ) ;
435
+ let document_result = index. get_document :: < MyObject > ( "1" ) . await ;
436
+
437
+ match document_result {
438
+ Ok ( _) => panic ! ( "The test was expecting no documents to be returned but got one." ) ,
439
+ Err ( e) => match e {
440
+ Error :: Meilisearch ( err) => {
441
+ assert_eq ! ( err. error_code, ErrorCode :: DocumentNotFound ) ;
442
+ }
443
+ _ => panic ! ( "The error was expected to be a Meilisearch error, but it was not." ) ,
444
+ } ,
445
+ }
446
+
447
+ Ok ( ( ) )
448
+ }
449
+
390
450
#[ meilisearch_test]
391
451
async fn test_get_documents_with_only_one_param (
392
452
client : Client ,
0 commit comments