# How to find matches based on Longest Common N-gram

**URL:** <https://discuss.elastic.co/t/how-to-find-matches-based-on-longest-common-n-gram/357671>\
**Category:** Elastic Search\
**Created:** [April 18, 2024, 6:19am UTC](https://discuss.elastic.co/t/how-to-find-matches-based-on-longest-common-n-gram/357671 "2024-04-18T06:19:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteliosM](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@SteliosM](https://discuss.elastic.co/u/SteliosM)\
**Post date:** [April 18, 2024, 6:19am UTC](https://discuss.elastic.co/t/how-to-find-matches-based-on-longest-common-n-gram/357671/1 "2024-04-18T06:19:11Z")

</div>

Hello Elastic Community,

I am currently working on a challenging problem where I need to find matches for an input string based on the longest common n-grams from a very large repository of indexed strings (around a billion entries). I need search query that prioritizes results based on contiguous substrings that appear in both the indexed strings and a new input query string.

For example, suppose I have the following strings indexed:

- "Blue house"
- "A nice blue house"
- "I want to buy a nice blue house"
- "house blue buy want I nice a" _\<- just to illustrate that I don't want to match tokens in random order, only n-grams_

Given a query string such as **"I do not want to buy a nice blue house"** , the ideal return would be "I want to buy a nice blue house", as it contains the longest n-gram match "want to buy a nice blue house" from the indexed strings.

However, I am not experienced with Elasticsearch and I am struggling with the proper configuration:

- I tried using a `match_phrase` query, but it only checks if the entire input string is a substring of the indexed documents, which is not what I need.
- I am aware of the n-gram tokenizer, but I am unsure how to configure and use it effectively when indexing the strings and when querying the index.

Any help here? Is there a better approach or tool I should consider given the volume and nature of the data?

Any insights or suggestions would be greatly appreciated!

Thank you!

---

<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:** [May 16, 2024, 6:19am UTC](https://discuss.elastic.co/t/how-to-find-matches-based-on-longest-common-n-gram/357671/2 "2024-05-16T06:19:52Z")

</div>

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