Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ search_1: |-
.await
.unwrap();
get_update_1: |-
// You can get the status of a `Progress` object:
let status: Status = progress.get_status().await.unwrap();

// Or you can use index to get an update status using its `update_id`:
let status: Status = index.get_update(1).await.unwrap();
get_all_updates_1: |-
let status: Vec<ProgressStatus> = index.get_all_updates().await.unwrap();
get_keys_1: |-
Expand Down
104 changes: 103 additions & 1 deletion src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,88 @@ impl<'a> Index<'a> {
self.update(primary_key).await
}

/// Get the status of an update on the index.
///
/// After executing an update, a `Progress` struct is returned,
/// you can use this struct to check on the status of the update.
///
/// In some cases, you might not need the status of the update directly,
/// or would rather not wait for it to resolve.
///
/// For these cases, you can get the `update_id` from the `Progress`
/// struct and use it to query the index later on.
///
/// For example, if a clients updates an entry over an HTTP request,
/// you can respond with the `update_id` and have the client check
/// on the update status later on.
///
/// # Example
///
/// ```
/// # use serde::{Serialize, Deserialize};
/// # use std::thread::sleep;
/// # use std::time::Duration;
/// # use meilisearch_sdk::{client::*, document, indexes::*, progress::UpdateStatus};
/// #
/// # #[derive(Debug, Serialize, Deserialize, PartialEq)]
/// # struct Document {
/// # id: usize,
/// # value: String,
/// # kind: String,
/// # }
/// #
/// # impl document::Document for Document {
/// # type UIDType = usize;
/// #
/// # fn get_uid(&self) -> &Self::UIDType {
/// # &self.id
/// # }
/// # }
/// #
/// # futures::executor::block_on(async move {
/// let client = Client::new("http://localhost:7700", "masterKey");
/// let movies = client.get_or_create("movies_get_one_update").await.unwrap();
///
/// let progress = movies.add_documents(&[
/// Document { id: 0, kind: "title".into(), value: "The Social Network".to_string() }
/// ], None).await.unwrap();
///
/// // Get update status directly on the progress object
/// let status = progress.get_status().await.unwrap();
/// let from_progress = match status {
/// UpdateStatus::Enqueued{content} => content.update_id,
/// UpdateStatus::Failed{content} => content.update_id,
/// UpdateStatus::Processed{content} => content.update_id,
/// };
///
/// let update_id = progress.get_update_id();
/// // Get update status from the index, using `update_id`
/// let status = movies.get_update(update_id).await.unwrap();
///
/// let from_index = match status {
/// UpdateStatus::Enqueued{content} => content.update_id,
/// UpdateStatus::Failed{content} => content.update_id,
/// UpdateStatus::Processed{content} => content.update_id,
/// };
///
/// assert_eq!(from_progress, from_index);
/// assert_eq!(from_progress, update_id);
/// # client.delete_index("movies_get_one_update").await.unwrap();
/// # });
/// ```
pub async fn get_update(&self, update_id: u64) -> Result<UpdateStatus, Error> {
request::<(), UpdateStatus>(
&format!(
"{}/indexes/{}/updates/{}",
self.client.host, self.uid, update_id
),
self.client.apikey,
Method::Get,
200,
)
.await
}

/// Get the status of all updates in a given index.
///
/// # Example
Expand Down Expand Up @@ -712,7 +794,7 @@ pub struct IndexStats {

#[cfg(test)]
mod tests {
use crate::{client::*};
use crate::{client::*, progress::UpdateStatus};
use futures_await_test::async_test;

#[async_test]
Expand All @@ -726,4 +808,24 @@ mod tests {

assert_eq!(status.len(), 0);
}

#[async_test]
async fn test_get_one_update() {
let client = Client::new("http://localhost:7700", "masterKey");
let uid = "test_get_one_update";

let index = client.get_or_create(uid).await.unwrap();
let progress = index.delete_all_documents().await.unwrap();

let update_id = progress.get_update_id();
let status = index.get_update(update_id).await.unwrap();

client.delete_index(uid).await.unwrap();

match status {
UpdateStatus::Enqueued{content} => assert_eq!(content.update_id, update_id),
UpdateStatus::Failed{content} => assert_eq!(content.update_id, update_id),
UpdateStatus::Processed{content} => assert_eq!(content.update_id, update_id),
}
}
}
17 changes: 17 additions & 0 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ pub struct Progress<'a> {
}

impl<'a> Progress<'a> {

/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, document::*};
/// # futures::executor::block_on(async move {
/// let client = Client::new("http://localhost:7700", "masterKey");
/// let mut movies_index = client.get_or_create("movies").await.unwrap();
/// let progress = movies_index.delete_all_documents().await.unwrap();
/// let update_id = progress.get_update_id();
/// # client.delete_index("movies").await.unwrap();
/// # });
/// ```
pub fn get_update_id(&self) -> u64 {
self.id as u64
}

/// # Example
///
/// ```
Expand Down