Struct meilisearch_sdk::client::Client
source · pub struct Client<Http: HttpClient = DefaultHttpClient> { /* private fields */ }
Expand description
The top-level struct of the SDK, representing a client containing indexes.
Implementations§
source§impl Client
impl Client
sourcepub fn new(
host: impl Into<String>,
api_key: Option<impl Into<String>>,
) -> Result<Client, Error>
pub fn new( host: impl Into<String>, api_key: Option<impl Into<String>>, ) -> Result<Client, Error>
Create a client using the specified server.
Don’t put a ‘/’ at the end of the host.
In production mode, see the documentation about authentication.
§Example
let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
source§impl<Http: HttpClient> Client<Http>
impl<Http: HttpClient> Client<Http>
pub fn new_with_client( host: impl Into<String>, api_key: Option<impl Into<String>>, http_client: Http, ) -> Client<Http>
pub async fn execute_multi_search_query<T: 'static + DeserializeOwned + Send + Sync>( &self, body: &MultiSearchQuery<'_, '_, Http>, ) -> Result<MultiSearchResponse<T>, Error>
sourcepub fn multi_search(&self) -> MultiSearchQuery<'_, '_, Http>
pub fn multi_search(&self) -> MultiSearchQuery<'_, '_, Http>
Make multiple search requests.
§Example
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
name: String,
description: String,
}
let mut movies = client.index("search");
let search_query_1 = SearchQuery::new(&movies)
.with_query("Interstellar")
.build();
let search_query_2 = SearchQuery::new(&movies)
.with_query("")
.build();
let response = client
.multi_search()
.with_search_query(search_query_1)
.with_search_query(search_query_2)
.execute::<Movie>()
.await
.unwrap();
assert_eq!(response.results.len(), 2);
sourcepub fn get_host(&self) -> &str
pub fn get_host(&self) -> &str
Return the host associated with this index.
§Example
let client = Client::new("http://doggo.dog", Some(MEILISEARCH_API_KEY)).unwrap();
assert_eq!(client.get_host(), "http://doggo.dog");
sourcepub fn get_api_key(&self) -> Option<&str>
pub fn get_api_key(&self) -> Option<&str>
Return the api key associated with this index.
§Example
let client = Client::new(MEILISEARCH_URL, Some("doggo")).unwrap();
assert_eq!(client.get_api_key(), Some("doggo"));
sourcepub async fn list_all_indexes(&self) -> Result<IndexesResults<Http>, Error>
pub async fn list_all_indexes(&self) -> Result<IndexesResults<Http>, Error>
sourcepub async fn list_all_indexes_with(
&self,
indexes_query: &IndexesQuery<'_, Http>,
) -> Result<IndexesResults<Http>, Error>
pub async fn list_all_indexes_with( &self, indexes_query: &IndexesQuery<'_, Http>, ) -> Result<IndexesResults<Http>, Error>
sourcepub async fn list_all_indexes_raw(&self) -> Result<Value, Error>
pub async fn list_all_indexes_raw(&self) -> Result<Value, Error>
sourcepub async fn list_all_indexes_raw_with(
&self,
indexes_query: &IndexesQuery<'_, Http>,
) -> Result<Value, Error>
pub async fn list_all_indexes_raw_with( &self, indexes_query: &IndexesQuery<'_, Http>, ) -> Result<Value, Error>
sourcepub async fn get_raw_index(&self, uid: impl AsRef<str>) -> Result<Value, Error>
pub async fn get_raw_index(&self, uid: impl AsRef<str>) -> Result<Value, Error>
Get a raw JSON Index, this index should already exist.
If you use it directly from an Index, you can use the method Index::fetch_info
, which is the equivalent method from an index.
§Example
let raw_index = client.get_raw_index("get_raw_index").await.unwrap();
assert_eq!(raw_index.get("uid").unwrap().as_str().unwrap(), "get_raw_index");
sourcepub fn index(&self, uid: impl Into<String>) -> Index<Http>
pub fn index(&self, uid: impl Into<String>) -> Index<Http>
Create a corresponding object of an Index without any check or doing an HTTP call.
sourcepub async fn create_index(
&self,
uid: impl AsRef<str>,
primary_key: Option<&str>,
) -> Result<TaskInfo, Error>
pub async fn create_index( &self, uid: impl AsRef<str>, primary_key: Option<&str>, ) -> Result<TaskInfo, Error>
Create an Index.
The second parameter will be used as the primary key of the new index. If it is not specified, Meilisearch will try to infer the primary key.
§Example
// Create a new index called movies and access it
let task = client.create_index("create_index", None).await.unwrap();
// Wait for the task to complete
let task = task.wait_for_completion(&client, None, None).await.unwrap();
// Try to get the inner index if the task succeeded
let index = task.try_make_index(&client).unwrap();
assert_eq!(index.as_ref(), "create_index");
sourcepub async fn delete_index(
&self,
uid: impl AsRef<str>,
) -> Result<TaskInfo, Error>
pub async fn delete_index( &self, uid: impl AsRef<str>, ) -> Result<TaskInfo, Error>
Delete an index from its UID.
To delete an Index, use the Index::delete
method.
sourcepub async fn get_indexes(&self) -> Result<IndexesResults<Http>, Error>
pub async fn get_indexes(&self) -> Result<IndexesResults<Http>, Error>
Alias for Client::list_all_indexes
.
sourcepub async fn get_indexes_with(
&self,
indexes_query: &IndexesQuery<'_, Http>,
) -> Result<IndexesResults<Http>, Error>
pub async fn get_indexes_with( &self, indexes_query: &IndexesQuery<'_, Http>, ) -> Result<IndexesResults<Http>, Error>
Alias for Client::list_all_indexes_with
.
sourcepub async fn get_indexes_raw(&self) -> Result<Value, Error>
pub async fn get_indexes_raw(&self) -> Result<Value, Error>
Alias for Client::list_all_indexes_raw
.
sourcepub async fn get_indexes_raw_with(
&self,
indexes_query: &IndexesQuery<'_, Http>,
) -> Result<Value, Error>
pub async fn get_indexes_raw_with( &self, indexes_query: &IndexesQuery<'_, Http>, ) -> Result<Value, Error>
Alias for Client::list_all_indexes_raw_with
.
sourcepub async fn swap_indexes(
&self,
indexes: impl IntoIterator<Item = &SwapIndexes>,
) -> Result<TaskInfo, Error>
pub async fn swap_indexes( &self, indexes: impl IntoIterator<Item = &SwapIndexes>, ) -> Result<TaskInfo, Error>
Swaps a list of two Indexes.
§Example
let task_index_1 = client.create_index("swap_index_1", None).await.unwrap();
let task_index_2 = client.create_index("swap_index_2", None).await.unwrap();
// Wait for the task to complete
task_index_2.wait_for_completion(&client, None, None).await.unwrap();
let task = client
.swap_indexes([&SwapIndexes {
indexes: (
"swap_index_1".to_string(),
"swap_index_2".to_string(),
),
}])
.await
.unwrap();
client.index("swap_index_1").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
client.index("swap_index_2").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
sourcepub async fn get_stats(&self) -> Result<ClientStats, Error>
pub async fn get_stats(&self) -> Result<ClientStats, Error>
sourcepub async fn health(&self) -> Result<Health, Error>
pub async fn health(&self) -> Result<Health, Error>
Get health of Meilisearch server.
§Example
let health = client.health().await.unwrap();
assert_eq!(health.status, "available");
sourcepub async fn is_healthy(&self) -> bool
pub async fn is_healthy(&self) -> bool
Get health of Meilisearch server.
§Example
let health = client.is_healthy().await;
assert_eq!(health, true);
sourcepub async fn get_keys_with(
&self,
keys_query: &KeysQuery,
) -> Result<KeysResults, Error>
pub async fn get_keys_with( &self, keys_query: &KeysQuery, ) -> Result<KeysResults, Error>
Get the API Keys from Meilisearch with parameters.
See Client::create_key
, Client::get_key
, and the meilisearch documentation.
§Example
let mut query = KeysQuery::new();
query.with_limit(1);
let keys = client.get_keys_with(&query).await.unwrap();
assert_eq!(keys.results.len(), 1);
sourcepub async fn get_keys(&self) -> Result<KeysResults, Error>
pub async fn get_keys(&self) -> Result<KeysResults, Error>
Get the API Keys from Meilisearch.
See Client::create_key
, Client::get_key
, and the meilisearch documentation.
§Example
let keys = client.get_keys().await.unwrap();
assert_eq!(keys.limit, 20);
sourcepub async fn get_key(&self, key: impl AsRef<str>) -> Result<Key, Error>
pub async fn get_key(&self, key: impl AsRef<str>) -> Result<Key, Error>
Get one API Key from Meilisearch.
See also Client::create_key
, Client::get_keys
, and the meilisearch documentation.
§Example
let key = client.get_key(key).await.expect("Invalid key");
assert_eq!(key.name, Some("Default Search API Key".to_string()));
sourcepub async fn delete_key(&self, key: impl AsRef<str>) -> Result<(), Error>
pub async fn delete_key(&self, key: impl AsRef<str>) -> Result<(), Error>
Delete an API Key from Meilisearch.
See also Client::create_key
, Client::update_key
, Client::get_key
, and the meilisearch documentation.
§Example
let key = KeyBuilder::new();
let key = client.create_key(key).await.unwrap();
let inner_key = key.key.clone();
client.delete_key(key).await.unwrap();
let keys = client.get_keys().await.unwrap();
assert!(keys.results.iter().all(|key| key.key != inner_key));
sourcepub async fn create_key(
&self,
key: impl AsRef<KeyBuilder>,
) -> Result<Key, Error>
pub async fn create_key( &self, key: impl AsRef<KeyBuilder>, ) -> Result<Key, Error>
Create an API Key in Meilisearch.
See also Client::update_key
, Client::delete_key
, Client::get_key
, and the meilisearch documentation.
§Example
let name = "create_key".to_string();
let mut key = KeyBuilder::new();
key.with_name(&name);
let key = client.create_key(key).await.unwrap();
assert_eq!(key.name, Some(name));
sourcepub async fn update_key(
&self,
key: impl AsRef<KeyUpdater>,
) -> Result<Key, Error>
pub async fn update_key( &self, key: impl AsRef<KeyUpdater>, ) -> Result<Key, Error>
Update an API Key in Meilisearch.
See also Client::create_key
, Client::delete_key
, Client::get_key
, and the meilisearch documentation.
§Example
let new_key = KeyBuilder::new();
let mut new_key = client.create_key(new_key).await.unwrap();
let mut key_update = KeyUpdater::new(new_key);
let name = "my name".to_string();
key_update.with_name(&name);
let key = client.update_key(key_update).await.unwrap();
assert_eq!(key.name, Some(name));
sourcepub async fn get_version(&self) -> Result<Version, Error>
pub async fn get_version(&self) -> Result<Version, Error>
sourcepub async fn wait_for_task(
&self,
task_id: impl AsRef<u32>,
interval: Option<Duration>,
timeout: Option<Duration>,
) -> Result<Task, Error>
pub async fn wait_for_task( &self, task_id: impl AsRef<u32>, interval: Option<Duration>, timeout: Option<Duration>, ) -> Result<Task, Error>
Wait until Meilisearch processes a Task, and get its status.
interval
= The frequency at which the server should be polled. Default = 50ms
timeout
= The maximum time to wait for processing to complete. Default = 5000ms
If the waited time exceeds timeout
then an Error::Timeout
will be returned.
See also [Index::wait_for_task
, Task::wait_for_completion
, TaskInfo::wait_for_completion
].
§Example
let movies = client.index("movies_client_wait_for_task");
let task = 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 status = client.wait_for_task(task, None, None).await.unwrap();
assert!(matches!(status, Task::Succeeded { .. }));
sourcepub async fn get_task(&self, task_id: impl AsRef<u32>) -> Result<Task, Error>
pub async fn get_task(&self, task_id: impl AsRef<u32>) -> Result<Task, Error>
Get a task from the server given a task id.
§Example
let task = index.delete_all_documents().await.unwrap();
let task = client.get_task(task).await.unwrap();
sourcepub async fn get_tasks_with(
&self,
tasks_query: &TasksSearchQuery<'_, Http>,
) -> Result<TasksResults, Error>
pub async fn get_tasks_with( &self, tasks_query: &TasksSearchQuery<'_, Http>, ) -> Result<TasksResults, Error>
Get all tasks with query parameters from the server.
§Example
let mut query = TasksSearchQuery::new(&client);
query.with_index_uids(["get_tasks_with"]);
let tasks = client.get_tasks_with(&query).await.unwrap();
sourcepub async fn cancel_tasks_with(
&self,
filters: &TasksCancelQuery<'_, Http>,
) -> Result<TaskInfo, Error>
pub async fn cancel_tasks_with( &self, filters: &TasksCancelQuery<'_, Http>, ) -> Result<TaskInfo, Error>
Cancel tasks with filters TasksCancelQuery
.
§Example
let mut query = TasksCancelQuery::new(&client);
query.with_index_uids(["movies"]);
let res = client.cancel_tasks_with(&query).await.unwrap();
sourcepub async fn delete_tasks_with(
&self,
filters: &TasksDeleteQuery<'_, Http>,
) -> Result<TaskInfo, Error>
pub async fn delete_tasks_with( &self, filters: &TasksDeleteQuery<'_, Http>, ) -> Result<TaskInfo, Error>
Delete tasks with filters TasksDeleteQuery
.
§Example
let mut query = TasksDeleteQuery::new(&client);
query.with_index_uids(["movies"]);
let res = client.delete_tasks_with(&query).await.unwrap();
sourcepub async fn get_tasks(&self) -> Result<TasksResults, Error>
pub async fn get_tasks(&self) -> Result<TasksResults, Error>
Get all tasks from the server.
§Example
let tasks = client.get_tasks().await.unwrap();
assert!(tasks.results.len() > 0);
sourcepub fn generate_tenant_token(
&self,
api_key_uid: String,
search_rules: Value,
api_key: Option<&str>,
expires_at: Option<OffsetDateTime>,
) -> Result<String, Error>
pub fn generate_tenant_token( &self, api_key_uid: String, search_rules: Value, api_key: Option<&str>, expires_at: Option<OffsetDateTime>, ) -> Result<String, Error>
Generates a new tenant token.
§Example
let api_key_uid = "76cf8b87-fd12-4688-ad34-260d930ca4f4".to_string();
let token = client.generate_tenant_token(api_key_uid, serde_json::json!(["*"]), None, None).unwrap();
let client = Client::new(MEILISEARCH_URL, Some(token)).unwrap();
source§impl<Http: HttpClient> Client<Http>
impl<Http: HttpClient> Client<Http>
Dump related methods. See the dumps module.
sourcepub async fn create_dump(&self) -> Result<TaskInfo, Error>
pub async fn create_dump(&self) -> Result<TaskInfo, Error>
Triggers a dump creation process.
Once the process is complete, a dump is created in the dumps directory. If the dumps directory does not exist yet, it will be created.
§Example
let task_info = client.create_dump().await.unwrap();
assert!(matches!(
task_info,
TaskInfo {
update_type: TaskType::DumpCreation { .. },
..
}
));
source§impl<Http: HttpClient> Client<Http>
impl<Http: HttpClient> Client<Http>
Snapshots related methods. See the snapshots module.
sourcepub async fn create_snapshot(&self) -> Result<TaskInfo, Error>
pub async fn create_snapshot(&self) -> Result<TaskInfo, Error>
Triggers a snapshots creation process.
Once the process is complete, a snapshots is created in the [snapshots directory]. If the snapshots directory does not exist yet, it will be created.
§Example
let task_info = client.create_snapshot().await.unwrap();
assert!(matches!(
task_info,
TaskInfo {
update_type: TaskType::SnapshotCreation { .. },
..
}
));
Trait Implementations§
Auto Trait Implementations§
impl<Http> Freeze for Client<Http>where
Http: Freeze,
impl<Http> RefUnwindSafe for Client<Http>where
Http: RefUnwindSafe,
impl<Http> Send for Client<Http>
impl<Http> Sync for Client<Http>
impl<Http> Unpin for Client<Http>where
Http: Unpin,
impl<Http> UnwindSafe for Client<Http>where
Http: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more