pub struct Index { /* private fields */ }
Expand description

An index containing [Document]s.

Example

You can create an index remotly and, if that succeed, make an Index out of it. See the Client::create_index method.

let client = Client::new("http://localhost:7700", "masterKey");

// get the index called movies or create it if it does not exist
let movies = client
  .create_index("index", None)
  .await
  .unwrap()
  // We wait for the task to execute until completion
  .wait_for_completion(&client, None, None)
  .await
  .unwrap()
  // Once the task finished, we try to create an `Index` out of it
  .try_make_index(&client)
  .unwrap();

assert_eq!(movies.as_ref(), "index");

Or, if you know the index already exist remotely you can create an Index with the Client::index function.

let client = Client::new("http://localhost:7700", "masterKey");

// use the implicit index creation if the index already exist or
// Meilisearch would be able to create the index if it does not exist during:
// - the documents addition (add and update routes)
// - the settings update
let movies = client.index("index");

// do something with the index

Implementations

Set the primary key of the index.

If you prefer, you can use the method Index::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 index = client.index("delete");
let task = index.delete().await.unwrap();
client.wait_for_task(task, None, None).await.unwrap();

Search for documents matching a specific query in the index.
See also Index::search.

Example
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct Movie {
    name: String,
    description: String,
}

let client = Client::new("http://localhost:7700", "masterKey");
let movies = client.index("execute_query");

// add some documents

let query = Query::new(&movies).with_query("Interstellar").with_limit(5).build();
let results = movies.execute_query::<Movie>(&query).await.unwrap();
assert!(results.hits.len()>0);

Search for documents matching a specific query in the index.
See also Index::execute_query.

Example
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct Movie {
    name: String,
    description: String,
}

let client = Client::new("http://localhost:7700", "masterKey");
let mut movies = client.index("search");

// add some documents

let results = movies.search()
    .with_query("Interstellar")
    .with_limit(5)
    .execute::<Movie>()
    .await
    .unwrap();

assert!(results.hits.len()>0);

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,
}


let client = Client::new("http://localhost:7700", "masterKey");
let movies = client.index("get_documents");

// retrieve a document (you have to put the document in the index before)
let interstellar = movies.get_document::<Movie>("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 [Document]s 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,
}


let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.index("get_documents");


// 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 [Document]s 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 Index::add_or_update.

You can use the alias Index::add_documents if you prefer.

Example
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct Movie {
   name: String,
   description: String,
}

let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.index("add_or_replace");

let task = 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();
// Meilisearch may take some time to execute the request so we are going to wait till it's completed
client.wait_for_task(task, None, None).await.unwrap();

let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
assert!(movies.len() >= 3);

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 Index::add_or_replace documents method.

Example
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct Movie {
   name: String,
   description: String,
}

let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.index("add_or_update");

let task = 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();

// Meilisearch may take some time to execute the request so we are going to wait till it's completed
client.wait_for_task(task, None, None).await.unwrap();

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 movie_index = client.index("delete_all_documents");

// add some documents

movie_index.delete_all_documents()
  .await
  .unwrap()
  .wait_for_completion(&client, None, None)
  .await
  .unwrap();
let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
assert_eq!(movies.len(), 0);

Delete one document based on its unique id.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut movies = client.index("delete_document");

// add a document with id = Interstellar

movies.delete_document("Interstellar")
  .await
  .unwrap()
  .wait_for_completion(&client, None, None)
  .await
  .unwrap();

Delete a selection of documents based on array of document id’s.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let movies = client.index("delete_documents");

// add some documents

// delete some documents
movies.delete_documents(&["Interstellar", "Unknown"])
  .await
  .unwrap()
  .wait_for_completion(&client, None, None)
  .await
  .unwrap();

Alias for the Index::update method.

Fetch the information of the index as a raw JSON Index, this index should already exist.

Example

// create the client
let client = Client::new("http://localhost:7700", "masterKey");

// get the information of the index named "fetch_info"
let movies = client.index("fetch_info").fetch_info().await.unwrap();

If you use it directly from the Client, you can use the method Client::get_raw_index, which is the equivalent method from the client.

Fetch the primary key of the index.

Example

// create the client
let client = Client::new("http://localhost:7700", "masterKey");

// get the primary key of the index named "movies"
let movies = client.index("movies").get_primary_key().await;

Get a Task from a specific Index to keep track of asynchronous operations.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let movies = client.index("get_task");

let task = movies.add_documents(&[
    Document { id: 0, kind: "title".into(), value: "The Social Network".to_string() }
], None).await.unwrap();

// Get task status from the index, using `uid`
let status = movies.get_task(&task).await.unwrap();

let from_index = match status {
   Task::Enqueued { content } => content.uid,
   Task::Processing { content } => content.uid,
   Task::Failed { content } => content.task.uid,
   Task::Succeeded { content } => content.uid,
};

assert_eq!(task.get_uid(), from_index);

Get the status of all tasks in a given index.

Example

let status = index.get_tasks().await.unwrap();
assert!(status.len() == 1); // the index was created

index.set_ranking_rules(["wrong_ranking_rule"]).await.unwrap();

let status = index.get_tasks().await.unwrap();
assert!(status.len() == 2);

Get stats of an index.

Example

let stats = index.get_stats().await.unwrap();
assert_eq!(stats.is_indexing, false);

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 [Client::wait_for_task, Task::wait_for_completion].

Example
let client = Client::new("http://localhost:7700", "masterKey");
let movies = client.index("movies_index_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 = movies.wait_for_task(task, None, None).await.unwrap();

assert!(matches!(status, Task::Succeeded { .. }));

Add documents to the index in batches

documents = A slice of documents batch_size = Optional parameter that allows you to specify the size of the batch batch_size is 1000 by default

Example
use serde::{Serialize, Deserialize};
use meilisearch_sdk::client::*;

#[derive(Serialize, Deserialize, Debug)]
struct Movie {
    name: String,
    description: String,
}

let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.index("add_documents_in_batches");

let tasks = movie_index.add_documents_in_batches(&[
 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(1),
    Some("name")
).await.unwrap();

client.wait_for_task(tasks.last().unwrap(), None, None).await.unwrap();

let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
assert!(movies.len() >= 3);
None).await.unwrap();

Update documents to the index in batches

documents = A slice of documents batch_size = Optional parameter that allows you to specify the size of the batch batch_size is 1000 by default

Example
use serde::{Serialize, Deserialize};
use meilisearch_sdk::client::*;

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct Movie {
    name: String,
    description: String,
}

let client = Client::new("http://localhost:7700", "masterKey");
let movie_index = client.index("update_documents_in_batches");

let tasks = movie_index.add_documents_in_batches(&[
 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(1),
    Some("name")
).await.unwrap();

client.wait_for_task(tasks.last().unwrap(), None, None).await.unwrap();

let movies = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
assert!(movies.len() >= 3);

let updated_movies = [
 Movie {
        name: String::from("Interstellar"),
        description: String::from("Updated!")
 },
 Movie {
        // note that the id field can only take alphanumerics characters (and '-' and '/')
        name: String::from("MrsDoubtfire"),
        description: String::from("Updated!")
 },
 Movie {
        name: String::from("Apollo13"),
        description: String::from("Updated!")
}];

let tasks = movie_index.update_documents_in_batches(&updated_movies, Some(1), None).await.unwrap();

client.wait_for_task(tasks.last().unwrap(), None, None).await.unwrap();

let movies_updated = movie_index.get_documents::<Movie>(None, None, None).await.unwrap();
assert!(movies_updated.len() >= 3);

assert!(&movies_updated[..] == &updated_movies[..]);

None).await.unwrap();

Get Settings of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_settings");
let settings = index.get_settings().await.unwrap();

Get synonyms of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_synonyms");
let synonyms = index.get_synonyms().await.unwrap();

Get stop-words of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_stop_words");
let stop_words = index.get_stop_words().await.unwrap();

Get ranking rules of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_ranking_rules");
let ranking_rules = index.get_ranking_rules().await.unwrap();

Get filterable attributes of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_filterable_attributes");
let filterable_attributes = index.get_filterable_attributes().await.unwrap();

Get sortable attributes of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_sortable_attributes");
let sortable_attributes = index.get_sortable_attributes().await.unwrap();

Get the distinct attribute of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_distinct_attribute");
let distinct_attribute = index.get_distinct_attribute().await.unwrap();

Get searchable attributes of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_searchable_attributes");
let searchable_attributes = index.get_searchable_attributes().await.unwrap();

Get displayed attributes of the Index.

let client = Client::new("http://localhost:7700", "masterKey");
let index = client.index("get_displayed_attributes");
let displayed_attributes = 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 index = client.index("set_settings");

let stop_words = vec![String::from("a"), String::from("the"), String::from("of")];
let settings = Settings::new()
    .with_stop_words(stop_words.clone());

let task = index.set_settings(&settings).await.unwrap();

Update synonyms of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("set_synonyms");

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 task = index.set_synonyms(&synonyms).await.unwrap();

Update stop-words of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("set_stop_words");

let stop_words = ["the", "of", "to"];
let task = index.set_stop_words(&stop_words).await.unwrap();

Update ranking rules of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("set_ranking_rules");

let ranking_rules = [
    "words",
    "typo",
    "proximity",
    "attribute",
    "sort",
    "exactness",
    "release_date:asc",
    "rank:desc",
];
let task = index.set_ranking_rules(ranking_rules).await.unwrap();

Update filterable attributes of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("set_filterable_attributes");

let filterable_attributes = ["genre", "director"];
let task = index.set_filterable_attributes(&filterable_attributes).await.unwrap();

Update sortable attributes of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("set_sortable_attributes");

let sortable_attributes = ["genre", "director"];
let task = 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 index = client.index("set_distinct_attribute");

let task = index.set_distinct_attribute("movie_id").await.unwrap();

Update searchable attributes of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("set_searchable_attributes");

let task = index.set_searchable_attributes(["title", "description", "uid"]).await.unwrap();

Update displayed attributes of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("set_displayed_attributes");

let task = 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 index = client.index("reset_settings");

let task = index.reset_settings().await.unwrap();

Reset synonyms of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("reset_synonyms");

let task = index.reset_synonyms().await.unwrap();

Reset stop-words of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("reset_stop_words");

let task = 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 index = client.index("reset_ranking_rules");

let task = index.reset_ranking_rules().await.unwrap();

Reset filterable attributes of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("reset_filterable_attributes");

let task = index.reset_filterable_attributes().await.unwrap();

Reset sortable attributes of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("reset_sortable_attributes");

let task = index.reset_sortable_attributes().await.unwrap();

Reset the distinct attribute of the Index.

Example
let client = Client::new("http://localhost:7700", "masterKey");
let mut index = client.index("reset_distinct_attribute");

let task = 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 index = client.index("reset_searchable_attributes");

let task = 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 index = client.index("reset_displayed_attributes");

let task = index.reset_displayed_attributes().await.unwrap();

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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