@@ -619,6 +619,88 @@ impl<'a> Index<'a> {
619619 self . update ( primary_key) . await
620620 }
621621
622+ /// Get the status of an update on the index.
623+ ///
624+ /// After executing an update, a `Progress` struct is returned,
625+ /// you can use this struct to check on the status of the update.
626+ ///
627+ /// In some cases, you might not need the status of the update directly,
628+ /// or would rather not wait for it to resolve.
629+ ///
630+ /// For these cases, you can get the `update_id` from the `Progress`
631+ /// struct and use it to query the index later on.
632+ ///
633+ /// For example, if a clients updates an entry over an HTTP request,
634+ /// you can respond with the `update_id` and have the client check
635+ /// on the update status later on.
636+ ///
637+ /// # Example
638+ ///
639+ /// ```
640+ /// # use serde::{Serialize, Deserialize};
641+ /// # use std::thread::sleep;
642+ /// # use std::time::Duration;
643+ /// # use meilisearch_sdk::{client::*, document, indexes::*, progress::UpdateStatus};
644+ /// #
645+ /// # #[derive(Debug, Serialize, Deserialize, PartialEq)]
646+ /// # struct Document {
647+ /// # id: usize,
648+ /// # value: String,
649+ /// # kind: String,
650+ /// # }
651+ /// #
652+ /// # impl document::Document for Document {
653+ /// # type UIDType = usize;
654+ /// #
655+ /// # fn get_uid(&self) -> &Self::UIDType {
656+ /// # &self.id
657+ /// # }
658+ /// # }
659+ /// #
660+ /// # futures::executor::block_on(async move {
661+ /// let client = Client::new("http://localhost:7700", "masterKey");
662+ /// let movies = client.get_or_create("movies_get_one_update").await.unwrap();
663+ ///
664+ /// let progress = movies.add_documents(&[
665+ /// Document { id: 0, kind: "title".into(), value: "The Social Network".to_string() }
666+ /// ], None).await.unwrap();
667+ ///
668+ /// // Get update status directly on the progress object
669+ /// let status = progress.get_status().await.unwrap();
670+ /// let from_progress = match status {
671+ /// UpdateStatus::Enqueued{content} => content.update_id,
672+ /// UpdateStatus::Failed{content} => content.update_id,
673+ /// UpdateStatus::Processed{content} => content.update_id,
674+ /// };
675+ ///
676+ /// let update_id = progress.get_update_id();
677+ /// // Get update status from the index, using `update_id`
678+ /// let status = movies.get_update(update_id).await.unwrap();
679+ ///
680+ /// let from_index = match status {
681+ /// UpdateStatus::Enqueued{content} => content.update_id,
682+ /// UpdateStatus::Failed{content} => content.update_id,
683+ /// UpdateStatus::Processed{content} => content.update_id,
684+ /// };
685+ ///
686+ /// assert_eq!(from_progress, from_index);
687+ /// assert_eq!(from_progress, update_id);
688+ /// # client.delete_index("movies_get_one_update").await.unwrap();
689+ /// # });
690+ /// ```
691+ pub async fn get_update ( & self , update_id : u64 ) -> Result < UpdateStatus , Error > {
692+ request :: < ( ) , UpdateStatus > (
693+ & format ! (
694+ "{}/indexes/{}/updates/{}" ,
695+ self . client. host, self . uid, update_id
696+ ) ,
697+ self . client . apikey ,
698+ Method :: Get ,
699+ 200 ,
700+ )
701+ . await
702+ }
703+
622704 /// Get the status of all updates in a given index.
623705 ///
624706 /// # Example
@@ -712,7 +794,7 @@ pub struct IndexStats {
712794
713795#[ cfg( test) ]
714796mod tests {
715- use crate :: { client:: * } ;
797+ use crate :: { client:: * , progress :: UpdateStatus } ;
716798 use futures_await_test:: async_test;
717799
718800 #[ async_test]
@@ -726,4 +808,24 @@ mod tests {
726808
727809 assert_eq ! ( status. len( ) , 0 ) ;
728810 }
811+
812+ #[ async_test]
813+ async fn test_get_one_update ( ) {
814+ let client = Client :: new ( "http://localhost:7700" , "masterKey" ) ;
815+ let uid = "test_get_one_update" ;
816+
817+ let index = client. get_or_create ( uid) . await . unwrap ( ) ;
818+ let progress = index. delete_all_documents ( ) . await . unwrap ( ) ;
819+
820+ let update_id = progress. get_update_id ( ) ;
821+ let status = index. get_update ( update_id) . await . unwrap ( ) ;
822+
823+ client. delete_index ( uid) . await . unwrap ( ) ;
824+
825+ match status {
826+ UpdateStatus :: Enqueued { content} => assert_eq ! ( content. update_id, update_id) ,
827+ UpdateStatus :: Failed { content} => assert_eq ! ( content. update_id, update_id) ,
828+ UpdateStatus :: Processed { content} => assert_eq ! ( content. update_id, update_id) ,
829+ }
830+ }
729831}
0 commit comments