1
+ use crate :: task_info:: TaskInfo ;
1
2
use async_trait:: async_trait;
2
3
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
3
4
@@ -314,6 +315,35 @@ impl<'a> DocumentsQuery<'a> {
314
315
}
315
316
}
316
317
318
+ #[ derive( Debug , Clone , Serialize ) ]
319
+ pub struct DocumentDeletionQuery < ' a > {
320
+ #[ serde( skip_serializing) ]
321
+ pub index : & ' a Index ,
322
+
323
+ /// Filters to apply.
324
+ ///
325
+ /// Read the [dedicated guide](https://docs.meilisearch.com/reference/features/filtering.html) to learn the syntax.
326
+ pub filter : Option < & ' a str > ,
327
+ }
328
+
329
+ impl < ' a > DocumentDeletionQuery < ' a > {
330
+ pub fn new ( index : & Index ) -> DocumentDeletionQuery {
331
+ DocumentDeletionQuery {
332
+ index,
333
+ filter : None ,
334
+ }
335
+ }
336
+
337
+ pub fn with_filter < ' b > ( & ' b mut self , filter : & ' a str ) -> & ' b mut DocumentDeletionQuery < ' a > {
338
+ self . filter = Some ( filter) ;
339
+ self
340
+ }
341
+
342
+ pub async fn execute < T : DeserializeOwned + ' static > ( & self ) -> Result < TaskInfo , Error > {
343
+ self . index . delete_documents_with ( self ) . await
344
+ }
345
+ }
346
+
317
347
#[ cfg( test) ]
318
348
mod tests {
319
349
use super :: * ;
@@ -383,7 +413,6 @@ mod tests {
383
413
#[ meilisearch_test]
384
414
async fn test_get_documents_with_execute ( client : Client , index : Index ) -> Result < ( ) , Error > {
385
415
setup_test_index ( & client, & index) . await ?;
386
- // let documents = index.get_documents(None, None, None).await.unwrap();
387
416
let documents = DocumentsQuery :: new ( & index)
388
417
. with_limit ( 1 )
389
418
. with_offset ( 1 )
@@ -399,6 +428,66 @@ mod tests {
399
428
Ok ( ( ) )
400
429
}
401
430
431
+ #[ meilisearch_test]
432
+ async fn test_delete_documents_with ( client : Client , index : Index ) -> Result < ( ) , Error > {
433
+ setup_test_index ( & client, & index) . await ?;
434
+ index
435
+ . set_filterable_attributes ( [ "id" ] )
436
+ . await ?
437
+ . wait_for_completion ( & client, None , None )
438
+ . await ?;
439
+
440
+ let mut query = DocumentDeletionQuery :: new ( & index) ;
441
+ query. with_filter ( "id = 1" ) ;
442
+ index
443
+ . delete_documents_with ( & query)
444
+ . await ?
445
+ . wait_for_completion ( & client, None , None )
446
+ . await ?;
447
+ let document_result = index. get_document :: < MyObject > ( "1" ) . await ;
448
+
449
+ match document_result {
450
+ Ok ( _) => panic ! ( "The test was expecting no documents to be returned but got one." ) ,
451
+ Err ( e) => match e {
452
+ Error :: Meilisearch ( err) => {
453
+ assert_eq ! ( err. error_code, ErrorCode :: DocumentNotFound ) ;
454
+ }
455
+ _ => panic ! ( "The error was expected to be a Meilisearch error, but it was not." ) ,
456
+ } ,
457
+ }
458
+
459
+ Ok ( ( ) )
460
+ }
461
+
462
+ #[ meilisearch_test]
463
+ async fn test_delete_documents_with_filter_not_filterable (
464
+ client : Client ,
465
+ index : Index ,
466
+ ) -> Result < ( ) , Error > {
467
+ setup_test_index ( & client, & index) . await ?;
468
+
469
+ let mut query = DocumentDeletionQuery :: new ( & index) ;
470
+ query. with_filter ( "id = 1" ) ;
471
+ let error = index
472
+ . delete_documents_with ( & query)
473
+ . await ?
474
+ . wait_for_completion ( & client, None , None )
475
+ . await ?;
476
+
477
+ let error = error. unwrap_failure ( ) ;
478
+
479
+ assert ! ( matches!(
480
+ error,
481
+ MeilisearchError {
482
+ error_code: ErrorCode :: InvalidDocumentFilter ,
483
+ error_type: ErrorType :: InvalidRequest ,
484
+ ..
485
+ }
486
+ ) ) ;
487
+
488
+ Ok ( ( ) )
489
+ }
490
+
402
491
#[ meilisearch_test]
403
492
async fn test_get_documents_with_only_one_param (
404
493
client : Client ,
0 commit comments