Can NEST index multiple documents to a data stream?

The IndexMany method has no way to specify the op_type. And the Bulk method doesn't seem to provide any configuration for op_type either.

Documents can be bulk indexed to a data stream with the bulk API

var client = new ElasticClient();

var docs = new [] 
{
	new { Name = "foo", Value = "bar" },
	new { Name = "foo2", Value = "bar2" },
	new { Name = "foo3", Value = "bar3" },
};

var bulkResponse = client.Bulk(b => b
	.Index("my-data-stream")
	.Refresh(Refresh.True)
	.CreateMany(docs)
);

which yields the following request

POST /my-data-stream/_bulk?refresh=true
{"create":{"_id":null}}
{"name":"foo","value":"bar"}
{"create":{"_id":null}}
{"name":"foo2","value":"bar2"}
{"create":{"_id":null}}
{"name":"foo3","value":"bar3"}

If you have many documents to index to a data stream, I'd recommend using the bulk observable to do so

// some function that lazily enumerates documents
IEnumerable<object> GetDocs()
{
	yield return new { Name = "foo", Value = "bar" };
	yield return new { Name = "foo2", Value = "bar2" };
	yield return new { Name = "foo3", Value = "bar3" };
}

var bulkAllObservable = client.BulkAll(GetDocs(), d => d
	.Index("my-data-stream")
	.RefreshOnCompleted()
	.BufferToBulk((desc, docs) => desc.CreateMany(docs))
);

var waitHandle = new ManualResetEvent(false);
ExceptionDispatchInfo exceptionDispatchInfo = null;

var observer = new BulkAllObserver(
	onNext: response =>
	{
		// do something e.g. write number of pages to console
    },
	onError: exception =>
	{
		exceptionDispatchInfo = ExceptionDispatchInfo.Capture(exception);
		waitHandle.Set();
	},
	onCompleted: () => waitHandle.Set());

bulkAllObservable.Subscribe(observer);
waitHandle.WaitOne();
exceptionDispatchInfo?.Throw();

Thanks. The Bulk methods seems to have already covered the op_type setting. Hope this could be mentioned somewhere in the documentation.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.