Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search query string regex

I am having an issue querying an field (title) using query string regex.

This works: "title:/test/"
This does not : "title:/^test$/"

However they mention it is supported https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax

My goal it to do exact match, but this match should not be partial, it should match the whole field value.

Does anybody have an idea what might be wrong here?

like image 629
Aleksey Savitsky Avatar asked Jun 17 '26 23:06

Aleksey Savitsky


1 Answers

From the documentation

The Lucene regular expression engine is not Perl-compatible but supports a smaller range of operators.

You are using anchors ^ and $, which are not supported because there is no need for that, again from the docs

Lucene’s patterns are always anchored. The pattern provided must match the entire string

If you are looking for the phrase query kind of match you could use double quotes like this

{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "\"test phrase\""

    }
  }
}

but this would also match documents with title like test phrase someword

If you want exact match, you should look for term queries, make your title field mapping "index" : "not_analyzed" or you could use keyword analyzer with lowercase filter for case insensitive match. Your query would look like this

{
  "query": {
    "term": {
      "title": {
        "value": "my title"
      }
    }
  }
}

This will give you exact match

like image 96
ChintanShah25 Avatar answered Jun 19 '26 13:06

ChintanShah25



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!