How to use PostData to send multiple items?

Hi all!

I need to send an array of raw data to Elastic, for this I wanted to use the auxiliary PostData class, which I pass to the Bulk method, but an error occurs when this method is called. The question is, what data should be transferred to the PostData class in order for it to work correctly with the Bulk method?

    static async Task Main(string[] args)
    {
        var startLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        // I want to send this JSON array
        // [{
        //      "id": "e7ad6925-c77c-48ac-ab42-89a8e8a99453",
        //      "eventid": "fd1bd252-7efb-4396-aea5-5ec804d80262",
        //      "accountid": "667fa4a4-b3f0-4391-b740-a26db510c2d7",
        //      "createddate": "2018-10-10T11:25:14.597Z"
        //  },
        // {
        //      "id": "11111111-c77c-48ac-ab42-89a8e8a99453",
        //      "eventid": "fd1bd252-7efb-4396-aea5-5ec804d80262",
        //      "accountid": "222224a4-b3f0-4391-b740-a26db510c2d7",
        //      "createddate": "2019-10-10T11:25:14.597Z"
        //  }]

        var myJson = JsonConvert.DeserializeObject<List<JObject>>(File.ReadAllText(Path.Combine(startLocation, @"Data\events.json")));
        var firstItem = myJson[0];

        var client = GetElasticClient();
        var indexName = "transactions-test-write";

        // When, if I use this method to send a single event, then everything is OK
        var responseSingle = client.LowLevel.
            Index<VoidResponse>(
                indexName,
                firstItem["id"].Value<string>(),
                PostData.String(firstItem.ToString(Formatting.None)),
                new IndexRequestParameters() { Routing = "et4" }
            );


        // When I use this code to send multiple items I got this error
        //
        // {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Malformed action/metadata line [1],
        // expected START_OBJECT or END_OBJECT but found [VALUE_STRING]"}],"type":"illegal_argument_exception",
        // "reason":"Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]"},
        // "status":400}

        var jsonPostData = PostData.MultiJson(myJson.Select(s => JsonConvert.SerializeObject(s, Formatting.None)));

        var bulkRequestParameters = new BulkRequestParameters { Routing = "et4" };
        var response = await client.LowLevel.BulkAsync<VoidResponse>(indexName, jsonPostData, bulkRequestParameters);
        if (!response.Success)
        {
            Console.WriteLine($"Error: {response.DebugInformation}");
        }

        Console.WriteLine("Completed");
        Console.ReadKey();
    }

How to use PostData to send multiple items?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.