# Better way to implement "contain with"

**URL:** <https://discuss.elastic.co/t/better-way-to-implement-contain-with/110031>\
**Category:** Elasticsearch\
**Created:** [December 3, 2017, 3:24am UTC](https://discuss.elastic.co/t/better-way-to-implement-contain-with/110031 "2017-12-03T03:24:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KevinT](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@KevinT](https://discuss.elastic.co/u/KevinT)\
**Post date:** [December 3, 2017, 3:24am UTC](https://discuss.elastic.co/t/better-way-to-implement-contain-with/110031/1 "2017-12-03T03:24:38Z")

</div>

Currently we are using wildcard query start and end with \* to implement the "contains with" function, however it seems the performance is really bad, and we find that ES have documented this on [https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-wildcard-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-wildcard-query.html)

So I'm wondering whether there is a better way to replace the wildcard query?

---

<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:** [December 3, 2017, 5:57am UTC](https://discuss.elastic.co/t/better-way-to-implement-contain-with/110031/2 "2017-12-03T05:57:57Z")

</div>

Try using n-grams instead of wildcards:  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/\_ngrams\_for\_partial\_matching.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_ngrams_for_partial_matching.html)

N-grams required increased storage and could suffer from precision issue if  
not tuned.

---

<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:** [December 31, 2017, 5:58am UTC](https://discuss.elastic.co/t/better-way-to-implement-contain-with/110031/3 "2017-12-31T05:58:05Z")

</div>

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