Query string query: match string beginning with pattern?

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.


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,


I get results where BEGINNING is anywhere in the phrase.

What should I do?

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.

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?

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

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".

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