# Better way to do a wildacard search - prefix wildcard or match\_phrase\_prefix?

**URL:** <https://discuss.elastic.co/t/better-way-to-do-a-wildacard-search-prefix-wildcard-or-match-phrase-prefix/116524>\
**Category:** Elasticsearch\
**Created:** [January 22, 2018, 7:04pm UTC](https://discuss.elastic.co/t/better-way-to-do-a-wildacard-search-prefix-wildcard-or-match-phrase-prefix/116524 "2018-01-22T19:04:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimalini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dimalini/32/22448_2.png) [@dimalini](https://discuss.elastic.co/u/dimalini)\
**Post date:** [January 22, 2018, 7:04pm UTC](https://discuss.elastic.co/t/better-way-to-do-a-wildacard-search-prefix-wildcard-or-match-phrase-prefix/116524/1 "2018-01-22T19:04:37Z")

</div>

Hi,  
We have a scenario where we need to support queries like "\*foo", "\*abc" etc(calling them prefix wildcard).

And doing a wildcard search like below, had a great impact on the available memory and impacted the search performance.

> Query1  
> POST index/\_search  
> {  
> "query" : {  
> "wildcard" : {  
> "field1" : "\*foo",  
> "rewrite" : "top\_terms\_boost\_100" // we try to force limit the expansions to 100  
> }  
> }

One solution we are currently thinking of ,is to have reverse token filter applied to a field to store the reverse terms and do the above query like -

> Query2  
> POST index/\_search  
> {  
> "query" : {  
> "wildcard" : {  
> "field1" : "oof\*",  
> "rewrite" : "top\_terms\_boost\_100" // we try to force limit the expansions to 100  
> }  
> }

I have 2 questions , please help clarify  
(1) Is there any way we could better rewrite the Query1 so that we don't have a performance hit in terms of memory consumption etc?

(2) On a field with reverse token applied is it better to use a wildcard term query (like Query2)or a match\_phrase\_prefix query ? Which one is recommended? Which is more performant?

Thanks, Divya

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 22, 2018, 7:32pm UTC](https://discuss.elastic.co/t/better-way-to-do-a-wildacard-search-prefix-wildcard-or-match-phrase-prefix/116524/2 "2018-01-22T19:32:37Z")

</div>

IMO it's indeed better to use a `reverse token` based analyzer in such a case (with lowercase as well).

I'd also use a reverse token, lowercase and edge n gram filter at index time and a reverse token, lowercase at search time.

That will be less performant at index time (as you have to generate more tokens but will be much faster IMO at search time.

You need to test this strategy though.

---

<div class="post-metadata">

**Author:** ![dimalini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dimalini/32/22448_2.png) [@dimalini](https://discuss.elastic.co/u/dimalini)\
**Post date:** [January 23, 2018, 2:35am UTC](https://discuss.elastic.co/t/better-way-to-do-a-wildacard-search-prefix-wildcard-or-match-phrase-prefix/116524/3 "2018-01-23T02:35:52Z")

</div>

Thanks for the suggestion David!!

Could you also please share any recommendation regarding match\_phrase\_prefix or wilcard term query, the more performant one at a larger scale ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 23, 2018, 4:16am UTC](https://discuss.elastic.co/t/better-way-to-do-a-wildacard-search-prefix-wildcard-or-match-phrase-prefix/116524/4 "2018-01-23T04:16:11Z")

</div>

Avoid wildcard as much as possible IMO.

---

<div class="post-metadata">

**Author:** ![dimalini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dimalini/32/22448_2.png) [@dimalini](https://discuss.elastic.co/u/dimalini)\
**Post date:** [January 23, 2018, 4:25am UTC](https://discuss.elastic.co/t/better-way-to-do-a-wildacard-search-prefix-wildcard-or-match-phrase-prefix/116524/5 "2018-01-23T04:25:37Z")

</div>

Thanks David!

---

<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:** [February 20, 2018, 4:25am UTC](https://discuss.elastic.co/t/better-way-to-do-a-wildacard-search-prefix-wildcard-or-match-phrase-prefix/116524/6 "2018-02-20T04:25:58Z")

</div>

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