# Search using ngram max\_ngram

**URL:** <https://discuss.elastic.co/t/search-using-ngram-max-ngram/122099>\
**Category:** Elasticsearch\
**Created:** [March 1, 2018, 2:42pm UTC](https://discuss.elastic.co/t/search-using-ngram-max-ngram/122099 "2018-03-01T14:42:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ram\_Ibaset](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ram_ibaset/32/27747_2.png) [@Ram\_Ibaset](https://discuss.elastic.co/u/Ram_Ibaset)\
**Post date:** [March 1, 2018, 2:42pm UTC](https://discuss.elastic.co/t/search-using-ngram-max-ngram/122099/1 "2018-03-01T14:42:31Z")

</div>

What's the best practice for pattern searches? I have implemented using ngram tokenizer, however there are min and max limitations. For instance

min\_gram - 3 and max\_gram - 4 will limit the search to 3 characters and a max of 4 characters. like,

**_abc_** will produce hits (if any) and **_abcd_** will produce hits (if any)  
If I type **_abcde_** then I won't have any hits.

Because the tokenizer is going to create tokens ranging from 3-4 chars in length.

The use case I am trying to solve is a scaleable pattern search which can accommodate any string length.

What's a solution for this. I came across suggestions to create multiple indices and "alias" them. Not sure what that does to the performance and how it can be used to scale for all of the use cases. Can someone help with the best practice implementation?

---

<div class="post-metadata">

**Author:** ![Ram\_Ibaset](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ram_ibaset/32/27747_2.png) [@Ram\_Ibaset](https://discuss.elastic.co/u/Ram_Ibaset)\
**Post date:** [March 1, 2018, 6:35pm UTC](https://discuss.elastic.co/t/search-using-ngram-max-ngram/122099/2 "2018-03-01T18:35:11Z")

</div>

I am more puzzled by the search results now. I ran a test against the following patterns: (example. AA-PP-001-007)

- search for 'aa-' is successful
- search for 'aa-p' is not successful
- search for 'aa- ....' until i type in the full string is unsuccessful
- search for 'aa-pp-001-007' is successful

My tokenizer min\_gram = 3 and max\_gram=10 and tokenizes all characters including special characters:

```
{"tokens":[{"token":"aa-","start_offset":0,"end_offset":3,"type":"word","position":0},{"token":"aa-p","start_offset":0,"end_offset":4,"type":"word","position":1},{"token":"aa-
    pp","start_offset":0,"end_offset":5,"type":"word","position":2},{"token":"aa-pp-
    ","start_offset":0,"end_offset":6,"type":"word","position":3},{"token":"aa-pp-
    0","start_offset":0,"end_offset":7,"type":"word","position":4},{"token":"aa-pp-00","start_offset":0,"end_offset":8,"type":"word","position":5},{"token":"aa-pp-001","start_offset":0,"end_offset":9,"type":"word","position":6},{"token":"aa-pp-001-","start_offset":0,"end_offset":10,"type":"word","position":7},{"token":"a-p","start_offset":1,"end_offset":4,"type":"word","position":8},{"token":"a-pp","start_offset":1,"end_offset":5,"type":"word","position":9},{"token":"a-pp-","start_offset":1,"end_offset":6,"type":"word","position":10},{"token":"a-pp-0","start_offset":1,"end_offset":7,"type":"word","position":11},{"token":"a-pp-00","start_offset":1,"end_offset":8,"type":"word","position":12},{"token":"a-pp-001","start_offset":1,"end_offset":9,"type":"word","position":13},{"token":"a-pp-001-","start_offset":1,"end_offset":10,"type":"word","position":14},{"token":"a-pp-001-0","start_offset":1,"end_offset":11,"type":"word","position":15},{"token":"-pp","start_offset":2,"end_offset":5,"type":"word","position":16},{"token":"-pp-","start_offset":2,"end_offset":6,"type":"word","position":17},{"token":"-pp-0","start_offset":2,"end_offset":7,"type":"word","position":18},{"token":"-pp-00","start_offset":2,"end_offset":8,"type":"word","position":19},{"token":"-pp-001","start_offset":2,"end_offset":9,"type":"word","position":20},{"token":"-pp-001-","start_offset":2,"end_offset":10,"type":"word","position":21},{"token":"-pp-001-0","start_offset":2,"end_offset":11,"type":"word","position":22},{"token":"-pp-001-00","start_offset":2,"end_offset":12,"type":"word","position":23},{"token":"pp-","start_offset":3,"end_offset":6,"type":"word","position":24},{"token":"pp-0","start_offset":3,"end_offset":7,"type":"word","position":25},{"token":"pp-00","start_offset":3,"end_offset":8,"type":"word","position":26},{"token":"pp-001","start_offset":3,"end_offset":9,"type":"word","position":27},{"token":"pp-001-","start_offset":3,"end_offset":10,"type":"word","position":28},{"token":"pp-001-0","start_offset":3,"end_offset":11,"type":"word","position":29},{"token":"pp-001-00","start_offset":3,"end_offset":12,"type":"word","position":30},{"token":"pp-001-007","start_offset":3,"end_offset":13,"type":"word","position":31},{"token":"p-0","start_offset":4,"end_offset":7,"type":"word","position":32},{"token":"p-00","start_offset":4,"end_offset":8,"type":"word","position":33},{"token":"p-001","start_offset":4,"end_offset":9,"type":"word","position":34},{"token":"p-001-","start_offset":4,"end_offset":10,"type":"word","position":35},{"token":"p-001-0","start_offset":4,"end_offset":11,"type":"word","position":36},{"token":"p-001-00","start_offset":4,"end_offset":12,"type":"word","position":37},{"token":"p-001-007","start_offset":4,"end_offset":13,"type":"word","position":38},{"token":"-00","start_offset":5,"end_offset":8,"type":"word","position":39},{"token":"-001","start_offset":5,"end_offset":9,"type":"word","position":40},{"token":"-001-","start_offset":5,"end_offset":10,"type":"word","position":41},{"token":"-001-0","start_offset":5,"end_offset":11,"type":"word","position":42},{"token":"-001-00","start_offset":5,"end_offset":12,"type":"word","position":43},{"token":"-001-007","start_offset":5,"end_offset":13,"type":"word","position":44},{"token":"001","start_offset":6,"end_offset":9,"type":"word","position":45},{"token":"001-","start_offset":6,"end_offset":10,"type":"word","position":46},{"token":"001-0","start_offset":6,"end_offset":11,"type":"word","position":47},{"token":"001-00","start_offset":6,"end_offset":12,"type":"word","position":48},{"token":"001-007","start_offset":6,"end_offset":13,"type":"word","position":49},{"token":"01-","start_offset":7,"end_offset":10,"type":"word","position":50},{"token":"01-0","start_offset":7,"end_offset":11,"type":"word","position":51},{"token":"01-00","start_offset":7,"end_offset":12,"type":"word","position":52},{"token":"01-007","start_offset":7,"end_offset":13,"type":"word","position":53},{"token":"1-0","start_offset":8,"end_offset":11,"type":"word","position":54},{"token":"1-00","start_offset":8,"end_offset":12,"type":"word","position":55},{"token":"1-007","start_offset":8,"end_offset":13,"type":"word","position":56},{"token":"-00","start_offset":9,"end_offset":12,"type":"word","position":57},{"token":"-007","start_offset":9,"end_offset":13,"type":"word","position":58},{"token":"007","start_offset":10,"end_offset":13,"type":"word","position":59}]}
```

---

<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 29, 2018, 6:35pm UTC](https://discuss.elastic.co/t/search-using-ngram-max-ngram/122099/3 "2018-03-29T18:35:26Z")

</div>

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