v0.12-searchP2
This commit is contained in:
@@ -1,30 +1,59 @@
|
||||
use meilisearch_sdk::{client::*, search::*};
|
||||
use meilisearch_sdk::client::*;
|
||||
use meilisearch_sdk::search::*;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Movie {
|
||||
id: usize,
|
||||
title: String,
|
||||
genres: Vec<String>,
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
pub struct BookMeili {
|
||||
pub id: i32,
|
||||
pub open_library_key: String,
|
||||
pub title: String,
|
||||
pub edition_count: i32,
|
||||
pub first_publish_year: i32,
|
||||
pub median_page_count: i32,
|
||||
pub goodread_id: String,
|
||||
pub description: String,
|
||||
pub cover: String,
|
||||
pub location: String,
|
||||
pub time_added: String,
|
||||
pub rating: i32,
|
||||
pub comments: String,
|
||||
pub author_name: Vec<String>,
|
||||
pub person: Vec<String>,
|
||||
pub place: Vec<String>,
|
||||
pub subject: Vec<String>,
|
||||
pub time: Vec<String>,
|
||||
pub isbn: Vec<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Create a client (without sending any request so that can't fail)
|
||||
let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
|
||||
|
||||
pub async fn create_or_update_book(book: BookMeili, client: &Client) {
|
||||
// An index is where the documents are stored.
|
||||
let movies = client.index("movies");
|
||||
|
||||
let books = client.index("books");
|
||||
// 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()] },
|
||||
books.add_or_replace(&[
|
||||
book
|
||||
], Some("id")).await.unwrap();
|
||||
|
||||
}
|
||||
|
||||
|
||||
pub async fn delete_book(bookid: i32, client: &Client) {
|
||||
// An index is where the documents are stored.
|
||||
let books = client.index("books");
|
||||
books.delete_document(bookid).await.unwrap();
|
||||
}
|
||||
|
||||
|
||||
pub async fn search_book(search: &str, page: usize, client: &Client) -> Result<Vec<BookMeili>, meilisearch_sdk::errors::Error> {
|
||||
// An index is where the documents are stored.
|
||||
let books = client.index("books");
|
||||
let results : SearchResults<BookMeili> = books.search().with_query(search).with_offset((page-1)*24)
|
||||
.execute::<BookMeili>().await.unwrap();
|
||||
|
||||
let formatted_results : Vec<BookMeili> = (results.hits).iter().map(|r| r.result.clone()).collect();
|
||||
//.iter()s.map(|r| r.formatted_result.unwrap()).collect();
|
||||
return Ok(formatted_results);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user