Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 271
P Lab Avatar asked Aug 30 '16 18:08

P Lab


People also ask

What is Elasticsearch in C#?

Elasticsearch is a scalable open-source full-text searching tool and also analytics engine. It is used to save, search, and analyze huge data faster and also in real time. First of all, Elasticsearch is Rest Service. We can communicate with any Elasticsearch Service, using four verbs or functions.

Is Elasticsearch still used?

Since its release in 2010, Elasticsearch has quickly become the most popular search engine and is commonly used for log analytics, full-text search, security intelligence, business analytics, and operational intelligence use cases.

What programming language is Elasticsearch?

Elasticsearch is developed in Java and is dual-licensed under the source-available Server Side Public License and the Elastic license, while other parts fall under the proprietary (source-available) Elastic License.

Is Elasticsearch an ETL?

No, Elasticsearch is not an ETL tool. It is a free and open-source search engine for text, numeric, geospatial, structured, and unstructured data. Elasticsearch is mostly used in business intelligence, security intelligence, and operational intelligence. There are separate ETL tools available for Elasticsearch.


2 Answers

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);
            }
like image 99
P Lab Avatar answered Sep 16 '22 11:09

P Lab


You need to add. The routing field to the query in order to map the child with parent. Like this below:-

var indexResponse = elasticService.Bulk(s => s.IndexMany<Child> 
                                      (childreslist, 
 (bulkDescriptor, record) => bulkDescriptor.Index(Constants.INDEX_NAME)
                                                                    .Type("_doc")
                                                                    .Routing(new 
            Routing(record.id.ToString()))
                                                                ));                  
like image 41
VIVEK KUMAR Avatar answered Sep 17 '22 11:09

VIVEK KUMAR