There are several ways to do this.
Attempting to index a document as dynamic will not work, but you can index it as an object through an IndexRequest object.
dynamic dynamicDoc = new { }; ElasticClient esClient = new ElasticClient(esSettings); IndexRequest<object> request = new IndexRequest<object>(dynamicDoc) { Index = "someindex", Type = "SomeType", Id = "someid" }; esClient.Index<object>(request);
Or if you are dealing with documents in bulk
List<dynamic> Documents = new List<dynamic>(); //Populate Documents BulkDescriptor descriptor = new BulkDescriptor(); foreach(var doc in Documents) { descriptor.Index<object>(i => i .Index("someindex") .Type("SomeType") .Id("someid") .Document(doc)); } esClient.Bulk(descriptor);
NEST (or, more precisely, Elasticsearch.Net) also has a .Raw variant attached to the ElasticClient class, which can index raw JSON. Using Raw.Index (), let's do the following:
string documentJson = JsonConvert.SerializeObject(document.Document); ElasticsearchResponse<string> result = esClient.Raw.Index(document.Index, document.Type, document.Id, documentJson);
The type descriptor for the response is the type in which you are expecting a response (a string means you will have a json serialized response that you can deserialize and do something about). This circumvents the whole problem of an object type, and NEST indexes the document in Elasticsearch exactly as expected.
Ellesedil
source share