|
41 | 41 | //! use futures::executor::block_on; |
42 | 42 | //! |
43 | 43 | //! #[derive(Serialize, Deserialize, Debug)] |
44 | | -//! struct Book { |
45 | | -//! book_id: usize, |
| 44 | +//! struct Movie { |
| 45 | +//! id: usize, |
46 | 46 | //! title: String, |
| 47 | +//! genres: Vec<String>, |
47 | 48 | //! } |
48 | 49 | //! |
49 | 50 | //! // That trait is required to make a struct usable by an index |
50 | | -//! impl Document for Book { |
| 51 | +//! impl Document for Movie { |
51 | 52 | //! type UIDType = usize; |
52 | 53 | //! |
53 | 54 | //! fn get_uid(&self) -> &Self::UIDType { |
54 | | -//! &self.book_id |
| 55 | +//! &self.id |
55 | 56 | //! } |
56 | 57 | //! } |
57 | 58 | //! |
58 | 59 | //! fn main() { block_on(async move { |
59 | 60 | //! // Create a client (without sending any request so that can't fail) |
60 | 61 | //! let client = Client::new("http://localhost:7700", "masterKey"); |
61 | 62 | //! |
62 | | -//! // Get the index called "books" |
63 | | -//! let books = client.get_or_create("books").await.unwrap(); |
64 | | -//! |
65 | | -//! // Add some books in the index |
66 | | -//! books.add_documents(&[ |
67 | | -//! Book{book_id: 123, title: String::from("Pride and Prejudice")}, |
68 | | -//! Book{book_id: 456, title: String::from("Le Petit Prince")}, |
69 | | -//! Book{book_id: 1, title: String::from("Alice In Wonderland")}, |
70 | | -//! Book{book_id: 1344, title: String::from("The Hobbit")}, |
71 | | -//! Book{book_id: 4, title: String::from("Harry Potter and the Half-Blood Prince")}, |
72 | | -//! Book{book_id: 42, title: String::from("The Hitchhiker's Guide to the Galaxy")}, |
73 | | -//! ], Some("book_id")).await.unwrap(); |
74 | | -//! |
75 | | -//! // Query books (note that there is a typo) |
76 | | -//! println!("{:?}", books.search().with_query("harry pottre").execute::<Book>().await.unwrap().hits); |
| 63 | +//! // Get the index called "movies" |
| 64 | +//! let movies = client.get_or_create("movies").await.unwrap(); |
| 65 | +//! |
| 66 | +//! // Add some movies in the index |
| 67 | +//! movies.add_documents(&[ |
| 68 | +//! Movie{id: 1, title: String::from("Carol"), genres: vec!["Romance".to_string(), "Drama".to_string()]}, |
| 69 | +//! Movie{id: 2, title: String::from("Wonder Woman"), genres: vec!["Action".to_string(), "Adventure".to_string()]}, |
| 70 | +//! Movie{id: 3, title: String::from("Life of Pi"), genres: vec!["Adventure".to_string(), "Drama".to_string()]}, |
| 71 | +//! Movie{id: 4, title: String::from("Mad Max"), genres: vec!["Adventure".to_string(), "Science Fiction".to_string()]}, |
| 72 | +//! Movie{id: 5, title: String::from("Moana"), genres: vec!["Fantasy".to_string(), "Action".to_string()]}, |
| 73 | +//! Movie{id: 6, title: String::from("Philadelphia"), genres: vec!["Drama".to_string()]}, |
| 74 | +//! ], Some("id")).await.unwrap(); |
| 75 | +//! |
| 76 | +//! // Query movies (note that there is a typo) |
| 77 | +//! println!("{:?}", movies.search().with_query("carol").execute::<Movie>().await.unwrap().hits); |
77 | 78 | //! })} |
78 | 79 | //! ``` |
79 | 80 | //! |
80 | 81 | //! Output: |
81 | 82 | //! |
82 | 83 | //! ```text |
83 | | -//! [Book { book_id: 4, title: "Harry Potter and the Half-Blood Prince" }] |
| 84 | +//! [Movie{id: 1, title: String::from("Carol"), genres: vec!["Romance", "Drama"]}] |
84 | 85 | //! ``` |
85 | 86 | //! |
86 | 87 | //! ## 🌐 Running in the Browser with WASM <!-- omit in TOC --> |
|
0 commit comments