Expand description
§🚀 Getting started
§Add Documents
use meilisearch_sdk::client::*;
use serde::{Serialize, Deserialize};
use futures::executor::block_on;
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
id: usize,
title: String,
genres: Vec<String>,
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
// Create a client (without sending any request so that can't fail)
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
// An index is where the documents are stored.
let movies = client.index("movies");
// Add some movies in the index. If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.
movies.add_documents(&[
Movie { id: 1, title: String::from("Carol"), genres: vec!["Romance".to_string(), "Drama".to_string()] },
Movie { id: 2, title: String::from("Wonder Woman"), genres: vec!["Action".to_string(), "Adventure".to_string()] },
Movie { id: 3, title: String::from("Life of Pi"), genres: vec!["Adventure".to_string(), "Drama".to_string()] },
Movie { id: 4, title: String::from("Mad Max"), genres: vec!["Adventure".to_string(), "Science Fiction".to_string()] },
Movie { id: 5, title: String::from("Moana"), genres: vec!["Fantasy".to_string(), "Action".to_string()] },
Movie { id: 6, title: String::from("Philadelphia"), genres: vec!["Drama".to_string()] },
], Some("id")).await.unwrap();
}
With the uid
, you can check the status (enqueued
, canceled
, processing
, succeeded
or failed
) of your documents addition using the task.
§Basic Search
// Meilisearch is typo-tolerant:
println!("{:?}", client.index("movies_2").search().with_query("caorl").execute::<Movie>().await.unwrap().hits);
Output:
[Movie { id: 1, title: String::from("Carol"), genres: vec!["Romance", "Drama"] }]
Json output:
{
"hits": [{
"id": 1,
"title": "Carol",
"genres": ["Romance", "Drama"]
}],
"offset": 0,
"limit": 10,
"processingTimeMs": 1,
"query": "caorl"
}
§Custom Search
let search_result = client.index("movies_3")
.search()
.with_query("phil")
.with_attributes_to_highlight(Selectors::Some(&["*"]))
.execute::<Movie>()
.await
.unwrap();
println!("{:?}", search_result.hits);
Json output:
{
"hits": [
{
"id": 6,
"title": "Philadelphia",
"_formatted": {
"id": 6,
"title": "<em>Phil</em>adelphia",
"genre": ["Drama"]
}
}
],
"offset": 0,
"limit": 20,
"processingTimeMs": 0,
"query": "phil"
}
§Custom Search With Filters
If you want to enable filtering, you must add your attributes to the filterableAttributes
index setting.
let filterable_attributes = [
"id",
"genres",
];
client.index("movies_4").set_filterable_attributes(&filterable_attributes).await.unwrap();
You only need to perform this operation once.
Note that Meilisearch will rebuild your index whenever you update filterableAttributes
. Depending on the size of your dataset, this might take time. You can track the process using the tasks.
Then, you can perform the search:
let search_result = client.index("movies_5")
.search()
.with_query("wonder")
.with_filter("id > 1 AND genres = Action")
.execute::<Movie>()
.await
.unwrap();
println!("{:?}", search_result.hits);
Json output:
{
"hits": [
{
"id": 2,
"title": "Wonder Woman",
"genres": ["Action", "Adventure"]
}
],
"offset": 0,
"limit": 20,
"estimatedTotalHits": 1,
"processingTimeMs": 0,
"query": "wonder"
}
§Customize the HttpClient
By default, the SDK uses reqwest
to make http calls.
The SDK lets you customize the http client by implementing the HttpClient
trait yourself and
initializing the Client
with the new_with_client
method.
You may be interested by the futures-unsend
feature which lets you specify a non-Send http client.
§Wasm support
The SDK supports wasm through reqwest. You’ll need to enable the futures-unsend
feature while importing it, though.
Modules§
- client
- Module containing the
Client
struct. - documents
- Module representing the documents structures.
- dumps
- Module containing the dumps trait.
The
dumps
module allows the creation of database dumps. - errors
- Module containing the
errors::Error
struct. - features
- Module related to runtime and instance features.
- indexes
- Module containing the Index struct.
- key
- Module containing the
Key
struct. - request
- reqwest
- search
- Module related to search queries and results.
- settings
- Module containing
Settings
. - snapshots
- Module containing the snapshots-feature.
The
snapshots
module allows the creation of database snapshots. - task_
info - Module representing the
TaskInfo
s. - tasks
- Module representing the
Task
s.