|
88 | 88 | //! [Movie{id: 1, title: String::from("Carol"), genres: vec!["Romance", "Drama"]}] |
89 | 89 | //! ``` |
90 | 90 | //! |
91 | | -//! # 🌐 Running in the Browser with WASM <!-- omit in TOC --> |
| 91 | +//! #### Custom Search With Filters <!-- omit in TOC --> |
| 92 | +//! |
| 93 | +//! If you want to enable filtering, you must add your attributes to the `filterableAttributes` |
| 94 | +//! index setting. |
| 95 | +//! |
| 96 | +//! ``` |
| 97 | +//! # use meilisearch_sdk::{document::*, client::*, search::*}; |
| 98 | +//! # use serde::{Serialize, Deserialize}; |
| 99 | +//! # 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 | +//! # fn main() { block_on(async move { |
| 114 | +//! # // Create a client (without sending any request so that can't fail) |
| 115 | +//! # let client = Client::new("http://localhost:7700", "masterKey"); |
| 116 | +//! # // Get the index called "movies" |
| 117 | +//! # let movies = client.get_or_create("movies").await.unwrap(); |
| 118 | +//! let filterable_attributes = [ |
| 119 | +//! "id", |
| 120 | +//! "genres" |
| 121 | +//! ]; |
| 122 | +//! 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 | +//! # })} |
| 135 | +//! ``` |
| 136 | +//! |
| 137 | +//! You only need to perform this operation once. |
| 138 | +//! |
| 139 | +//! Note that MeiliSearch will rebuild your index whenever you update `filterableAttributes`. |
| 140 | +//! Depending on the size of your dataset, this might take time. You can track the whole process |
| 141 | +//! using the [update |
| 142 | +//! status](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status). |
| 143 | +//! |
| 144 | +//! Then, you can perform the search: |
| 145 | +//! |
| 146 | +//! ``` |
| 147 | +//! # use meilisearch_sdk::{document::*, client::*, search::*}; |
| 148 | +//! # use serde::{Serialize, Deserialize}; |
| 149 | +//! # use futures::executor::block_on; |
| 150 | +//! # #[derive(Serialize, Deserialize, Debug)] |
| 151 | +//! # struct Movie { |
| 152 | +//! # id: usize, |
| 153 | +//! # title: String, |
| 154 | +//! # genres: Vec<String>, |
| 155 | +//! # } |
| 156 | +//! # // That trait is required to make a struct usable by an index |
| 157 | +//! # impl Document for Movie { |
| 158 | +//! # type UIDType = usize; |
| 159 | +//! # fn get_uid(&self) -> &Self::UIDType { |
| 160 | +//! # &self.id |
| 161 | +//! # } |
| 162 | +//! # } |
| 163 | +//! # fn main() { block_on(async move { |
| 164 | +//! # // Create a client (without sending any request so that can't fail) |
| 165 | +//! # let client = Client::new("http://localhost:7700", "masterKey"); |
| 166 | +//! # // Get the index called "movies" |
| 167 | +//! # let movies = client.get_or_create("movies").await.unwrap(); |
| 168 | +//! # let filterable_attributes = [ |
| 169 | +//! # "id", |
| 170 | +//! # "genres" |
| 171 | +//! # ]; |
| 172 | +//! # 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(); |
| 182 | +//! println!("{:?}", movies.search().with_query("wonder").with_filter("id > 1 AND genres = Action") |
| 183 | +//! .execute::<Movie>().await.unwrap().hits); |
| 184 | +//! # })} |
| 185 | +//! ``` |
| 186 | +//! |
| 187 | +//! ```json |
| 188 | +//! { |
| 189 | +//! "hits": [ |
| 190 | +//! { |
| 191 | +//! "id": 2, |
| 192 | +//! "title": "Wonder Woman", |
| 193 | +//! "genres": ["Action", "Adventure"] |
| 194 | +//! } |
| 195 | +//! ], |
| 196 | +//! "offset": 0, |
| 197 | +//! "limit": 20, |
| 198 | +//! "nbHits": 1, |
| 199 | +//! "processingTimeMs": 0, |
| 200 | +//! "query": "wonder" |
| 201 | +//! } |
| 202 | +//! ``` |
| 203 | +//! |
| 204 | +//! ## 🌐 Running in the Browser with WASM <!-- omit in TOC --> |
92 | 205 | //! |
93 | 206 | //! This crate fully supports WASM. |
94 | 207 | //! |
|
0 commit comments