# How to create an index template with NGramTokenizer using .NET client 8.0

**URL:** <https://discuss.elastic.co/t/how-to-create-an-index-template-with-ngramtokenizer-using-net-client-8-0/321569>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 19, 2022, 2:36pm UTC](https://discuss.elastic.co/t/how-to-create-an-index-template-with-ngramtokenizer-using-net-client-8-0/321569 "2022-12-19T14:36:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![UmLao](https://avatars.discourse-cdn.com/v4/letter/u/a6a055/32.png) [@UmLao](https://discuss.elastic.co/u/UmLao)\
**Post date:** [December 19, 2022, 2:36pm UTC](https://discuss.elastic.co/t/how-to-create-an-index-template-with-ngramtokenizer-using-net-client-8-0/321569/1 "2022-12-19T14:36:14Z")

</div>

I try to convert our C# code from Nest to the new Elastic.Clients.Elasticsearch client. Got stuck on many details. Currently I can't see how to build a request for creating an index template.

Code snippet:

```auto
var createIndexTemplateResponse = await _elasticClient.Indices.PutIndexTemplateAsync(templateName,
	d => d
	.IndexPatterns($"{_idxName}-*")
	.ComposedOf(new Collection<Name>())
	.Template(t => t
		.Settings(s => s
			.Index(i => i
				.DefaultPipeline(_pipelineName)
				.Analysis(a => a
					.Analyzer(ad => ad
						.Custom("fta", c => c
							.Filter(new[] { "lowercase" })
							.Tokenizer("ftt")))
					.Tokenizer(td => td
						.Add("ftt", ...))))))// TODO: Complete the code to use an NGramTokenizer
);

```

I want to use an NGramTokenizer like this:

```auto
var ngt = new NGramTokenizer
{
	MaxGram = 3,
	MinGram = 3,
	TokenChars = new[] { TokenChar.Letter, TokenChar.Digit, TokenChar.Punctuation, TokenChar.Symbol }
};

```

How to complete my request?

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [December 21, 2022, 11:57am UTC](https://discuss.elastic.co/t/how-to-create-an-index-template-with-ngramtokenizer-using-net-client-8-0/321569/2 "2022-12-21T11:57:38Z")

</div>

Hi, @UmLao.

The current release has a bug making analysis settings difficult to configure and causing bad serialization. This has been fixed this week and will go out in 8.0.4, hopefully in the next day or so. Once that ships, you'll be able to configure you template as follows:

```auto
var createIndexTemplateResponse = await client.Indices.PutIndexTemplateAsync(templateName,	d => d
	.IndexPatterns($"{_idxName}-*")
	.ComposedOf(new Collection<Name>())
	.Template(t => t
		.Settings(s => s
			.Index(i => i
				.DefaultPipeline(_pipelineName)
				.Analysis(a => a
					.Analyzers(ad => ad
						.Custom("fta", c => c
							.Filter(new[] { "lowercase" })
							.Tokenizer("ftt")))
					.Tokenizers(td => td
						.NGram("ftt", n => n
							.MaxGram(3)
							.MinGram(3)
							.TokenChars(new[] { TokenChar.Letter, TokenChar.Digit, TokenChar.Punctuation, TokenChar.Symbol }))))))));

```

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [December 21, 2022, 3:10pm UTC](https://discuss.elastic.co/t/how-to-create-an-index-template-with-ngramtokenizer-using-net-client-8-0/321569/3 "2022-12-21T15:10:23Z")

</div>

8.0.4 is now available on NuGet.

---

<div class="post-metadata">

**Author:** ![UmLao](https://avatars.discourse-cdn.com/v4/letter/u/a6a055/32.png) [@UmLao](https://discuss.elastic.co/u/UmLao)\
**Post date:** [December 22, 2022, 6:33am UTC](https://discuss.elastic.co/t/how-to-create-an-index-template-with-ngramtokenizer-using-net-client-8-0/321569/4 "2022-12-22T06:33:53Z")

</div>

Thanks Steve, now I can create this template as expected.

Follow-up: I also want to read this template in order to verify if its contents are correct. But the following request for getting this template **throws an Exception** :

```auto
var getIndexTemplateResponse = await _elasticClient.Indices.GetIndexTemplateAsync(d => d
	.Name(templateName));

```

The templateName is `myindex_template`.

The exception is

```auto
Elastic.Transport.UnexpectedTransportException: The JSON value could not be converted to Elastic.Clients.Elasticsearch.Names. Path: $.index_templates[0].index_template.index_patterns | LineNumber: 5 | BytePositionInLine: 28. ---> System.Text.Json.JsonException: The JSON value could not be converted to Elastic.Clients.Elasticsearch.Names. Path: $.index_templates[0].index_template.index_patterns | LineNumber: 5 | BytePositionInLine: 28.
   bei System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
   bei System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   bei System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   bei System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
   bei System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   bei System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   bei System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   bei System.Text.Json.JsonSerializer.ReadCore[TValue](JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& state, JsonConverter converterBase)
   bei System.Text.Json.JsonSerializer.ContinueDeserialize[TValue](ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, JsonConverter converter, JsonSerializerOptions options)
   bei System.Text.Json.JsonSerializer.<ReadAllAsync>d__65`1.MoveNext()

```

This is the template as seen in Kibana (Kibana and ES 8.2.3)

```auto
{
  "template": {
    "settings": {
      "index": {
        "analysis": {
          "analyzer": {
            "fta": {
              "filter": [
                "lowercase"
              ],
              "type": "custom",
              "tokenizer": "ftt"
            }
          },
          "tokenizer": {
            "ftt": {
              "token_chars": [
                "letter",
                "digit",
                "punctuation",
                "symbol"
              ],
              "min_gram": "3",
              "type": "ngram",
              "max_gram": "3"
            }
          }
        },
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "default_pipeline": "myindex_pipeline"
      }
    },
    "aliases": {},
    "mappings": {}
  }
}

```

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [December 22, 2022, 9:58am UTC](https://discuss.elastic.co/t/how-to-create-an-index-template-with-ngramtokenizer-using-net-client-8-0/321569/5 "2022-12-22T09:58:33Z")

</div>

Thanks for highlighting this bug. I've [raised an issue](https://github.com/elastic/elasticsearch-net/issues/7161) to track it. For exceptions and bugs like this one, please raise those directly as issues on the repository so they get my attention. The forum is best for general usage questions.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 19, 2023, 9:58am UTC](https://discuss.elastic.co/t/how-to-create-an-index-template-with-ngramtokenizer-using-net-client-8-0/321569/6 "2023-01-19T09:58:42Z")

</div>

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