(ES 8.6.2,W10)
在 Insomnia 中,当我尝试使用命令 DELETE 和 url https://localhost:9500/my_test_index 删除不存在的索引时,我似乎总是得到这样的 JSON 对象:
{
"error": {
"root_cause": [
...
"status": 404
}
类似地,当我在 Python 中使用
requests
执行上述操作时,我得到了上述响应。
但是当我运行
reqwest
请求(在 Rust 中)执行相同的请求时,我有时会得到上面的结果,但经常得到这个:
{
"acknowledged": true
}
我这样做的原因是删除索引(如果存在)。因此,200 或 404 都是可接受的状态代码。但如果我得到“确认”,无论是真是假,我不知道这意味着什么:即使索引不存在,它也是
true
。
这一切都发生在一台本地机器上,只有一个分片。目前我的分片中没有其他索引。当我创建该索引时,它的状态为黄色。所以我的服务器/分片似乎足够健康。
任何人都可以建议为什么会发生这种情况吗?在这种情况下,“已确认”是否意味着服务器正忙(在上一个命令中)? Rust 的
reqwest
代码有可能比 Python 运行得更快吗?
编辑
我的 Rust 代码发生在实用程序函数内,该函数使用
duang
箱来允许可选/命名参数:
duang!(
pub fn reqwest_call<T: serde::de::DeserializeOwned + std::fmt::Debug>(url: &str, method: reqwest::Method = reqwest::Method::GET, timeout: u64 = 5_000,
acceptable_status_codes: Vec<u16> = vec![200], content_type: &str = "application/json", body_str: &str = "")
-> Result<T, Box<dyn std::error::Error>> {
let mut error_details = format!(
"=> url was |{url}|\n=> method was {method}\n=> content type was {content_type}\n=> timeout was {timeout}\n=> body\n|");
if body_str.len() <= 200 {
error_details.push_str(body_str);
}
else {
error_details.push_str(&(body_str.to_string()[0..200]));
error_details.push_str("...");
};
error_details.push_str("|\n\n");
let response_result = get_reqwest_client(timeout)?
.request(method, url)
.header("Content-type", content_type)
.body(body_str.to_string())
.basic_auth("mike12", Some("mike12"))
.send();
let response = match response_result {
Ok(response) => response,
Err(e) => {
error!("{e}\n{error_details}\n{}", Backtrace::force_capture());
return Err(Box::new(e))
}
};
...
...这又调用另一个实用程序
get_reqwest_client
,它会提供具有指定超时的 reqwest.blocking.Client
。当前超时为 5 秒(请参阅上面的默认参数)。没有发生超时错误。如果是,send
会将我们发送到上面的 Err
分支,并且不会检查返回的 JSON 对象。
在大多数情况下,您将得到 404 或 200 以及
"acknowledged": true
。当找到索引并发起删除操作时,您会得到"acknowledged": false
,但由于主节点繁忙,无法在指定的超时时间内处理请求,所以时间太长。此行为不是特定于客户端的,并且 python 和 rust 响应之间应该没有区别,除非您在 request
调用中执行不同的操作。如果您可以显示您的代码,我也许可以给您更好的解释。