elasticsearch JSON index命令返回不同的JSON对象

ubbxdtey  于 7个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(95)

(ES 8.6.2,W10)
在Increase中,当我尝试使用命令Increase和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运行得更快?

编辑

我似乎发现了一个模式:我运行调用方Python程序,它调用Rust模块,我得到了这个无法解释的结果,即response.text()是“{“acknowledged”:true}"。然后我再次运行它,得到一个预期的响应,带有“error”和“status”键。然后我再次运行,得到一个无法解释的结果,等等。也就是说,在这两个之间似乎有一个交替。

编辑2

如果我得到这个“{“acknowledged”:true }”response.text(),然后重复该命令(无论是否在等待1 ms后),似乎我总是在第二次尝试时得到预期的响应类型(带“status”键)。
有趣的是,这似乎只发生在使用Rust时,而不是Python时。我想知道这是什么解释。阅读我看到一些关于“节点”的东西,可能需要时间以你期望的方式响应。很奇怪:想知道Maven是否能建议可能发生的事情。

Rust代码

根据要求。我的Rust代码发生在一个实用程序函数中,它使用duang crate来允许可选/命名参数:

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对象。
Rust对我的实用方法的实际调用:

let url = format!("{ES_URL}/{}", &op_handler.index_name);
    #[derive(Deserialize, Debug)]
    struct DeleteResponse {
        status: usize
    }
    let acceptable_status_codes: Vec<u16> = vec![200, 404];
    let _: DeleteResponse = match reqwest_call!(&url, reqwest::Method::DELETE,
        acceptable_status_codes=acceptable_status_codes) {
        Ok(response) => {
            ...
        Err(e) => {
            ... 
            /* this part will reveal the message of error "e": namely 
that the JSON does not have a key "status", which it must have: otherwise
it can't be interpreted (cast) as a `DeleteResponse` `struct`.*/

qyyhg6bp

qyyhg6bp1#

在大多数情况下,您将获得404或200与"acknowledged": true。您将获得"acknowledged": false时,索引被发现,并启动删除操作,但它需要太长的时间,因为主节点是忙碌忙,无法在指定的超时内处理请求。这种行为不是客户端-python和rust响应之间应该没有区别,除非你在request调用中做了一些不同的事情。
我试着重现你的情况,不幸的是你省略了代码中一些非常重要的部分,但是如果你在.send();之后放一个print语句,我会得到这样的结果:

Ok(
  Response { 
    url: Url { 
      scheme: "http", 
      cannot_be_a_base: false, 
      username: "", 
      password: None, 
      host: Some(Domain("localhost")),
      port: Some(9500), 
      path: "/test", 
      query: None, 
      fragment: None 
   }, 
   status: 404, 
   headers: {"x-elastic-product": "Elasticsearch", "content-type": "application/json", "content-length": "353"} 
  }
)

字符串
如果我在match之后放置另一个print语句,并查看response.bytes(),你会看到这样的内容:

{
    "error":
    {
        "root_cause":
        [
            {
                "type": "index_not_found_exception",
                "reason": "no such index [test]",
                "resource.type": "index_or_alias",
                "resource.id": "test",
                "index_uuid": "_na_",
                "index": "test"
            }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index [test]",
        "resource.type": "index_or_alias",
        "resource.id": "test",
        "index_uuid": "_na_",
        "index": "test"
    },
    "status": 404
}


所以如果我把reqwest的结尾替换成

Ok(serde_json::from_slice(response.bytes().unwrap().as_ref()).unwrap())


我在调用程序中返回Ok(DeleteResponse { status: 404 })。看起来一切都按预期工作。你得到了什么?

相关问题