Closed GopherJ closed 4 years ago
extern crate actix_web;
extern crate serde_json;
use std::io;
use std::str::FromStr;
use actix_web::{middleware, web, error, Error, App, HttpResponse, HttpServer};
use futures::{Future, Stream};
use elastic::prelude::*;
use serde_json::{Value, json};
use tokio_core::reactor::Core;
fn search(
(payload, path): (web::Payload, web::Path<(String, String)>),
) -> impl Future<Item = HttpResponse, Error = Error> {
let mut core = Core::new().unwrap();
payload.concat2().map_err(Error::from).and_then(move |body| {
let query = serde_json::Value::from_str(std::str::from_utf8(&body)?)?;
println!("{}", init::ELASTIC_URL.as_str());
let client = AsyncClientBuilder::new()
.base_url(init::ELASTIC_URL.as_str())
.build(&core.handle())
.map_err(error::ErrorInternalServerError)?;
Ok(client.search::<Value>()
//.index(format!("rt_events.{}", path.0))
.index("_all")
.ty(Some(format!("{}", path.1)))
.body(query)
.send())
})
.and_then(|sf| {
sf .and_then(|res| { Ok(HttpResponse::Ok().body("slksdjls"))
})
.map_err(|err| {
println!("{:?}", err);
error::ErrorInternalServerError(err)
})
})
.map_err(error::ErrorInternalServerError)
}
mod init;
fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
println!("Starting http server: {}", init::LISTEN_ADDR.as_str());
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.data(web::JsonConfig::default().limit(4096))
.service(
web::resource("/u_applications/log_search/rt_events/{namespace}/{doctype}.json")
.route(web::post().to_async(search)),
)
})
.bind(init::LISTEN_ADDR.as_str())?
.run()
}
when I use 0.21.0-pre.1
I got this error:
Client(ClientError { inner: Error(Response(200), State { next_error: Some(Parse(ParseError { inner: Error("invalid type: integer `189233`, expected struct HitsTotal", line: 1, column: 111) })), backtrace: None }) })
So unfortunately, even I already spent one day and a half, I cannot find a way to make this client working with elasticsearch@5.6.16
and rust nightly/stable
It only works with elasticsearch@7
, but clearly there are still many companies who use elasticsearch@5
......
can someone help me on this?
Sorry for the late reply.
0.20.10
should target 5.x
.
Canceled { cause: Some("connection was not ready") }
Looks like you just needed to wait a moment (sometimes takes a while) for Elasticsearch to be ready.
@GopherJ Is it ok if I close this?
@mwilliammyers magically it starts to work after switching from ubuntu to deepin.........
Hello, I tried
0.20.10
for elasticsearch@5.6.x, but it seems I cannot compile the examples? So which verison shall I use for elasticsearch 5.6.x?I got this error while I just would like to do a simple search: