发出 HTTP POST 请求时捕获 ElasticSearch 异常

MY_G

我使用 ElasticSearch 7.2.0我使用System.Net.Http.HttpClient该类POST向我的 ElasticSearch 服务器发出请求:

private const string Host = "http://127.0.0.1";
private const int Port = 9200;

public static async Task<HttpResponseMessage> Receive(string jsonPostContents)
{
    try
    {
        return await this.httpClient.PostAsync($"{Host}:{Port}/_bulk?pipeline=ParseDuration", 
                                               new StringContent(jsonPostContents, Encoding.UTF8, mediaType: MimeTypes.Json));
    }

    catch (Exception exception)
    {
        StringContent responseString = new StringContent($"Exception encountered: {exception.Message}.\n" +
                                                         $"Inner exception: {exception.InnerException?.Message}\n" +
                                                         $"Stack trace: {exception.StackTrace}");

        return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = responseString };
    }
}

cmd.exe我运行 ElasticSearch窗口中,打印了以下错误消息:

[2019-09-17T11:45:14,569][DEBUG][o.e.a.b.TransportBulkAction] [MyServer] failed to execute pipeline [ParseDuration] for document [testindex/_doc/5c91f6e0-b72f-48b2-8430-ad76a640347a]
java.lang.IllegalArgumentException: pipeline with id [ParseDuration] does not exist
        at org.elasticsearch.ingest.IngestService$4.doRun(IngestService.java:408) [elasticsearch-7.2.0.jar:7.2.0]
        at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:758) [elasticsearch-7.2.0.jar:7.2.0]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-7.2.0.jar:7.2.0]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
        at java.lang.Thread.run(Thread.java:835) [?:?]

但是,该Receive(string jsonPostContents)方法不会返回 HTTP 错误响应,OK而是返回响应(带有 HTTP 状态代码200)。

如何确保可靠地捕获所有 ElasticSearch 异常?

巴努杰

仅当存在连接问题时,批量 API 才返回错误,否则返回 200。

您应该检查批量响应。如果批量出现单个错误,则响应将如下所示:

{
   "took": x,
   "errors": true,
   "items":[
   //element i: shows what happened with the item i from bulk
   ]
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章