【问题标题】:Elasticsearch .NET only allows me to bulk upload 80 timesElasticsearch .NET 只允许我批量上传 80 次
【发布时间】:2018-01-16 07:50:59
【问题描述】:

我在 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 倍结束。 (每个文档大小都非常相似)

有什么想法吗?谢谢

【问题讨论】:

  • 您尝试发送的一个请求有多大,即列表中有多少项,一个请求中的总字节数是多少?
  • 另外,您使用的是哪个版本的 NEST?您的目标是什么版本的 Elasticsearch?什么操作系统和 .NET 框架版本?
  • @RussCam 感谢您的提问,我刚刚更新了我的问题。
  • 你知道为什么 lib 在 80 次批量操作后就开始超时了吗?
  • 我怀疑从每个调用返回的 Stream 没有被处理掉,并且您正在达到打开并发连接数的默认连接限制(80 个):github.com/elastic/elasticsearch-net/blob/master/src/…

标签: elasticsearch nest elasticsearch.net


【解决方案1】:

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

所以修复方法是indexResponse.Body.Dispose(我没有尝试过这个)或使用VoidResponsereportClient.BulkAsync&lt;VoidResponse&gt;(items);,它不需要响应流。我已经尝试了第二个,它可以工作。

【讨论】:

  • 这里可以使用VoidResponse,但是实现还是缺少批量操作失败的处理。您使用低级别客户端而不是使用高级客户端 NEST 是否有原因?
  • 唯一的原因是因为我只在低级别的索引中找到了批量索引实现
  • 高级客户端具有批量 API。看看测试:github.com/elastic/elasticsearch-net/blob/5.x/src/Tests/… 和一个例子:stackoverflow.com/a/45300910/1831
  • 谢谢。我试过高级客户端,描述符很好用,没有80问题。
猜你喜欢
  • 2023-03-12
  • 2012-03-06
  • 1970-01-01
  • 2017-03-20
  • 2013-01-29
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 2018-06-30
相关资源
最近更新 更多