# Word ngrams with match phrase

**URL:** <https://discuss.elastic.co/t/word-ngrams-with-match-phrase/145582>\
**Category:** Elasticsearch\
**Created:** [August 22, 2018, 3:44pm UTC](https://discuss.elastic.co/t/word-ngrams-with-match-phrase/145582 "2018-08-22T15:44:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterbe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peterbe/32/14836_2.png) [@peterbe](https://discuss.elastic.co/u/peterbe)\
**Post date:** [August 22, 2018, 3:44pm UTC](https://discuss.elastic.co/t/word-ngrams-with-match-phrase/145582/1 "2018-08-22T15:44:17Z")

</div>

I've indexed my documents with a `common_grams` token filter. It's working well. If I search for `kiss the frog` it will find documents, when searching with [match phrase query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html), that contains text like `you must kiss the frog he said`. So good so far.

However, if I search for `kiss the frog she` (note the `she` instead of `he`) it won't find anything with a match phrase query. Makes sense because that string of text doesn't appear in any document (`she` instead of `he`). I can switch to a regular match query and it finds documents like `the frog was green` or `kiss the girl said the witch`. In my context, this is often not desirable because I get like 12,000 matches or some other ridiculous high amount of matches. Basically, documents that contain `frog` and `kiss the` and `the frog` etc. Not very useful. (Also, this was a very simple and short example.)

What I'd really like to do is "re-attempt the query". A human might notice and say "Try searching for just `kiss the frog` and skip the last word.". I've tried writing some Python code with some basic natural language processing that breaks up the whole search query term into smaller parts, and re-attempt each one, one at a time. This functionality kicks in only when 0 documents are found with a match phrase query. But this is kinda lousy as there are a crazy amount of word-ngrams combinations in a long search term.

For example, from the query term:

```
cause im not who you think i am you think i wont hurt no body you think i can be your medicine

```

I get:

```
['cause im not who you',
 'cause im not who',
 'cause im not',
 'cause im not who you think',
 'cause im not who you',
 'cause im not who',
 'think i wont hurt',
 'wont hurt no body',
 'you think i wont',
 'i wont hurt no',
 'hurt no body you',
 'no body you think',
 'body you think i']

```

That's 13 additional queries! And sometimes, I have to go further and look for even shorter word-ngrams and there's even more of those.  
Also, sometimes I go even further and do boolean "must" queries on combinations like:

```
[['cause im not', 'no body you think'],
['cause im not', 'you think i wont'],
...

```

I would love some guidance and tips on how to improve on this?

My goal is to have what Google has, where they, in a split second, realize that the whole search term doesn't match anything but the search results are still good and they have to say "Missing: /she/".  
 ![30%20AM](https://us1.discourse-cdn.com/elastic/original/3X/0/1/01ad04b046c8005711d5fdb189650686cc1fbaca.png)

---

<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:** [September 19, 2018, 3:44pm UTC](https://discuss.elastic.co/t/word-ngrams-with-match-phrase/145582/2 "2018-09-19T15:44:22Z")

</div>

Closed on September 19, 2018, 3:44pm UTC

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