# Conditional N-gram token filter?

**URL:** <https://discuss.elastic.co/t/conditional-n-gram-token-filter/264460>\
**Category:** Elasticsearch\
**Created:** [February 16, 2021, 5:21pm UTC](https://discuss.elastic.co/t/conditional-n-gram-token-filter/264460 "2021-02-16T17:21:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehsan\_kabiri\_33](https://avatars.discourse-cdn.com/v4/letter/e/b77776/32.png) [@ehsan\_kabiri\_33](https://discuss.elastic.co/u/ehsan_kabiri_33)\
**Post date:** [February 16, 2021, 5:21pm UTC](https://discuss.elastic.co/t/conditional-n-gram-token-filter/264460/1 "2021-02-16T17:21:19Z")

</div>

Could we have conditional N-gram token filter? Tell me yes or no ?

More explanation : I have a text "The animal is cute"  
I want to know if it is possible to have `min_gram=3` if word.length\>=3  
and `min_gram=2` if word.length\< 3 ?!

Invert-indexing all tokens/words with min\_gram=2 could have many cost on server and min\_gram=3 results in missing some small words in Persian.  
Any hint is appreciable

I would be so thankful if your answer is yes, give me an example.  
This is my current code using Nest and I need to know if I can change N-gram token filter for different words

```auto
         var suggestionindexResponse = await client.Indices.CreateAsync("suggestionindex", c => c
                .Settings(s => s
                .Setting(UpdatableIndexSettings.MaxNGramDiff, 8)
                .Analysis(a => a
                .CharFilters(cf => cf.Mapping("mycharfilter", cf => cf.Mappings(new[] { "\\u200C=>\\u0020" }))) //convert somthing like half space (Ctrl + - or shift+ctrl+4) to space                 
                .Analyzers(aa => aa
                .Custom("suggestionanalyzer", ca => ca
                .CharFilters("mycharfilter")
                .Filters(new List<string> { "lowercase", "my_stopword", "mynGram", "decimal_digit", "arabic_normalization", "persian_normalization" })
                .Tokenizer("standard"))
                .Custom("suggestionsearchanalyzer", ca => ca
                .CharFilters("mycharfilter")
                .Filters(new List<string> { "lowercase", "my_stopword", "decimal_digit", "arabic_normalization", "persian_normalization" })
                .Tokenizer("standard"))
                )
                .TokenFilters(tf => tf
                .Stop("my_stopword", st => st.StopWords("_persian_"))
                .NGram("mynGram", td => td
                .MaxGram(10).MinGram(2))
                )
                ))
                .Map<WordSuggestion>(m => m
                  .Dynamic(DynamicMapping.Strict)
                  .AutoMap()
                  .Properties(ps => ps
                  .Text(s => s.Name(n => n.word).Analyzer("suggestionanalyzer").SearchAnalyzer("suggestionsearchanalyzer"))
                  .Number(n => n.Name(i => i.Id).Type(NumberType.Integer))
                 )));

```

---

<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:** [March 16, 2021, 5:21pm UTC](https://discuss.elastic.co/t/conditional-n-gram-token-filter/264460/2 "2021-03-16T17:21:49Z")

</div>

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