Search code examples
c#elasticsearchindexingparent-childnest

Elasticsearch C# NEST IndexMany Children


I'm having an issue using the bulk method in NEST to index child records into Elasticsearch.

I am using ElasticSearch 2.3.5 and NEST 2.4.4

I've mapped an index as such:

    myindex
    {
     "mappings": {
       "elasticparent": {},
        "elasticchild": {
          "_parent": {
            "type": elasticparent
          }
        }
      }
    }

And I've indexed the parent objects using the IndexMany method:

    client.IndexMany<elasticparent>(batch, "myindex");

This all works well.

I would now like to index the children using IndexMany. Here's what I've tried so far:

     client.Bulk(s => s.IndexMany(IenumerableOfChild,
                                  (bulkDescriptor, record) =>
                                  bulkDescriptor.Index("myindex").Type("elasticchild").Parent(record.Id)));

The child and parent share the same Id integer.

I don't get an error, but the children never get indexed and the documents are never added to the total indexed count.

Indexing them individually works:

    foreach (var child in IenumerableOfChild
            {

                client.Index(child, descriptor => descriptor
                 .Parent(child.Id.ToString()).Index("myindex"));
            }

I don't want to index mass amounts individually. I would like to use IndexMany to bulk index the child records. Can someone point out what I'm doing wrong?


Solution

  • After further investigation, the Elastic Server was returning a timeout. By batching the requests to 1000 items at a time, it is now working properly!

        foreach (IEnumerable<object> batch in objects.Batch(1000))
                {
                    var indexResponse = client.Bulk(s => s.IndexMany(batch,
                                             (bulkDescriptor, record) =>
                                               bulkDescriptor.Index("myindex").Parent(record.Id.ToString()).Document(record).Type("elasticchild").Id(record.Id.ToString())));
    
                    Console.WriteLine(indexResponse);
                }