Reputation: 121
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?
Upvotes: 7
Views: 4687
Reputation: 21
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()))
));
Upvotes: 1
Reputation: 121
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);
}
Upvotes: 5