Struct meilisearch_sdk::indexes::Index [−][src]
pub struct Index { /* fields omitted */ }
Expand description
Implementations
Set the primary key of the index.
If you prefer, you can use the method set_primary_key, which is an alias.
Delete the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
// get the index named "movies" and delete it
let movies = client.get_index("movies").await.unwrap();
movies.delete().await.unwrap();
Delete the index if it exists.
Example
let client = Client::new("http://localhost:7700", "masterKey");
client.create_index("movies", None).await;
// get the index named "movies" and delete it
let movies = client.assume_index("movies");
let mut deleted = movies.delete_if_exists().await.unwrap();
assert_eq!(deleted, true);
let index = client.get_index("movies").await;
assert!(index.is_err());
// get an index that doesn't exist and try to delete it
let no_index = client.assume_index("no_index");
deleted = no_index.delete_if_exists().await.unwrap();
assert_eq!(deleted, false);
pub async fn execute_query<T: 'static + DeserializeOwned>(
&self,
query: &Query<'_>
) -> Result<SearchResults<T>, Error>
pub async fn execute_query<T: 'static + DeserializeOwned>(
&self,
query: &Query<'_>
) -> Result<SearchResults<T>, Error>
Search for documents matching a specific query in the index.
See also the search method.
Example
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
name: String,
description: String,
}
// that trait is used by the sdk when the primary key is needed
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType {
&self.name
}
}
let client = Client::new("http://localhost:7700", "masterKey");
let mut movies = client.get_or_create("movies").await.unwrap();
// add some documents
let query = Query::new(&movies).with_query("Interstellar").with_limit(5).build();
let results = movies.execute_query::<Movie>(&query).await.unwrap();
Search for documents matching a specific query in the index.
See also the execute_query method.
Example
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
name: String,
description: String,
}
// that trait is used by the sdk when the primary key is needed
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType {
&self.name
}
}
let client = Client::new("http://localhost:7700", "masterKey");
let mut movies = client.get_or_create("movies").await.unwrap();
// add some documents
let results = movies.search()
.with_query("Interstellar")
.with_limit(5)
.execute::<Movie>()
.await
.unwrap();
Get one document using its unique id.
Serde is needed. Add serde = {version="1.0", features=["derive"]}
in the dependencies section of your Cargo.toml.
Example
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
name: String,
description: String,
}
// that trait is used by the sdk when the primary key is needed
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType {
&self.name
}
}
let client = Client::new("http://localhost:7700", "masterKey");
let movies = client.get_index("movies").await.unwrap();
// retrieve a document (you have to put the document in the index before)
let interstellar = movies.get_document::<Movie>(String::from("Interstellar")).await.unwrap();
assert_eq!(interstellar, Movie{
name: String::from("Interstellar"),
description: String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")
});
Get documents by batch.
Using the optional parameters offset and limit, you can browse through all your documents. If None, offset will be set to 0, limit to 20, and all attributes will be retrieved.
Note: Documents are ordered by MeiliSearch depending on the hash of their id.
Example
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
name: String,
description: String,
}
// that trait is used by the sdk when the primary key is needed
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType {
&self.name
}
}
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_index("movies").await.unwrap();
// retrieve movies (you have to put some movies in the index before)
let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
assert!(movies.len() > 0);
Add a list of documents or replace them if they already exist.
If you send an already existing document (same id) the whole existing document will be overwritten by the new document. Fields previously in the document not present in the new document are removed.
For a partial update of the document see add_or_update.
You can use the alias add_documents if you prefer.
Example
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
name: String,
description: String,
}
// that trait is used by the sdk when the primary key is needed
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType {
&self.name
}
}
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies_add_or_replace").await.unwrap();
let progress = movie_index.add_or_replace(&[
Movie{
name: String::from("Interstellar"),
description: String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")
},
Movie{
// note that the id field can only take alphanumerics characters (and '-' and '/')
name: String::from("MrsDoubtfire"),
description: String::from("Loving but irresponsible dad Daniel Hillard, estranged from his exasperated spouse, is crushed by a court order allowing only weekly visits with his kids. When Daniel learns his ex needs a housekeeper, he gets the job -- disguised as an English nanny. Soon he becomes not only his children's best pal but the kind of parent he should have been from the start.")
},
Movie{
name: String::from("Apollo13"),
description: String::from("The true story of technical troubles that scuttle the Apollo 13 lunar mission in 1971, risking the lives of astronaut Jim Lovell and his crew, with the failed journey turning into a thrilling saga of heroism. Drifting more than 200,000 miles from Earth, the astronauts work furiously with the ground crew to avert tragedy.")
},
], Some("name")).await.unwrap();
sleep(Duration::from_secs(1)); // MeiliSearch may take some time to execute the request
// retrieve movies (you have to put some movies in the index before)
let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
assert!(movies.len() >= 3);
Alias for add_or_replace.
Add a list of documents and update them if they already.
If you send an already existing document (same id) the old document will be only partially updated according to the fields of the new document. Thus, any fields not present in the new document are kept and remained unchanged.
To completely overwrite a document, check out the add_and_replace documents method.
Example
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
name: String,
description: String,
}
// that trait is used by the sdk when the primary key is needed
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType {
&self.name
}
}
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies_add_or_update").await.unwrap();
let progress = movie_index.add_or_update(&[
Movie{
name: String::from("Interstellar"),
description: String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")
},
Movie{
// note that the id field can only take alphanumerics characters (and '-' and '/')
name: String::from("MrsDoubtfire"),
description: String::from("Loving but irresponsible dad Daniel Hillard, estranged from his exasperated spouse, is crushed by a court order allowing only weekly visits with his kids. When Daniel learns his ex needs a housekeeper, he gets the job -- disguised as an English nanny. Soon he becomes not only his children's best pal but the kind of parent he should have been from the start.")
},
Movie{
name: String::from("Apollo13"),
description: String::from("The true story of technical troubles that scuttle the Apollo 13 lunar mission in 1971, risking the lives of astronaut Jim Lovell and his crew, with the failed journey turning into a thrilling saga of heroism. Drifting more than 200,000 miles from Earth, the astronauts work furiously with the ground crew to avert tragedy.")
},
], Some("name")).await.unwrap();
sleep(Duration::from_secs(1)); // MeiliSearch may take some time to execute the request
// retrieve movies (you have to put some movies in the index before)
let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
assert!(movies.len() >= 3);
Delete all documents in the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
// add some documents
let progress = movie_index.delete_all_documents().await.unwrap();
Delete one document based on its unique id.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movies = client.get_or_create("movies").await.unwrap();
// add a document with id = Interstellar
let progress = movies.delete_document("Interstellar").await.unwrap();
Delete a selection of documents based on array of document id’s.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movies = client.get_or_create("movies").await.unwrap();
// add some documents
// delete some documents
let progress = movies.delete_documents(&["Interstellar", "Unknown"]).await.unwrap();
Alias for the update method.
Get the status of an update on the index.
After executing an update, a Progress
struct is returned,
you can use this struct to check on the status of the update.
In some cases, you might not need the status of the update directly, or would rather not wait for it to resolve.
For these cases, you can get the update_id
from the Progress
struct and use it to query the index later on.
For example, if a clients updates an entry over an HTTP request,
you can respond with the update_id
and have the client check
on the update status later on.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let movies = client.get_or_create("movies_get_one_update").await.unwrap();
let progress = movies.add_documents(&[
Document { id: 0, kind: "title".into(), value: "The Social Network".to_string() }
], None).await.unwrap();
// Get update status directly on the progress object
let status = progress.get_status().await.unwrap();
let from_progress = match status {
UpdateStatus::Enqueued{content} => content.update_id,
UpdateStatus::Processing{content} => content.update_id,
UpdateStatus::Failed{content} => content.update_id,
UpdateStatus::Processed{content} => content.update_id,
};
let update_id = progress.get_update_id();
// Get update status from the index, using `update_id`
let status = movies.get_update(update_id).await.unwrap();
let from_index = match status {
UpdateStatus::Enqueued{content} => content.update_id,
UpdateStatus::Processing{content} => content.update_id,
UpdateStatus::Failed{content} => content.update_id,
UpdateStatus::Processed{content} => content.update_id,
};
assert_eq!(from_progress, from_index);
assert_eq!(from_progress, update_id);
Get the status of all updates in a given index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let movies = client.get_or_create("movies_get_all_updates").await.unwrap();
let status = movies.get_all_updates().await.unwrap();
assert!(status.len() >= 2);
Get settings of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let settings = movie_index.get_settings().await.unwrap();
Get synonyms of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let synonyms = movie_index.get_synonyms().await.unwrap();
Get stop-words of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let stop_words = movie_index.get_stop_words().await.unwrap();
Get ranking rules of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let ranking_rules = movie_index.get_ranking_rules().await.unwrap();
Get filterable attributes of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let filterable_attributes = movie_index.get_filterable_attributes().await.unwrap();
Get sortable attributes of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let sortable_attributes = movie_index.get_sortable_attributes().await.unwrap();
Get the distinct attribute of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let distinct_attribute = movie_index.get_distinct_attribute().await.unwrap();
Get searchable attributes of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let searchable_attributes = movie_index.get_searchable_attributes().await.unwrap();
Get displayed attributes of the Index.
let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.get_or_create("movies").await.unwrap();
let displayed_attributes = movie_index.get_displayed_attributes().await.unwrap();
Update settings of the index. Updates in the settings are partial. This means that any parameters corresponding to a None value will be left unchanged.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let stop_words = vec![String::from("a"), String::from("the"), String::from("of")];
let settings = Settings::new()
.with_stop_words(stop_words.clone());
let progress = movie_index.set_settings(&settings).await.unwrap();
Update synonyms of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let mut synonyms = std::collections::HashMap::new();
synonyms.insert(String::from("wolverine"), vec![String::from("xmen"), String::from("logan")]);
synonyms.insert(String::from("logan"), vec![String::from("xmen"), String::from("wolverine")]);
synonyms.insert(String::from("wow"), vec![String::from("world of warcraft")]);
let progress = movie_index.set_synonyms(&synonyms).await.unwrap();
Update stop-words of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let stop_words = ["the", "of", "to"];
let progress = movie_index.set_stop_words(&stop_words).await.unwrap();
pub async fn set_ranking_rules(
&self,
ranking_rules: impl IntoVecString
) -> Result<Progress, Error>
pub async fn set_ranking_rules(
&self,
ranking_rules: impl IntoVecString
) -> Result<Progress, Error>
Update ranking rules of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let ranking_rules = [
"words",
"typo",
"proximity",
"attribute",
"sort",
"exactness",
"release_date:asc",
"rank:desc",
];
let progress = movie_index.set_ranking_rules(ranking_rules).await.unwrap();
pub async fn set_filterable_attributes(
&self,
filterable_attributes: impl IntoVecString
) -> Result<Progress, Error>
pub async fn set_filterable_attributes(
&self,
filterable_attributes: impl IntoVecString
) -> Result<Progress, Error>
Update filterable attributes of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let filterable_attributes = ["genre", "director"];
let progress = movie_index.set_filterable_attributes(&filterable_attributes).await.unwrap();
pub async fn set_sortable_attributes(
&self,
sortable_attributes: impl IntoVecString
) -> Result<Progress, Error>
pub async fn set_sortable_attributes(
&self,
sortable_attributes: impl IntoVecString
) -> Result<Progress, Error>
Update sortable attributes of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let sortable_attributes = ["genre", "director"];
let progress = movie_index.set_sortable_attributes(&sortable_attributes).await.unwrap();
Update the distinct attribute of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.set_distinct_attribute("movie_id").await.unwrap();
pub async fn set_searchable_attributes(
&self,
searchable_attributes: impl IntoVecString
) -> Result<Progress, Error>
pub async fn set_searchable_attributes(
&self,
searchable_attributes: impl IntoVecString
) -> Result<Progress, Error>
Update searchable attributes of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.set_searchable_attributes(["title", "description", "uid"]).await.unwrap();
pub async fn set_displayed_attributes(
&self,
displayed_attributes: impl IntoVecString
) -> Result<Progress, Error>
pub async fn set_displayed_attributes(
&self,
displayed_attributes: impl IntoVecString
) -> Result<Progress, Error>
Update displayed attributes of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.set_displayed_attributes(["title", "description", "release_date", "rank", "poster"]).await.unwrap();
Reset settings of the index. All settings will be reset to their default value.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.reset_settings().await.unwrap();
Reset stop-words of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.reset_stop_words().await.unwrap();
Reset ranking rules of the index to default value. Default value: [“words”, “typo”, “proximity”, “attribute”, “sort”, “exactness”].
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.reset_ranking_rules().await.unwrap();
Reset [filterable attributes]https://docs.meilisearch.com/reference/features/filtering_and_faceted_search.html) of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.reset_filterable_attributes().await.unwrap();
Reset [sortable attributes]https://docs.meilisearch.com/reference/features/sorting.html) of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.reset_sortable_attributes().await.unwrap();
Reset the distinct attribute of the index.
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.reset_distinct_attribute().await.unwrap();
Reset searchable attributes of the index (enable all attributes).
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.reset_searchable_attributes().await.unwrap();
Reset displayed attributes of the index (enable all attributes).
Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movie_index = client.get_or_create("movies").await.unwrap();
let progress = movie_index.reset_displayed_attributes().await.unwrap();
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Index
impl UnwindSafe for Index
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more