Skip to content

Commit 835b86f

Browse files
meili-bors[bot]hmacrcurquiza
authored
Merge #516
516: Implement client for experimental-features API r=curquiza a=hmacr # Pull Request ## Related issue Needed for #498 & #501, since these issues involve updating the experimental-feature as a pre-requisite ## What does this PR do? - Introduces the experimental-features client in the SDK - Implements the `get` and `patch` endpoint. - Adds code-samples and tests. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: hmacr <[email protected]> Co-authored-by: Clémentine U. - curqui <[email protected]>
2 parents f6fbe74 + 8997683 commit 835b86f

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

.code-samples.meilisearch.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -1665,3 +1665,19 @@ multi_search_1: |-
16651665
.execute::<MovieRatings>()
16661666
.await
16671667
.unwrap();
1668+
get_experimental_features_1: |-
1669+
let client = Client::new("http://localhost:7700", Some("apiKey"));
1670+
let features = ExperimentalFeatures::new(&client);
1671+
let res = features
1672+
.get()
1673+
.await
1674+
.unwrap();
1675+
update_experimental_features_1: |-
1676+
let client = Client::new("http://localhost:7700", Some("apiKey"));
1677+
let mut features = ExperimentalFeatures::new(&client);
1678+
features.set_score_details(true);
1679+
features.set_vector_store(true);
1680+
let res = features
1681+
.update()
1682+
.await
1683+
.unwrap();

src/features.rs

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
use crate::{
2+
request::{request, Method},
3+
Client, Error,
4+
};
5+
use serde::{Deserialize, Serialize};
6+
7+
/// Struct representing the experimental features result from the API.
8+
#[derive(Clone, Debug, Deserialize)]
9+
#[serde(rename_all = "camelCase")]
10+
pub struct ExperimentalFeaturesResult {
11+
pub score_details: bool,
12+
pub vector_store: bool,
13+
}
14+
15+
/// Struct representing the experimental features request.
16+
///
17+
/// You can build this struct using the builder pattern.
18+
///
19+
/// # Example
20+
///
21+
/// ```
22+
/// # use meilisearch_sdk::{Client, ExperimentalFeatures};
23+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
24+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
25+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
26+
/// let mut features = ExperimentalFeatures::new(&client);
27+
/// features.set_score_details(true);
28+
/// ```
29+
#[derive(Debug, Serialize)]
30+
#[serde(rename_all = "camelCase")]
31+
pub struct ExperimentalFeatures<'a> {
32+
#[serde(skip_serializing)]
33+
client: &'a Client,
34+
#[serde(skip_serializing_if = "Option::is_none")]
35+
pub score_details: Option<bool>,
36+
#[serde(skip_serializing_if = "Option::is_none")]
37+
pub vector_store: Option<bool>,
38+
}
39+
40+
impl<'a> ExperimentalFeatures<'a> {
41+
pub fn new(client: &'a Client) -> Self {
42+
ExperimentalFeatures {
43+
client,
44+
score_details: None,
45+
vector_store: None,
46+
}
47+
}
48+
49+
pub fn set_score_details(&mut self, score_details: bool) -> &mut Self {
50+
self.score_details = Some(score_details);
51+
self
52+
}
53+
54+
pub fn set_vector_store(&mut self, vector_store: bool) -> &mut Self {
55+
self.vector_store = Some(vector_store);
56+
self
57+
}
58+
59+
/// Get all the experimental features
60+
///
61+
/// # Example
62+
///
63+
/// ```
64+
/// # use meilisearch_sdk::{Client, ExperimentalFeatures};
65+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
66+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
67+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
68+
/// futures::executor::block_on(async move {
69+
/// let features = ExperimentalFeatures::new(&client);
70+
/// features.get().await.unwrap();
71+
/// });
72+
/// ```
73+
pub async fn get(&self) -> Result<ExperimentalFeaturesResult, Error> {
74+
request::<(), (), ExperimentalFeaturesResult>(
75+
&format!("{}/experimental-features", self.client.host),
76+
self.client.get_api_key(),
77+
Method::Get { query: () },
78+
200,
79+
)
80+
.await
81+
}
82+
83+
/// Update the experimental features
84+
///
85+
/// # Example
86+
///
87+
/// ```
88+
/// # use meilisearch_sdk::{Client, ExperimentalFeatures};
89+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
90+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
91+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
92+
/// futures::executor::block_on(async move {
93+
/// let mut features = ExperimentalFeatures::new(&client);
94+
/// features.set_score_details(true);
95+
/// features.update().await.unwrap();
96+
/// });
97+
/// ```
98+
pub async fn update(&self) -> Result<ExperimentalFeaturesResult, Error> {
99+
request::<(), &Self, ExperimentalFeaturesResult>(
100+
&format!("{}/experimental-features", self.client.host),
101+
self.client.get_api_key(),
102+
Method::Patch {
103+
query: (),
104+
body: self,
105+
},
106+
200,
107+
)
108+
.await
109+
}
110+
}
111+
112+
#[cfg(test)]
113+
mod tests {
114+
use super::*;
115+
use meilisearch_test_macro::meilisearch_test;
116+
117+
#[meilisearch_test]
118+
async fn test_experimental_features_get(client: Client) {
119+
let mut features = ExperimentalFeatures::new(&client);
120+
features.set_score_details(false);
121+
features.set_vector_store(false);
122+
let _ = features.update().await.unwrap();
123+
124+
let res = features.get().await.unwrap();
125+
126+
assert!(!res.score_details);
127+
assert!(!res.vector_store);
128+
}
129+
130+
#[meilisearch_test]
131+
async fn test_experimental_features_enable_score_details(client: Client) {
132+
let mut features = ExperimentalFeatures::new(&client);
133+
features.set_score_details(true);
134+
135+
let res = features.update().await.unwrap();
136+
137+
assert!(res.score_details);
138+
}
139+
140+
#[meilisearch_test]
141+
async fn test_experimental_features_enable_vector_store(client: Client) {
142+
let mut features = ExperimentalFeatures::new(&client);
143+
features.set_vector_store(true);
144+
145+
let res = features.update().await.unwrap();
146+
147+
assert!(res.vector_store);
148+
}
149+
}

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ pub mod documents;
231231
pub mod dumps;
232232
/// Module containing the [`errors::Error`] struct.
233233
pub mod errors;
234+
/// Module related to runtime and instance features.
235+
pub mod features;
234236
/// Module containing the Index struct.
235237
pub mod indexes;
236238
/// Module containing the [`key::Key`] struct.
@@ -253,6 +255,7 @@ pub use client::*;
253255
pub use documents::*;
254256
pub use dumps::*;
255257
pub use errors::*;
258+
pub use features::*;
256259
pub use indexes::*;
257260
pub use key::*;
258261
pub use search::*;

0 commit comments

Comments
 (0)