|
1 | | -//! # 🔧 Installation |
2 | | -//! |
3 | | -//! To use `meilisearch-sdk`, add this to your `Cargo.toml`: |
4 | | -//! |
5 | | -//! ```toml |
6 | | -//! [dependencies] |
7 | | -//! meilisearch-sdk = "0.11.0" |
8 | | -//! ``` |
9 | | -//! |
10 | | -//! The following optional dependencies may also be useful: |
11 | | -//! |
12 | | -//! ```toml |
13 | | -//! futures = "0.3" # To be able to block on async functions if you are not using an async runtime |
14 | | -//! serde = { version = "1.0", features = ["derive"] } |
15 | | -//! ``` |
16 | | -//! |
17 | | -//! This crate is `async` but you can choose to use an async runtime like [tokio](https://crates.io/crates/tokio) or just [block on futures](https://docs.rs/futures/latest/futures/executor/fn.block_on.html). |
18 | | -//! You can enable the `sync` feature to make most structs `Sync`. It may be a bit slower. |
19 | | -//! |
20 | | -//! Using this crate is possible without [serde](https://crates.io/crates/serde), but a lot of features require serde. |
21 | | -//! |
22 | | -//! ## Run a MeiliSearch Instance <!-- omit in TOC --> |
23 | | -//! |
24 | | -//! This crate requires a MeiliSearch server to run. |
25 | | -//! |
26 | | -//! There are many easy ways to [download and run a MeiliSearch instance](https://docs.meilisearch.com/reference/features/installation.html#download-and-launch). |
27 | | -//! |
28 | | -//! For example,using the `curl` command in [your Terminal](https://itconnect.uw.edu/learn/workshops/online-tutorials/web-publishing/what-is-a-terminal/): |
29 | | -//! |
30 | | -//! ```bash |
31 | | -//! # Install MeiliSearch |
32 | | -//! curl -L https://install.meilisearch.com | sh |
33 | | -//! |
34 | | -//! # Launch MeiliSearch |
35 | | -//! ./meilisearch --master-key=masterKey |
36 | | -//! ``` |
37 | | -//! |
38 | | -//! NB: you can also download MeiliSearch from **Homebrew** or **APT**. |
39 | | -//! |
40 | 1 | //! # 🚀 Getting Started |
41 | 2 | //! |
42 | 3 | //! ```rust |
|
94 | 55 | //! index setting. |
95 | 56 | //! |
96 | 57 | //! ``` |
97 | | -//! # use meilisearch_sdk::{document::*, client::*, search::*}; |
| 58 | +//! # use meilisearch_sdk::{client::*}; |
98 | 59 | //! # use serde::{Serialize, Deserialize}; |
99 | 60 | //! # use futures::executor::block_on; |
100 | | -//! # #[derive(Serialize, Deserialize, Debug)] |
101 | | -//! # struct Movie { |
102 | | -//! # id: usize, |
103 | | -//! # title: String, |
104 | | -//! # genres: Vec<String>, |
105 | | -//! # } |
106 | | -//! # // That trait is required to make a struct usable by an index |
107 | | -//! # impl Document for Movie { |
108 | | -//! # type UIDType = usize; |
109 | | -//! # fn get_uid(&self) -> &Self::UIDType { |
110 | | -//! # &self.id |
111 | | -//! # } |
112 | | -//! # } |
113 | 61 | //! # fn main() { block_on(async move { |
114 | | -//! # // Create a client (without sending any request so that can't fail) |
115 | 62 | //! # let client = Client::new("http://localhost:7700", "masterKey"); |
116 | | -//! # // Get the index called "movies" |
117 | 63 | //! # let movies = client.get_or_create("movies").await.unwrap(); |
118 | 64 | //! let filterable_attributes = [ |
119 | 65 | //! "id", |
120 | 66 | //! "genres" |
121 | 67 | //! ]; |
122 | 68 | //! movies.set_filterable_attributes(&filterable_attributes).await.unwrap(); |
123 | | -//! # // Add some movies in the index |
124 | | -//! # movies.add_documents(&[ |
125 | | -//! # Movie{id: 1, title: String::from("Carol"), genres: vec!["Romance".to_string(), "Drama".to_string()]}, |
126 | | -//! # Movie{id: 2, title: String::from("Wonder Woman"), genres: vec!["Action".to_string(), "Adventure".to_string()]}, |
127 | | -//! # Movie{id: 3, title: String::from("Life of Pi"), genres: vec!["Adventure".to_string(), "Drama".to_string()]}, |
128 | | -//! # Movie{id: 4, title: String::from("Mad Max"), genres: vec!["Adventure".to_string(), "Science Fiction".to_string()]}, |
129 | | -//! # Movie{id: 5, title: String::from("Moana"), genres: vec!["Fantasy".to_string(), "Action".to_string()]}, |
130 | | -//! # Movie{id: 6, title: String::from("Philadelphia"), genres: vec!["Drama".to_string()]}, |
131 | | -//! # ], Some("id")).await.unwrap(); |
132 | | -//! # // Query movies (note that there is a typo) |
133 | | -//! # println!("{:?}", movies.search().with_query("carol").execute::<Movie>().await.unwrap().hits); |
134 | 69 | //! # })} |
135 | 70 | //! ``` |
136 | 71 | //! |
|
153 | 88 | //! # title: String, |
154 | 89 | //! # genres: Vec<String>, |
155 | 90 | //! # } |
156 | | -//! # // That trait is required to make a struct usable by an index |
157 | 91 | //! # impl Document for Movie { |
158 | 92 | //! # type UIDType = usize; |
159 | 93 | //! # fn get_uid(&self) -> &Self::UIDType { |
160 | 94 | //! # &self.id |
161 | 95 | //! # } |
162 | 96 | //! # } |
163 | 97 | //! # fn main() { block_on(async move { |
164 | | -//! # // Create a client (without sending any request so that can't fail) |
165 | 98 | //! # let client = Client::new("http://localhost:7700", "masterKey"); |
166 | | -//! # // Get the index called "movies" |
167 | 99 | //! # let movies = client.get_or_create("movies").await.unwrap(); |
168 | 100 | //! # let filterable_attributes = [ |
169 | 101 | //! # "id", |
170 | 102 | //! # "genres" |
171 | 103 | //! # ]; |
172 | 104 | //! # movies.set_filterable_attributes(&filterable_attributes).await.unwrap(); |
173 | | -//! # // Add some movies in the index |
174 | | -//! # movies.add_documents(&[ |
175 | | -//! # Movie{id: 1, title: String::from("Carol"), genres: vec!["Romance".to_string(), "Drama".to_string()]}, |
176 | | -//! # Movie{id: 2, title: String::from("Wonder Woman"), genres: vec!["Action".to_string(), "Adventure".to_string()]}, |
177 | | -//! # Movie{id: 3, title: String::from("Life of Pi"), genres: vec!["Adventure".to_string(), "Drama".to_string()]}, |
178 | | -//! # Movie{id: 4, title: String::from("Mad Max"), genres: vec!["Adventure".to_string(), "Science Fiction".to_string()]}, |
179 | | -//! # Movie{id: 5, title: String::from("Moana"), genres: vec!["Fantasy".to_string(), "Action".to_string()]}, |
180 | | -//! # Movie{id: 6, title: String::from("Philadelphia"), genres: vec!["Drama".to_string()]}, |
181 | | -//! # ], Some("id")).await.unwrap(); |
| 105 | +//! # movies.add_documents(&[ |
| 106 | +//! # Movie{id: 1, title: String::from("Carol"), genres: vec!["Romance".to_string(), "Drama".to_string()]}, |
| 107 | +//! # Movie{id: 2, title: String::from("Wonder Woman"), genres: vec!["Action".to_string(), "Adventure".to_string()]}, |
| 108 | +//! # Movie{id: 3, title: String::from("Life of Pi"), genres: vec!["Adventure".to_string(), "Drama".to_string()]}, |
| 109 | +//! # Movie{id: 4, title: String::from("Mad Max"), genres: vec!["Adventure".to_string(), "Science Fiction".to_string()]}, |
| 110 | +//! # Movie{id: 5, title: String::from("Moana"), genres: vec!["Fantasy".to_string(), "Action".to_string()]}, |
| 111 | +//! # Movie{id: 6, title: String::from("Philadelphia"), genres: vec!["Drama".to_string()]}, |
| 112 | +//! # ], Some("id")).await.unwrap(); |
182 | 113 | //! println!("{:?}", movies.search().with_query("wonder").with_filter("id > 1 AND genres = Action") |
183 | 114 | //! .execute::<Movie>().await.unwrap().hits); |
184 | 115 | //! # })} |
|
200 | 131 | //! "query": "wonder" |
201 | 132 | //! } |
202 | 133 | //! ``` |
203 | | -//! |
204 | | -//! ## 🌐 Running in the Browser with WASM <!-- omit in TOC --> |
205 | | -//! |
206 | | -//! This crate fully supports WASM. |
207 | | -//! |
208 | | -//! The only difference between the WASM and the native version is that the native version has one more variant (`Error::Http`) in the Error enum. That should not matter so much but we could add this variant in WASM too. |
209 | | -//! |
210 | | -//! However, making a program intended to run in a web browser requires a **very** different design than a CLI program. To see an example of a simple Rust web app using MeiliSearch, see the [our demo](./examples/web_app). |
211 | | -//! |
212 | | -//! WARNING: `meilisearch-sdk` will panic if no Window is available (ex: Web extension). |
213 | 134 |
|
214 | 135 | #![warn(clippy::all)] |
215 | 136 | #![allow(clippy::needless_doctest_main)] |
|
0 commit comments