【问题标题】:ElasticSearch Bulk IndexingElasticSearch 批量索引
【发布时间】:2016-04-16 15:36:36
【问题描述】:

我正在使用版本 2.1 和 2.0 (alpha) 客户端和 Nest C# 客户端...我正在尝试批量插入一些记录,但是,服务器返回错误。我正在使用与 Linux 服务器通信的 Windows 客户端上的客户端(但我认为这并不重要)。

    public static void AddQuestionsToElasticSearch()
    {
        var es = new ElasticsearchClient(new ConnectionConfiguration(
            new Uri("http://elasticserver:9200"))
        );

        var foobars = new FooBar().Parse();
        var descriptor = new BulkDescriptor();

        descriptor.CreateMany<FooBar>(foobars, (bd, q) => bd.Id(q.Id.ToString()).Index("foobars"));

        Console.WriteLine($"Inserting foobars into ES...");
        var sw = new Stopwatch();
        sw.Start();

        var result = es.Bulk<FooBar>(descriptor);

        sw.Stop();
        Console.WriteLine($"Finished inserting foobars {GetTimeTaken(sw.Elapsed)}");
    }

更新 - 错误信息

我得到的错误是在 Bulk() 方法返回的响应中......返回的 BulkResponse 的两个属性是:

OriginalException: "远程服务器返回错误:(400) Bad Request"

ServerError.Error:“验证失败:1:未添加请求”

【问题讨论】:

  • 更新问题有错误
  • 你确定new FooBar().Parse();实际上返回了任何物品吗?
  • 是的。它确实返回有效数据....简单地模拟数据源 new []{new FooBar(), new FooBar(), .... };

标签: elasticsearch nest elasticsearch-net


【解决方案1】:

您犯了一个简单的错误 - 您使用 Elasticsearch.Net 中的低级别 ElasticsearchClient 发出请求,但向它发送了来自 NEST 的强类型批量请求。纠正很简单,你只需要使用 NEST 中的ElasticClient

public static void AddQuestionsToElasticSearch()
{
    var es = new ElasticClient(new Uri("http://elasticserver:9200"));

    var foobars = new FooBar().Parse();
    var descriptor = new BulkDescriptor();

    descriptor.CreateMany<FooBar>(foobars, (bd, q) => bd.Id(q.Id.ToString()).Index("foobars"));

    Console.WriteLine($"Inserting foobars into ES...");
    var sw = new Stopwatch();
    sw.Start();

    var result = es.Bulk(descriptor);

    sw.Stop();
    Console.WriteLine($"Finished inserting foobars {GetTimeTaken(sw.Elapsed)}");
}

来自 NEST 的ElasticClient 是高级客户端,并在后台使用来自 Elasticsearch.Net 的ElasticsearchClient

【讨论】:

  • 谢谢@Russ - 现在会试试。我是 ES 的新手,所以请原谅我,但我还有一个小问题——我应该使用 CreateMany() 还是 IndexMany()?无法从文档中分辨出差异(可能错过了一些东西)。
  • CreateMany&lt;T&gt;() 将在索引中已存在具有相同类型和 Id 的文档的任何单独创建调用中失败,而 IndexMany&lt;T&gt; 将插入文档或使用相同的类型和Id(除非您指定了类型,否则它将从 T 推断)。从本质上讲,CreateMany&lt;T&gt; 在您想要使用乐观并发控制的情况下很有用。见elastic.co/guide/en/elasticsearch/reference/current/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
相关资源
最近更新 更多