-
Notifications
You must be signed in to change notification settings - Fork 102
Create an example showing how to update the Settings #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
.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 | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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?I like this! 👍
There was a problem hiding this comment.
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:
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 aVec
, but then you can elide thechar
because the.chars()
method returns an iterator ofchar
😁