@@ -86,46 +86,47 @@ use serde::{Serialize, Deserialize};
8686use futures :: executor :: block_on;
8787
8888#[derive(Serialize , Deserialize , Debug )]
89- struct Book {
90- book_id : usize ,
89+ struct Movie {
90+ id : usize ,
9191 title : String ,
92+ genres : Vec <String >,
9293}
9394
9495// That trait is required to make a struct usable by an index
95- impl Document for Book {
96+ impl Document for Movie {
9697 type UIDType = usize ;
9798
9899 fn get_uid (& self ) -> & Self :: UIDType {
99- & self . book_id
100+ & self . id
100101 }
101102}
102103
103104fn main () { block_on (async move {
104105 // Create a client (without sending any request so that can't fail)
105106 let client = Client :: new (" http://localhost:7700" , " masterKey" );
106107
107- // Get the index called "books "
108- let books = client . get_or_create (" books " ). await . unwrap ();
109-
110- // Add some books in the index
111- books . add_documents (& [
112- Book { book_id : 123 , title : String :: from (" Pride and Prejudice " ) },
113- Book { book_id : 456 , title : String :: from (" Le Petit Prince " ) },
114- Book { book_id : 1 , title : String :: from (" Alice In Wonderland " ) },
115- Book { book_id : 1344 , title : String :: from (" The Hobbit " ) },
116- Book { book_id : 4 , title : String :: from (" Harry Potter and the Half-Blood Prince " ) },
117- Book { book_id : 42 , title : String :: from (" The Hitchhiker's Guide to the Galaxy " ) },
118- ], Some (" book_id " )). await . unwrap ();
119-
120- // Query books (note that there is a typo)
121- println! (" {:?}" , books . search (). with_query (" harry pottre " ). execute :: <Book >(). await . unwrap (). hits);
108+ // Get the index called "movies "
109+ let movies = client . get_or_create (" movies " ). await . unwrap ();
110+
111+ // Add some movies in the index
112+ movies . add_documents (& [
113+ Movie { id : 1 , title : String :: from (" Carol " ), genres : vec! [ " Romance " , " Drama " ] },
114+ Movie { id : 2 , title : String :: from (" Wonder Woman " ), genres : vec! [ " Action " , " Adventure " ] },
115+ Movie { id : 3 , title : String :: from (" Life of Pi " ), genres : vec! [ " Adventure " , " Drama " ] },
116+ Movie { id : 4 , title : String :: from (" Mad Max " ), genres : vec! [ " Adventure " , " Science Fiction " ] },
117+ Movie { id : 5 , title : String :: from (" Moana " ), genres : vec! [ " Fantasy " , " Action " ] },
118+ Movie { id : 6 , title : String :: from (" Philadelphia " ), genres : vec! [ " Drama " ] },
119+ ], Some (" id " )). await . unwrap ();
120+
121+ // Query movies (note that there is a typo)
122+ println! (" {:?}" , movies . search (). with_query (" Carol " ). execute :: <Movie >(). await . unwrap (). hits);
122123})}
123124```
124125
125126Output:
126127
127128```
128- [Book { book_id: 4 , title: "Harry Potter and the Half-Blood Prince" }]
129+ [Movie{id: 1 , title: String::from("Carol"), genres: vec!["Romance", "Drama"] }]
129130```
130131
131132### 🌐 Running in the Browser with WASM <!-- omit in TOC -->
0 commit comments