# Avoid Partial Text Match using n-gram analyser

**URL:** <https://discuss.elastic.co/t/avoid-partial-text-match-using-n-gram-analyser/91591>\
**Category:** Elasticsearch\
**Created:** [July 3, 2017, 6:36am UTC](https://discuss.elastic.co/t/avoid-partial-text-match-using-n-gram-analyser/91591 "2017-07-03T06:36:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kashi\_mn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kashi_mn/32/19639_2.png) [@kashi\_mn](https://discuss.elastic.co/u/kashi_mn)\
**Post date:** [July 3, 2017, 6:36am UTC](https://discuss.elastic.co/t/avoid-partial-text-match-using-n-gram-analyser/91591/1 "2017-07-03T06:36:32Z")

</div>

I have a scenario where in I am using n-gram analyser as Default Analyser and Standard analyser as search analyser in my settings for the field - "title".

The word "NOS" is very specific to our application and I do have few documents with the text starting with "NOS". When I do a search I am getting results of all the documents including the ones that has NOS as a substring. But my expectation is to get only documents which has only NOS text in it.

For example : When I search for "NOS" i get the results containing "diagnosis", "noseband" etc.

n-gram analyser is really working great for rest of the application in terms of partial matching. But I would like to avoid partial matching for such keywords- NOS, BNN, etc.

Any help please?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [July 4, 2017, 5:28am UTC](https://discuss.elastic.co/t/avoid-partial-text-match-using-n-gram-analyser/91591/2 "2017-07-04T05:28:36Z")

</div>

Slightly tricky scenario since you want to go against the default matching  
algorithm. There is the keyword marker filter, but it will only protect  
tokens against further filters that recognize the marker. Stemmers and  
synonyms will recognize it, but I doubt n-gram filters will.

One hack would be to apply a pattern replace character filter [1] (which  
works pre-tokenization) to convert those words to a nonsensical work  
(unicode characters work best), which should be shorter than your minimum  
n-gram and then reconvert them with a synonym filter. These special tokens  
need to be unique enough to be found in the text (all caps is great).

[1]  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html)

---

<div class="post-metadata">

**Author:** ![kashi\_mn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kashi_mn/32/19639_2.png) [@kashi\_mn](https://discuss.elastic.co/u/kashi_mn)\
**Post date:** [July 4, 2017, 6:07am UTC](https://discuss.elastic.co/t/avoid-partial-text-match-using-n-gram-analyser/91591/3 "2017-07-04T06:07:34Z")

</div>

Thank you @Ivan for the help. I will try pattern replace approach and see how it goes.

---

<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:** [August 1, 2017, 6:08am UTC](https://discuss.elastic.co/t/avoid-partial-text-match-using-n-gram-analyser/91591/4 "2017-08-01T06:08:19Z")

</div>

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