0

我在 Windows 上的 ASP.NET API (.NET 4.6) 上使用 Elasticsearch.NET (5.6),并尝试发布到 AWS 上托管的 elasticsearch(我尝试了 5.1.1 和 6,两者的行为相同)。

我有以下代码将文档批量索引到 Elasticsearch。图片多次调用以下代码块:

        var node = new System.Uri(restEndPoint);
        var settings = new ConnectionSettings(node);
        var lowlevelClient = new ElasticLowLevelClient(settings);

        var index = indexStart + indexSuffix;

        var items = new List<object>(list.Count() * 2);
        foreach (var conn in list)
        {
            items.Add(new { index = new { _index = index, _type = "doc", _id = getId(conn) } });
            items.Add(conn);
        }

        try
        {
            var indexResponse = lowlevelClient.Bulk<Stream>(items);
            if (indexResponse.HttpStatusCode != 200)
            {
                throw new Exception(indexResponse.DebugInformation);
            }

            return indexResponse.HttpStatusCode;
        }
        catch (Exception ex)
        {
            ExceptionManager.LogException(ex, "Cannot publish to ES");
            return null;
        }

它运行良好,可以将文档发布到 Elasticsearch,但它只能运行 80 次,80 次之后,它总是会出现异常:

# OriginalException: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at Elasticsearch.Net.HttpConnection.Request[TReturn](RequestData requestData) in C:\Users\russ\source\elasticsearch-net-5.x\src\Elasticsearch.Net\Connection\HttpConnection.cs:line 148

最有趣的部分是:我尝试将批量大小更改为 200 或 30,结果是 16000 和 2400,这意味着两者最终都是 80 倍。(每个文档大小都非常相似)

有任何想法吗?谢谢

4

1 回答 1

0

存在连接限制(另请参阅问题下@RussCam 的评论)。所以真正的问题是Stream在响应中保持连接。

所以解决方法是indexResponse.Body.Dispose(我没有尝试过这个)或使用VoidResponsereportClient.BulkAsync<VoidResponse>(items);不需要响应流。我已经尝试了第二个并且它有效。

于 2018-01-15T23:50:59.550 回答