# Query string query: match string beginning with pattern?

**URL:** <https://discuss.elastic.co/t/query-string-query-match-string-beginning-with-pattern/57716>\
**Category:** Elasticsearch\
**Created:** [August 10, 2016, 3:12pm UTC](https://discuss.elastic.co/t/query-string-query-match-string-beginning-with-pattern/57716 "2016-08-10T15:12:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![twigbranch](https://avatars.discourse-cdn.com/v4/letter/t/2bfe46/32.png) [@twigbranch](https://discuss.elastic.co/u/twigbranch)\
**Post date:** [August 10, 2016, 3:12pm UTC](https://discuss.elastic.co/t/query-string-query-match-string-beginning-with-pattern/57716/1 "2016-08-10T15:12:13Z")

</div>

I have an analyzed filed containing various phrases. I'm running query\_string queries against that field and would like to find records where the string begins with a certain pattern. I was attempting to do this via regular expression.

/^BEGINNING.\*/

but the lucene engine does not support anchors, so this does not work. Elasticsearch documention about regular expressions indicate patterns are already anchored, but when I do,

/BEGINNING.\*/

I get results where BEGINNING is _anywhere_ in the phrase.

What should I do?

---

<div class="post-metadata">

**Author:** ![abeyad](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@abeyad](https://discuss.elastic.co/u/abeyad)\
**Post date:** [August 10, 2016, 10:16pm UTC](https://discuss.elastic.co/t/query-string-query-match-string-beginning-with-pattern/57716/2 "2016-08-10T22:16:55Z")

</div>

In the mapping definition for the field you are searching against, you would either want to set it to `not_analyzed` or if you need some analysis like lower-casing, you could add something like this to your index settings:

`"analyzer":{ "lowercase_analyzer": { "tokenizer":"keyword", "filter":"lowercase" } }`

and then in your mappings definition, set the analyzer to `lowercase_analyzer`.

---

<div class="post-metadata">

**Author:** ![twigbranch](https://avatars.discourse-cdn.com/v4/letter/t/2bfe46/32.png) [@twigbranch](https://discuss.elastic.co/u/twigbranch)\
**Post date:** [August 11, 2016, 1:32pm UTC](https://discuss.elastic.co/t/query-string-query-match-string-beginning-with-pattern/57716/3 "2016-08-11T13:32:15Z")

</div>

Humm... I'm currently using the snowball analyzer. If I want to continue this, but also have the ability to use anchored regexp, then would I need to also index the field as not\_analyzed, and when someone inputs a reg expression with "/ /" run the query against the not\_analyzed version?

---

<div class="post-metadata">

**Author:** ![abeyad](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@abeyad](https://discuss.elastic.co/u/abeyad)\
**Post date:** [August 11, 2016, 5:53pm UTC](https://discuss.elastic.co/t/query-string-query-match-string-beginning-with-pattern/57716/4 "2016-08-11T17:53:17Z")

</div>

That's correct, and you can combine the analyzed and non-analyzed versions of the field into more sophisticated queries using multi-match: [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html)

---

<div class="post-metadata">

**Author:** ![davidbkemp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidbkemp/32/594_2.png) [@davidbkemp](https://discuss.elastic.co/u/davidbkemp)\
**Post date:** [August 12, 2016, 8:39am UTC](https://discuss.elastic.co/t/query-string-query-match-string-beginning-with-pattern/57716/5 "2016-08-12T08:39:46Z")

</div>

Another trick that might work is to insert a marker string at the beginning of each phrase at index time and include that marker at the beginning of your phrase queries. Given that character filters act before the text is tokenised, you might be able to use a pattern replace character filter to insert the marker string for you. Ie, replace /^(.\*)/ with "aaa $1".  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html)

I haven't tried it, and so can't give any guarantees

---

<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 5, 2017, 10:28pm UTC](https://discuss.elastic.co/t/query-string-query-match-string-beginning-with-pattern/57716/6 "2017-07-05T22:28:06Z")

</div>


