【问题标题】:Elasticsearch Java API access source when Exception is thrown抛出异常时的 Elasticsearch Java API 访问源
【发布时间】:2020-06-30 18:22:01
【问题描述】:

我正在学习 Java RestHighLevelClient,但我找不到这个问题的答案。

当您向未找到文档的内容提交 REST 请求时,您将看到如下内容:

$ curl localhost:9200/customer/_doc/1?pretty
{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index [customer]",
        "resource.type" : "index_expression",
        "resource.id" : "customer",
        "index_uuid" : "_na_",
        "index" : "customer"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [customer]",
    "resource.type" : "index_expression",
    "resource.id" : "customer",
    "index_uuid" : "_na_",
    "index" : "customer"
  },
  "status" : 404
}

但是在 Java 客户端中,您可以编写如下代码:

    GetRequest request = new GetRequest(INDEX, ROOT);
    GetResponse response = null;
    try {
        response = client.get(request, RequestOptions.DEFAULT);
    } catch (IOException ioe) {
         // do something with the IOException
    } catch (ElasticsearchException ese) {
         // where is the response source?
    }

因此,如果未找到文档,您将得到 ElasticsearchException,在这种情况下,局部变量 response 为空。那么你从哪里得到 低级别存在的源文档? (最好作为地图)。

【问题讨论】:

  • 源文档出现在底层是什么意思?
  • @AmitKhandelwal 查看curl 命令的输出。它是一个 JS 对象,包含您想要的有关错误的所有信息。我如何在我发布的 Java 代码中得到它?

标签: elasticsearch elasticsearch-java-api


【解决方案1】:

您可以通过ElasticsearchException 中的抑制异常来获取低级别响应的来源。

Throwable[] suppressed = ese.getSuppressed();
if (suppressed.length > 0 && suppressed[0] instanceof ResponseException) {
    ResponseException re = (ResponseException) suppressed[0];
    Response response = re.getResponse();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2017-09-30
    • 2013-05-24
    • 2021-07-12
    相关资源
    最近更新 更多