Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ search_1: |-
.await
.unwrap();
get_update_1: |-
let status: Status = progress.get_status().await.unwrap();
let status: Status = index.get_status(1).await.unwrap();
get_all_updates_1: |-
let status: Vec<ProgressStatus> = index.get_all_updates().await.unwrap();
get_keys_1: |-
Expand Down
106 changes: 105 additions & 1 deletion src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,66 @@ impl<'a> Index<'a> {
self.update(primary_key).await
}

/// Get the status of a given updates in a given index.
///
/// # 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() },
/// Document { id: 1, kind: "title".into(), value: "Harry Potter and the Sorcerer's Stone".to_string() },
/// ], None).await.unwrap();
///
/// let update_id = progress.get_update_id();
/// let status = movies.get_update(update_id).await.unwrap();
///
/// let content_update_id = match status {
/// UpdateStatus::Enqueued{content} => content.update_id,
/// UpdateStatus::Failed{content} => content.update_id,
/// UpdateStatus::Processed{content} => content.update_id,
/// };
///
/// assert_eq!(content_update_id, 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,9 +772,23 @@ pub struct IndexStats {

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

#[derive(Deserialize,Debug, Serialize)]
struct Movie {
name: String,
description: String,
}

impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType {
&self.name
}
}

#[async_test]
async fn test_get_all_updates_no_docs() {
let client = Client::new("http://localhost:7700", "masterKey");
Expand All @@ -726,4 +800,34 @@ 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
.add_or_replace(
&[Movie {
name: "test".to_string(),
description: "no desc".to_string(),
}],
Some("name"),
)
.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),
}
}
}
18 changes: 18 additions & 0 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ 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