Skip to content

Instantly share code, notes, and snippets.

@Yegoroff
Last active October 2, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yegoroff/2235286 to your computer and use it in GitHub Desktop.
Save Yegoroff/2235286 to your computer and use it in GitHub Desktop.
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