Create index pattern from C# Nest

Hello everyone.
I need to create an index pattern using C # Nest, without using kibana.
I searched the Nest API but didn't find it. Is possible create index pattern using the Nest API?

Regards,

Alder

Yes, it's possible to create an index template with an index pattern with Nest (high level client) or Elasticsearch.Net (low level client)

Assuming 7.x versions, with Nest

var client = new ElasticClient();

var response = client.Indices.PutTemplate("template_name", p => p
	.IndexPatterns("foo-*")
	.Map(m => m
		.DynamicTemplates(d => d
			.DynamicTemplate("strings", dt => dt
				.MatchMappingType("string")
				.Mapping(mm => mm
					.Text(t => t
						.Fields(f => f
							.Keyword(k => k
								.Name("raw")
								.IgnoreAbove(256)
							)
						)
					)
				)
			)
		)
	)
);

or using the object initializer syntax to construct a request

var request = new PutIndexTemplateRequest("template_name")
{
	IndexPatterns = new[] { "foo" },
	Mappings = new TypeMapping
	{
		DynamicTemplates = new DynamicTemplateContainer
		{
			{ "strings", new DynamicTemplate
				{
					MatchMappingType = "string",
					Mapping = new TextProperty
					{
						Fields = new Properties
						{
							{ "raw", new KeywordProperty
								{
									Name = "raw",
									IgnoreAbove = 256
								}
							}
						}
					}

				}
			}
		}
	}
};

var response = client.Indices.PutTemplate(request);

both yield the request

PUT <scheme://host:port>/_template/template_name
{
  "index_patterns": [
    "foo-*"
  ],
  "mappings": {
    "dynamic_templates": [
      {
        "strings": {
          "mapping": {
            "fields": {
              "raw": {
                "ignore_above": 256,
                "type": "keyword"
              }
            },
            "type": "text"
          },
          "match_mapping_type": "string"
        }
      }
    ]
  }
}

With the low level client

var lowLevelClient = new ElasticLowLevelClient();

var json = @"{
  ""index_patterns"": [
    ""foo-*""
  ],
  ""mappings"": {
    ""dynamic_templates"": [
      {
        ""strings"": {
          ""mapping"": {
            ""fields"": {
              ""raw"": {
                ""ignore_above"": 256,
                ""type"": ""keyword""
              }
            },
            ""type"": ""text""
          },
          ""match_mapping_type"": ""string""
        }
      }
    ]
  }
}";

var stringResponse = lowLevelClient.Indices.PutTemplateForAll<StringResponse>(
    "template_name", 
    PostData.String(json));

Thank you very much

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