Struct meilisearch_sdk::client::Client
source · [−]pub struct Client { /* private fields */ }Expand description
The top-level struct of the SDK, representing a client containing indexes.
Implementations
sourceimpl Client
impl Client
sourcepub fn new(host: impl Into<String>, api_key: impl Into<String>) -> Client
pub fn new(host: impl Into<String>, api_key: impl Into<String>) -> Client
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
// create the client
let client = Client::new("http://localhost:7700", "masterKey");sourcepub async fn get_raw_index(
&self,
uid: impl AsRef<str>
) -> Result<JsonIndex, Error>
pub async fn get_raw_index(
&self,
uid: impl AsRef<str>
) -> Result<JsonIndex, Error>
Get a raw JSON Index, this index should already exist.
Example
// create the client
let client = Client::new("http://localhost:7700", "masterKey");
// get the index named "get_raw_index"
let raw_index = client.get_raw_index("get_raw_index").await.unwrap();
assert_eq!(raw_index.uid, "get_raw_index");If you use it directly from an Index, you can use the method Index::fetch_info, which is the equivalent method from an index.
sourcepub fn index(&self, uid: impl Into<String>) -> Index
pub fn index(&self, uid: impl Into<String>) -> Index
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<Task, Error>
pub async fn create_index(
&self,
uid: impl AsRef<str>,
primary_key: Option<&str>
) -> Result<Task, 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 the client
let client = Client::new("http://localhost:7700", "masterKey");
// 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<Task, Error>
pub async fn delete_index(&self, uid: impl AsRef<str>) -> Result<Task, Error>
Delete an index from its UID. To delete an Index, use the Index::delete method.
sourcepub async fn get_indexes(&self) -> Result<Vec<Index>, Error>
pub async fn get_indexes(&self) -> Result<Vec<Index>, Error>
Alias for Client::list_all_indexes.
sourcepub async fn get_indexes_raw(&self) -> Result<Vec<JsonIndex>, Error>
pub async fn get_indexes_raw(&self) -> Result<Vec<JsonIndex>, Error>
Alias for Client::list_all_indexes_raw.
sourcepub async fn get_stats(&self) -> Result<ClientStats, Error>
pub async fn get_stats(&self) -> Result<ClientStats, Error>
Get stats of all indexes.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let stats = client.get_stats().await.unwrap();sourcepub async fn health(&self) -> Result<Health, Error>
pub async fn health(&self) -> Result<Health, Error>
Get health of Meilisearch server.
Example
let client = Client::new("http://localhost:7700", "masterKey");
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, return true or false.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let health = client.is_healthy().await;
assert_eq!(health, true);sourcepub async fn get_keys(&self) -> Result<Vec<Key>, Error>
pub async fn get_keys(&self) -> Result<Vec<Key>, Error>
Get the API Keys from Meilisearch. See the meilisearch documentation.
See also Client::create_key and Client::get_key.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let keys = client.get_keys().await.unwrap();
assert!(keys.len() >= 2);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 the meilisearch documentation.
See also Client::create_key and Client::get_keys.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let key_id = // enter your API key here, for the example we'll say we entered our search API key.
let key = client.get_key(key_id).await.unwrap();
assert_eq!(key.description, "Default Search API Key (Use it to search from the frontend)");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 the meilisearch documentation.
See also Client::create_key, Client::update_key and Client::get_key.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let key = KeyBuilder::new("delete_key");
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.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 the meilisearch documentation.
See also Client::update_key, Client::delete_key and Client::get_key.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut key = KeyBuilder::new("create_key");
key.with_index("*").with_action(Action::DocumentsAdd);
let key = client.create_key(key).await.unwrap();
assert_eq!(key.description, "create_key");sourcepub async fn update_key(&self, key: impl AsRef<Key>) -> Result<Key, Error>
pub async fn update_key(&self, key: impl AsRef<Key>) -> Result<Key, Error>
Update an API Key in Meilisearch. See the meilisearch documentation.
See also Client::create_key, Client::delete_key and Client::get_key.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let key = KeyBuilder::new("update_key");
let mut key = client.create_key(key).await.unwrap();
assert!(key.indexes.is_empty());
key.indexes = vec!["*".to_string()];
let key = client.update_key(key).await.unwrap();
assert_eq!(key.indexes, vec!["*"]);sourcepub async fn get_version(&self) -> Result<Version, Error>
pub async fn get_version(&self) -> Result<Version, Error>
Get version of the Meilisearch server.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let version = client.get_version().await.unwrap();sourcepub async fn wait_for_task(
&self,
task_id: impl AsRef<u64>,
interval: Option<Duration>,
timeout: Option<Duration>
) -> Result<Task, Error>
pub async fn wait_for_task(
&self,
task_id: impl AsRef<u64>,
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].
Example
let client = Client::new("http://localhost:7700", "masterKey");
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<u64>) -> Result<Task, Error>
pub async fn get_task(&self, task_id: impl AsRef<u64>) -> 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 fn generate_tenant_token(
&self,
search_rules: Value,
api_key: Option<&str>,
expires_at: Option<OffsetDateTime>
) -> Result<String, Error>
pub fn generate_tenant_token(
&self,
search_rules: Value,
api_key: Option<&str>,
expires_at: Option<OffsetDateTime>
) -> Result<String, Error>
Generates a new tenant token.
Example
let token = client.generate_tenant_token(serde_json::json!(["*"]), None, None).unwrap();
let client = client::Client::new("http://localhost:7700", token);sourceimpl Client
impl Client
Dump related methods.
See the dumps module.
sourcepub async fn create_dump(&self) -> Result<DumpInfo, Error>
pub async fn create_dump(&self) -> Result<DumpInfo, 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 dump_info = client.create_dump().await.unwrap();
assert!(matches!(dump_info.status, DumpStatus::InProgress));sourcepub async fn get_dump_status(
&self,
dump_uid: impl AsRef<str>
) -> Result<DumpInfo, Error>
pub async fn get_dump_status(
&self,
dump_uid: impl AsRef<str>
) -> Result<DumpInfo, Error>
Get the status of a dump creation process using the uid returned after calling the dump creation method.
Example
let dump_info = client.get_dump_status(&dump_info.uid).await.unwrap();Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnwindSafe for Client
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span, returning an
Instrumented wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more