c# - 索引、IndexMany、IndexAsnyc、IndexManyAsync 与 NEST

标签 c# elasticsearch nest

我尝试使用 nest for ElasticSearch 来理解索引选项,我执行了每个选项,这是我的结果:

    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node, defaultIndex: "mydatabase"); 
    settings.SetTimeout(1800000);             
    var client = new ElasticClient(settings);  
    var createIndexResult = client.CreateIndex("mydatabase");  
    var mapResult = client.Map<Product>(c => c.MapFromAttributes().SourceField(s=>s.Enabled(true));

1) Index:当我通过遍历每个对象来使用 Index 选项时,虽然速度很慢,但它运行流畅。

foreach (var item in Items)
{
  elasticClient.Index(item);
}

2) IndexAsync:这毫无异常(exception)地工作,但它并不比 snyc 迭代快,而且更少的文档被编入索引。

 foreach (var item in Items)
    {
      elasticClient.IndexAsync(item);
    }

3) IndexMany: 我尝试过,elasticClient.IndexMany(items); 当然没有 foreach,它运行得比执行 foreach -index 选项更快,但不知何故当我有很多数据(在我的例子中是 500.000 个对象)它抛出异常,说

"System.Net.WebException: The underlying connection was closed: A connection that its continuation was expected, has been closed by the server ..     at System.Net.HttpWebRequest.GetResponse ()"

当我检查日志文件时,我只能看到

"2016-01-14

10:21:49,567][WARN ][http.netty ] [Microchip] Caught exception while handling client http traffic, closing connection [id: 0x68398975, /0:0:0:0:0:0:0:1:57860 => /0:0:0:0:0:0:0:1:9200]"

4)IndexManyAsync: elasticClient.IndexManyAsync(Items); 尝试使用 indexasnyc 会抛出与 snyc 类似的异常,但我可以在日志文件中看到更多信息。

[2016-01-14 11:00:16,086][WARN ][http.netty ] [Microchip] Caught exception while handling client http traffic, closing connection [id: 0x43bca172, /0:0:0:0:0:0:0:1:59314 => /0:0:0:0:0:0:0:1:9200] org.elasticsearch.common.netty.handler.codec.frame.TooLongFrameException: HTTP content length exceeded 104857600 bytes.

我的问题是确切的区别是什么?在哪些情况下我们可能需要异步?为什么 indexmany 和 indexmanyasnyc 选项都抛出这样的异常? 看起来 index 选项是最安全的。这样使用它可以吗?

最佳答案

使用 syncasync 不会对 Elasticsearch 索引性能产生任何影响。如果您不想在完成索引时阻止您的客户端代码,那么您会想要使用 async,仅此而已。

关于 IndexIndexMany,始终建议使用后者以利用批处理并避免客户端和 Elasticsearch 之间的请求/响应周期过多。也就是说,您不能简单地在单个请求中索引如此大量的文档。异常消息非常清楚地表明您的批处理索引请求已超过 100MB 的 HTTP 内容长度限制。您需要做的是使用 IndexMany 减少要索引的文档数量,这样您就不会达到此限制,然后多次调用 IndexMany 直到完成所有索引500,000 个文档。

关于c# - 索引、IndexMany、IndexAsnyc、IndexManyAsync 与 NEST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34787350/

相关文章:

c# - 如何在 WPF 中动态填充 treeview

c# - 使用 Entity Framework 在wpf中排序

mysql - 我如何使用 logstash 从 MySQL 动态更新我的数据(不重复)?

c# - Asp.net 5 web api返回状态代码和正文

c# - Singleton:第一次如何实例化Singleton?

c# - 弹性嵌套将元添加到聚合

search - 设置Solr或ElasticSearch需要什么技能?

elasticsearch - NEST 中的地理距离范围过滤器?

JsonReaderException 为elasticsearch创建批量索引

c# - NEST 2.3.1 (Elastic Search) 创建索引出错