# beginsWith / endsWith / contains

**URL:** <https://discuss.elastic.co/t/beginswith-endswith-contains/5746>\
**Category:** Elasticsearch\
**Created:** [November 2, 2011, 9:43am UTC](https://discuss.elastic.co/t/beginswith-endswith-contains/5746 "2011-11-02T09:43:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawi/32/2439_2.png) [@dawi](https://discuss.elastic.co/u/dawi)\
**Post date:** [November 2, 2011, 9:43am UTC](https://discuss.elastic.co/t/beginswith-endswith-contains/5746/1 "2011-11-02T09:43:16Z")

</div>

Hi,

I have to implement a search backend for our product to replace the  
old sql queries.

The index is build using the following default analyzer settings:  
index.analysis.analyzer.default.filter = lowercase  
index.analysis.analyzer.default.tokenizer = keyword

Now I have to provide the following search methods:

- exact
- matchesPattern
- beginsWith
- endsWith
- contains

1. exact is implemented as term query.

- "aaa" matches exactly "aaa"
- "a_a" matches "a_a" but not "aaa"

1. matchesPattern is implemented as wildcard query

- "a" matches "a"
- "a\*" matches "a", "aa", "ab", "a\*"  
Question here: how can I search for wildcards in fields?
- something like "a\*_" that would match "a_", "a_a" and "a_b" but not  
"a" and "aa"

1. beginsWith is implemented as prefix query.

- "aaa" matches "aaa", "aaaa", "aaa\*"
- "a_a" matches "a_a", "a\*aa" but not "aaa"

1. endsWith  
Should behave as beginsWith.  
But what is the best way to implement this feature?  
Is it possible to define multiple analyzers per field and to use the  
reverse filter for this?  
index.analysis.analyzer.reverse.filter=reverse  
index.analysis.analyzer.reverse.tokenizer=keyword  
Or do I have to store a reversed version of the field in the index and  
use the prefix query?

2. contains

- "a" matches "aa", "aaaa"
- "a\*" matches "a\*" or "aa\*aa" but not "aaaa"  
Could be implemented using wildcard query if character escaping is  
possible there.

Hopefully anyone has some tips to point me in the right directions.

Regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [November 2, 2011, 2:34pm UTC](https://discuss.elastic.co/t/beginswith-endswith-contains/5746/2 "2011-11-02T14:34:40Z")

</div>

It is possible to use multiple types (and so multiple analyzers) for  
one field:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Also I would suggest to use edge ngram tokenizer/filter to improve  
performance of wildcard searches:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

there is also an option for front or back.

Peter.

On 2 Nov., 10:43, dawi [d.wilmer.1...@googlemail.com](mailto:d.wilmer.1...@googlemail.com) wrote:

> Hi,
> 
> I have to implement a search backend for our product to replace the  
> old sql queries.
> 
> The index is build using the following default analyzer settings:  
> index.analysis.analyzer.default.filter = lowercase  
> index.analysis.analyzer.default.tokenizer = keyword
> 
> Now I have to provide the following search methods:
> 
> - exact
> - matchesPattern
> - beginsWith
> - endsWith
> - contains
> 
> 1. exact is implemented as term query.
> 
> - "aaa" matches exactly "aaa"
> - "a_a" matches "a_a" but not "aaa"
> 
> 1. matchesPattern is implemented as wildcard query
> 
> - "a" matches "a"
> - "a\*" matches "a", "aa", "ab", "a\*"  
> Question here: how can I search for wildcards in fields?
> - something like "a\*_" that would match "a_", "a_a" and "a_b" but not  
> "a" and "aa"
> 
> 1. beginsWith is implemented as prefix query.
> 
> - "aaa" matches "aaa", "aaaa", "aaa\*"
> - "a_a" matches "a_a", "a\*aa" but not "aaa"
> 
> 1. endsWith  
> Should behave as beginsWith.  
> But what is the best way to implement this feature?  
> Is it possible to define multiple analyzers per field and to use the  
> reverse filter for this?  
> index.analysis.analyzer.reverse.filter=reverse  
> index.analysis.analyzer.reverse.tokenizer=keyword  
> Or do I have to store a reversed version of the field in the index and  
> use the prefix query?
> 
> 2. contains
> 
> - "a" matches "aa", "aaaa"
> - "a\*" matches "a\*" or "aa\*aa" but not "aaaa"  
> Could be implemented using wildcard query if character escaping is  
> possible there.
> 
> Hopefully anyone has some tips to point me in the right directions.
> 
> Regards,  
> Daniel

---

<div class="post-metadata">

**Author:** ![dawi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawi/32/2439_2.png) [@dawi](https://discuss.elastic.co/u/dawi)\
**Post date:** [November 3, 2011, 8:59am UTC](https://discuss.elastic.co/t/beginswith-endswith-contains/5746/3 "2011-11-03T08:59:32Z")

</div>

Hi Karussell,

thanks for the hint of using multi-field-type.

But we are using elasticsearch schema free, so is it possible to define  
different analyzers more global than on a specific field? I am not sure if  
this is possible, at least I could not finde one example that does so in  
the documentation. There all mappings are done on field level.

Concerning the Edge NGram filter: I will try it and see how this influences  
performance and index size.

Concerning wildcard queries: Is there now possibility to escape wildcard  
characters (e.g. search for "aa\*" finds "aa\*")?

Regards,  
Daniel

---

<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 6, 2017, 3:49am UTC](https://discuss.elastic.co/t/beginswith-endswith-contains/5746/4 "2017-07-06T03:49:58Z")

</div>


