Skip to content

Instantly share code, notes, and snippets.

@deeja
Last active March 30, 2021 16:46
Show Gist options
  • Save deeja/8de89c3f1d31b5e153f005c781c24a0e to your computer and use it in GitHub Desktop.
Save deeja/8de89c3f1d31b5e153f005c781c24a0e to your computer and use it in GitHub Desktop.
Create a data stream index template using NEST for Elastic Search
using System;
using Elasticsearch.Net;
using Nest;
using NUnit.Framework;
public class CreateDataStreamTest
{
[Test]
public void ShouldCreateDataStreamTemplate()
{
var client = new ElasticClient(new Uri("http://localhost:9200/"));
var v = "somename";
var response = PutDataStreamTemplate(client, v);
Assert.False(response.TryGetServerError(out var serverError) && serverError.Status > 0, "Error {0}", serverError);
}
private static StringResponse PutDataStreamTemplate(ElasticClient client, string templateName)
{
var typeMappingDescriptor = new TypeMappingDescriptor<MyDocument>().AutoMap();
var response = client.LowLevel.Indices.PutTemplateV2ForAll<StringResponse>($"{templateName}_data_stream",
PostData.Serializable(new
{
index_patterns = new[] {$"{templateName}*"},
data_stream = new { },
template = new
{
mappings = typeMappingDescriptor
}
}));
return response;
}
public class MyDocument
{
[Date(Name = "@timestamp")] public DateTime Timestamp { get; set; }
[Keyword] public string Code { get; set; }
public int SortOrder { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment