# Multiword search term with contiguos character matching

**URL:** <https://discuss.elastic.co/t/multiword-search-term-with-contiguos-character-matching/133602>\
**Category:** Elasticsearch\
**Created:** [May 29, 2018, 2:03am UTC](https://discuss.elastic.co/t/multiword-search-term-with-contiguos-character-matching/133602 "2018-05-29T02:03:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![echander](https://avatars.discourse-cdn.com/v4/letter/e/f9ae1b/32.png) [@echander](https://discuss.elastic.co/u/echander)\
**Post date:** [May 29, 2018, 2:03am UTC](https://discuss.elastic.co/t/multiword-search-term-with-contiguos-character-matching/133602/1 "2018-05-29T02:03:02Z")

</div>

I am trying to write a query in ElasticSearch which matches contiguous characters in the words. So, if my index has "John Doe", I should still see "John Doe" returned by Elasticsearch for the below searches.

1.john doe  
2.john do  
3.ohn do  
4.john  
5.n doe

I have an ngram analyzer on my data with a mingram:2 and maxgram:10.  
I have tried the below query so far. Is ngram the way to go here or am I missing something. Please note that searching "jo oe" should NOT return "John Doe" result.

{  
"query": {  
"multi\_match": {  
"query": "term",  
"operator": "AND",  
"type": "phrase\_prefix",  
"max\_expansions": 50,  
"fields": [  
"Field1",  
"Field2"  
]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![echander](https://avatars.discourse-cdn.com/v4/letter/e/f9ae1b/32.png) [@echander](https://discuss.elastic.co/u/echander)\
**Post date:** [May 29, 2018, 6:49pm UTC](https://discuss.elastic.co/t/multiword-search-term-with-contiguos-character-matching/133602/2 "2018-05-29T18:49:28Z")

</div>

This is what worked for me. Instead of an ngram, index your data as keyword. And use wildcard regex match to match the words.

"query": {  
"bool": {  
"should": [  
{  
"wildcard": { "Field1": "_" + term + "_" }  
},  
{  
"wildcard": { "Field2": "_" + term + "_" }  
}  
],  
"minimum\_should\_match": 1  
}  
}

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 3, 2018, 8:21am UTC](https://discuss.elastic.co/t/multiword-search-term-with-contiguos-character-matching/133602/3 "2018-06-03T08:21:16Z")

</div>

Very interesting use case!  
Did you face any performance issues?  
For example: the search "ohn do" as you need to start the search with a wildcard character "\*ohn do\*".

If your documents have "John Doe", maybe it would be better to use regexp query instead of wildcard.  
As you said, wildcard is used on "not analyzed" fields.

---

<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:** [July 1, 2018, 8:21am UTC](https://discuss.elastic.co/t/multiword-search-term-with-contiguos-character-matching/133602/4 "2018-07-01T08:21:17Z")

</div>

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