Skip to content

Instantly share code, notes, and snippets.

@Yegoroff
Last active Oct 2, 2015
Embed
What would you like to do?
Elastic Search Bulk
// Sample of Bulk functionality
var connection = new ElasticConnection("localhost", 9200);
var serializer = new JsonNetSerializer();
var command = new BulkCommand(index: "myindex", type: "mytype");
// MANUAL BULK CONSTRUCTION.
var myObject = new MyObject();
var json = new BulkBuilder(serializer);
.Index( data: myObject, id: myObject.Id)
// Optional parameters [index, type, id, versionType, version, routing, percolate, parent, timestamp, ttl, replication]
.Create( data: myObject, index: "anotherIndex", type: "anotherType", id: myObject.Id, versionType: VersionType.External, version: 1)
.Delete( id: myObject.Id)
.Build();
string result = connection.Post(command, json);
// Parse bulk result.
BulkResult bulkResult = serializer.ToBulkResult(result);
...
// PIPELINED BULK CONSTRUCTION FROM ARRAY
IEnumerable<MyObjects> myObjects = new List<MyObject>();
IEnumerable<string> jsons = new BulkBuilder(serializer)
.PipelineCollection( myObjects,
(builder, object) => builder.Index( data: myObject, id: myObject.Id)) // You can apply any custom logic here
// to generate Inserts, Creates, Deletes.
.BuildBatches(batchSize: 30); // returns deferred JSONs IEnumerable with at most 30 bulk operations in each element,
// this will allow to process input elements on the fly - not generating all bulk JSON at
// Alternatively you can use Build(); to generate all bulk JSON.
foreach(string json in jsons)
{
// Send bulk batch.
string result = connection.Post(command, json);
// Parse bulk batch result.
BulkResult bulkResult = serializer.ToBulkResult(result);
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment