how to add document when using meilisearch

160 views Asked by At

I am using meilisearch(v1.4.0) as the full text search engine, add the document with rust client code like this:

use meilisearch_sdk::TaskInfo;
use rust_wheel::common::util::rd_file_util::join_paths;
use serde::{Deserialize, Serialize};

#[tokio::main]
async fn main() {
    let url = "http://meilisearch.reddwarf-toolbox.svc.cluster.local:7700";
    // https://www.meilisearch.com/docs/reference/api/documents
    let full_url = join_paths(&[url, "/indexes/files/documents".to_string().as_str()]);
    let api_key = "9a4jH1W8BcK27ubILgOfF0psNBg-cXa_klzNZSyOHWs";
    let client = meilisearch_sdk::Client::new(full_url, Some(api_key));
    let mut d = Vec::new();
    d.push("value".to_string());
    let task = client.index("movies").add_or_replace(&[
    Movie {
        id:287947,title:"Shazam".to_string(),
        poster:"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg".to_string(),
        overview:"A boy is given the ability to become an adult superhero in times of need with a single magic word.".to_string(),release_date: 123, 
        genres: d
    }
  ], Some("287947"))
  .await;
    match task {
        Err(e) => {
            print!("error: {}", e)
        }
        Ok(_) => {}
    }
}

#[derive(Serialize, Deserialize)]
pub struct Movie {
    id: i64,
    title: String,
    poster: String,
    overview: String,
    release_date: i64,
    genres: Vec<String>,
}

and this is the Cargo.toml:

[package]
name = "rust-learn"
version = "0.1.0"
edition = "2018"

[dependencies]
tokio = { version = "1.17.0", features = ["full"] }
actix-web = "4"
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
log4rs = "1.2.0"
meilisearch-sdk = "0.24.2"
rust_wheel = { git = "https://github.com/jiangxiaoqiang/rust_wheel.git", branch = "diesel2.0", features = [
    "model",
    "common",
    "rwconfig",
    "texhub"
] }

the debbuing process shows error: error: HTTP request failed: invalid HTTP request. I am follow the document from https://www.meilisearch.com/docs/reference/api/documents. Am I missing something? what should I do to fixed this issue? Also tried to remove the path and tweak code like this:

use rust_wheel::common::util::rd_file_util::join_paths;
use serde::{Deserialize, Serialize};

#[tokio::main]
async fn main() {
    let url = "http://meilisearch.reddwarf-toolbox.svc.cluster.local:7700";
    let api_key = "9a4jH1W8BcK27ubILgOfF0psNBg-cXa_klzNZSyOHWs";
    let client = meilisearch_sdk::Client::new(url, Some(api_key));
    let mut d = Vec::new();
    d.push("value".to_string());
    let task = client.index("files").add_documents(&[
    Movie {
        id:287947,
        title:"Shazam".to_string(),
        poster:"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg".to_string(),
        overview:"A boy is given the ability to become an adult superhero in times of need with a single magic word.".to_string(),release_date: 123, 
        genres: d
    }
  ], Some("287947"))
  .await;
    match task {
        Err(e) => {
            print!("error: {}", e)
        }
        Ok(suc) => {
            print!("success: {:?}", suc)
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct Movie {
    id: i64,
    title: String,
    poster: String,
    overview: String,
    release_date: i64,
    genres: Vec<String>,
}

                                                                                                                

the log shows success but the meilisearch UI could not found the added document, this is the log:

success: TaskInfo { enqueued_at: 2023-11-02 6:37:35.118942639 +00:00:00, index_uid: Some("files"), status: "enqueued", update_type: DocumentAdditionOrUpdate { details: None }, task_uid: 8 }

and this is the UI:

enter image description here

0

There are 0 answers