Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documents by function #613

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
64 changes: 64 additions & 0 deletions src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
DefaultHttpClient,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use std::{collections::HashMap, fmt::Display, time::Duration};
use time::OffsetDateTime;

Expand Down Expand Up @@ -76,6 +77,13 @@ pub struct Index<Http: HttpClient = DefaultHttpClient> {
pub primary_key: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DocumentEditionByFunction {
pub filter: Option<Value>,
pub context: Option<Value>,
pub function: String,
}

impl<Http: HttpClient> Index<Http> {
pub fn new(uid: impl Into<String>, client: Client<Http>) -> Index<Http> {
Index {
Expand Down Expand Up @@ -327,6 +335,62 @@ impl<Http: HttpClient> Index<Http> {
.await
}

/// Add a new capability to update documents based on a function.
///
///
/// # Example
///
/// ```
/// # use serde::{Serialize, Deserialize};
/// # use meilisearch_sdk::{client::*, indexes::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// struct Movie {
/// name: String,
/// description: String
/// }
/// let function = DocumentEditionByFunction {
/// filter: None,
/// context: None,
/// function: "if doc[\"id\"] % 2 == 0 { doc[\"title\"] = \"The best movie\" }".to_string(),
/// };
///
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// let movies = client.index("update_documents_by_function");
/// let interstellar = movies.update_documents_by_function(function).await.expect("Error during test");
///
/// assert_eq!(interstellar.status.len(), 2);
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn update_documents_by_function(
&self,
function: DocumentEditionByFunction,
) -> Result<TaskInfo, Error> {
let url = format!(
"{}/indexes/{}/documents/edit",
self.client.host, self.uid
);


self.client
.http_client
.request::<(), DocumentEditionByFunction, TaskInfo>(
&url,
Method::Post {
query: (),
body: function,
},
202,
)
.await
}


/// Get one document with parameters.
///
/// # Example
Expand Down
Loading