Search code examples
elasticsearchnestelasticsearch-net

ElasticSearch Bulk Indexing


I am using version 2.1 and the 2.0 (alpha) client with the Nest C# client... I am attempting to bulk insert some records, however, the server is returning an error. I am utilizing the client on a windows client speaking to a Linux server (I don't believe this should matter however).

    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)}");
    }

Update - Error Info

The error I'm getting is in the response returned from the Bulk() method... the two properties on the BulkResponse returned are:

OriginalException: "The remote server returned an error: (400) Bad Request"

ServerError.Error: "Validation Failed: 1: no requests added"


Solution

  • You've made a simple mistake - you're using the low level ElasticsearchClient from Elasticsearch.Net to make the request, but sending it a strongly typed bulk request from NEST. To rectify is simple enough, you just need to use the ElasticClient from NEST

    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)}");
    }
    

    ElasticClient from NEST is the high level client and uses ElasticsearchClient from Elasticsearch.Net under the covers.