# Like query \*string\*

**URL:** <https://discuss.elastic.co/t/like-query-string/149971>\
**Category:** Elasticsearch\
**Created:** [September 26, 2018, 9:40am UTC](https://discuss.elastic.co/t/like-query-string/149971 "2018-09-26T09:40:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [September 26, 2018, 9:40am UTC](https://discuss.elastic.co/t/like-query-string/149971/1 "2018-09-26T09:40:00Z")

</div>

Hi guys,

Specification:  
Elasticsearch version : 6.2.2

I have data which having "emailaddress" field. Suppose my one of the value is "user@example.com". I might have search input like "@exam", "user@" , "user", ".com" etc.

I have tried to achieve this using query\_string with \*\* pattern match. I got appropriate result but It seems, it is slow. I also cannot use uax\_url\_email tokenizer because user is free to give any input. I have used default standard tokenizer.

With this, suppose i have two email address "user@example.com" and "example@user.com". If i am searching with "user@", It is showing both result. Which is not my output.

Is there any specific tokenizer or method by which i can achieve my output. It should be same as like mysql's like query with %@user%.

After this i used edge\_ngram tokenizer which gives me partially better output. Below is my analyzer:

```
"analysis":{
  "filter":{
	"emailtoken":{
	  "type":"edge_ngram",
	  "min_gram":1,
	  "max_gram":255,
	  "token_chars":[
	    "letter",
	    "digit",
	    "symbol",
	    "punctuation"
	  ]
	}
  },
  "analyzer":{
	"email":{
	  "type":"custom",
	  "tokenizer":"standard",
	  "filter":[
	    "lowercase",
	    "emailtoken"
	  ]
	 }
  }
 }
}

```

With this, Suppose i have email address like "name.sirname@example.com", So above anlyzer also got failed with the search of ".sirname" or "@example".

Thanks

---

<div class="post-metadata">

**Author:** ![fkelbert](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fkelbert/32/35762_2.png) [@fkelbert](https://discuss.elastic.co/u/fkelbert)\
**Post date:** [September 26, 2018, 9:48am UTC](https://discuss.elastic.co/t/like-query-string/149971/2 "2018-09-26T09:48:00Z")

</div>

Hi @ashishtiwari1993,

As you described, \*\* does work, but gives poor performance. This is due to how Elasticsearch indexes its documents internally.

Another approach is to index your strings multiple times (using [fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html)) using different [custom analyzers](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html). In particular, you might want to look into the [NGram tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html).

You might then also want to consider combining NGrams with a [prefix query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html).

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [September 26, 2018, 9:52am UTC](https://discuss.elastic.co/t/like-query-string/149971/3 "2018-09-26T09:52:52Z")

</div>

@fkelbert Thanks for quick prompt. I have edit my question forgot to mention about edge\_ngram tokenizer. In edge\_ngram i am facing issue with special chars like ".", "@".

---

<div class="post-metadata">

**Author:** ![fkelbert](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fkelbert/32/35762_2.png) [@fkelbert](https://discuss.elastic.co/u/fkelbert)\
**Post date:** [September 26, 2018, 9:55am UTC](https://discuss.elastic.co/t/like-query-string/149971/4 "2018-09-26T09:55:11Z")

</div>

Hi @ashishtiwari1993, Consider using NGrams instead of Edge-NGrams .

---

<div class="post-metadata">

**Author:** ![fkelbert](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fkelbert/32/35762_2.png) [@fkelbert](https://discuss.elastic.co/u/fkelbert)\
**Post date:** [September 26, 2018, 9:56am UTC](https://discuss.elastic.co/t/like-query-string/149971/5 "2018-09-26T09:56:56Z")

</div>

Further to that, you probably want to use the [`keyword` tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-keyword-tokenizer.html), which does not actually tokenize the string (i.e., it is a noop and keeps the string as-is).

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [September 26, 2018, 10:03am UTC](https://discuss.elastic.co/t/like-query-string/149971/6 "2018-09-26T10:03:19Z")

</div>

@fkelbert Thats cool it wokrs 🙂 Thanks for quick help. Just one doubt, I am having lots of records which contains email address. Now ngram tokenizer is going to apply on each email address. Would it be cause and delay in write performance ? Because i have multiple fields, On which want to implement the same method .

---

<div class="post-metadata">

**Author:** ![fkelbert](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fkelbert/32/35762_2.png) [@fkelbert](https://discuss.elastic.co/u/fkelbert)\
**Post date:** [September 26, 2018, 10:07am UTC](https://discuss.elastic.co/t/like-query-string/149971/7 "2018-09-26T10:07:41Z")

</div>

Great to hear 🙂

I'm afraid you'll need to experiment with performance. You can increase write throughput by configuring your index to use more primary shards.

Out of curiosity: how much faster are the ngrams compared to the \*\* method for you?

---

<div class="post-metadata">

**Author:** ![fkelbert](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fkelbert/32/35762_2.png) [@fkelbert](https://discuss.elastic.co/u/fkelbert)\
**Post date:** [September 26, 2018, 10:12am UTC](https://discuss.elastic.co/t/like-query-string/149971/8 "2018-09-26T10:12:41Z")

</div>

Another note: Think twice whether you _actually need_ this kind of method for many of your fields. For search, the "more standard" analyzers do a pretty good job in 95% of the use cases.

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [September 26, 2018, 10:15am UTC](https://discuss.elastic.co/t/like-query-string/149971/9 "2018-09-26T10:15:21Z")

</div>

Right now i am using same \*\*. Need to reindex the data to measure performance. But it will definitely faster than to regex, wildcard & \*\*. I will share my output here 🙂 . Thanks for the suggestion @fkelbert. Definitely i ll take care of use case.

---

<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:** [October 24, 2018, 10:15am UTC](https://discuss.elastic.co/t/like-query-string/149971/10 "2018-10-24T10:15:30Z")

</div>

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