diff --git a/src/indexes.rs b/src/indexes.rs index 05c728f1..cdd910c1 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -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; @@ -76,6 +77,13 @@ pub struct Index { pub primary_key: Option, } +#[derive(Debug, Serialize, Deserialize)] +pub struct DocumentEditionByFunction { + pub filter: Option, + pub context: Option, + pub function: String, +} + impl Index { pub fn new(uid: impl Into, client: Client) -> Index { Index { @@ -327,6 +335,62 @@ impl Index { .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 { + 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