Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ env_logger = "0.9"
futures-await-test = "0.3"
futures = "0.3"
meilisearch-test-macro = { path = "meilisearch-test-macro" }
tokio = { version = "1", features = ["rt", "macros"] }

# The following dependencies are required for examples
wasm-bindgen = "0.2"
Expand Down
65 changes: 65 additions & 0 deletions examples/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use meilisearch_sdk::client::Client;
use meilisearch_sdk::indexes::Index;
use meilisearch_sdk::settings::Settings;

// we need an async runtime
#[tokio::main(flavor = "current_thread")]
async fn main() {
let client: Client = Client::new("http://localhost:7700", "masterKey");

// We try to create an index called `movies` with a primary_key of `movie_id`.
let my_index: Index = client
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 8, you don't define the "type" but here you defined it, is this a convention or just a non-required thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, that's useless!
I copy-pasted some code and forgot to remove it.

But now you talk about it, I think it would be good to show all the types we're using, so when someone looks at the example, he knows instantaneously which function returns what without even looking at the doc.

I'll update my PR thanks 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! This is something I need to be used to, in some communities like in the C# they are going to the full var style (without explicit types). Btw there is a widely used decision regarding the theme?

he knows instantaneously which function returns what without even looking at the doc.

I like this! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep usually in rust you elide all the types you can.
If the compiler is able to infer something let it do it.
Sometimes you even write « half » a type like that:

let chars: Vec<_> = "a string".chars().collect();

Here rust can't guess which kind of data structure you want to create with the .collect method so you need to say it's a Vec, but then you can elide the char because the .chars() method returns an iterator of char 😁

.create_index("movies", Some("movie_id"))
.await
.expect("Could not join the remote server.")
// The creation of indexes is asynchronous. But for the sake of the example so we will
// wait until the update is entirely processed.
.wait_for_completion(&client, None, None)
.await
.expect("Could not join the remote server.")
// If the creation was successful we can generate an `Index` out of it.
.try_make_index(&client)
// This error comes from meilisearch itself.
.expect("An error happened with the index creation.");

// And now we can update the settings!
// You can read more about the available options here: https://docs.meilisearch.com/learn/configuration/settings.html#index-settings
let settings: Settings = Settings::new()
.with_searchable_attributes(["name", "title"])
.with_filterable_attributes(["created_at"]);

// Updating the settings is also an asynchronous operation.
let task = my_index
.set_settings(&settings)
.await
.expect("Could not join the remote server.")
// And here we wait for the operation to execute entirely so we can check any error happened.
.wait_for_completion(&client, None, None)
.await
.expect("Could not join the remote server.");

// We check if the task failed.
if task.is_failure() {
panic!(
"Could not update the settings. {}",
task.unwrap_failure().error_message
);
}

// And finally we delete the `Index`.
my_index
.delete()
.await
.expect("Could not join the remote server.")
.wait_for_completion(&client, None, None)
.await
.expect("Could not join the remote server.");

// We check if the task failed.
if task.is_failure() {
panic!(
"Could not delete the index. {}",
task.unwrap_failure().error_message
);
}
}