Crate meilisearch_sdk[−][src]
Expand description
๐ Getting Started
Add Documents
use meilisearch_sdk::{document::*, client::*};
use serde::{Serialize, Deserialize};
use futures::executor::block_on;
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
id: usize,
title: String,
genres: Vec<String>,
}
// That trait is required to make a struct usable by an index
impl Document for Movie {
type UIDType = usize;
fn get_uid(&self) -> &Self::UIDType {
&self.id
}
}
fn main() { block_on(async move {
// Create a client (without sending any request so that can't fail)
let client = Client::new("http://localhost:7700", "masterKey");
// 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();
})}Basic Search
// MeiliSearch is typo-tolerant:
println!("{:?}", client.index("movies").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
println!("{:?}", client.index("movies").search().with_query("phil").with_attributes_to_highlight(Selectors::Some(&["*"])).execute::<Movie>().await.unwrap().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").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 whole process
using the update
status.
Then, you can perform the search:
println!("{:?}", client.index("movies").search().with_query("wonder").with_filter("id > 1 AND genres = Action")
.execute::<Movie>().await.unwrap().hits);Json output:
{
"hits": [
{
"id": 2,
"title": "Wonder Woman",
"genres": ["Action", "Adventure"]
}
],
"offset": 0,
"limit": 20,
"nbHits": 1,
"processingTimeMs": 0,
"query": "wonder"
}Modules
Module containing the Client struct.
Module containing the Document trait.
The dumps module allows the creation of database dumps.
Dumps are .dump files that can be used to launch MeiliSearch.
Dumps are compatible between MeiliSearch versions.
Module containing the Error struct.
Module containing the Index struct.
Module containing objects useful for tracking the progress of async operations.
Module related to search queries and results.
Module containing settings
