Anchored Regex on array of tags

Hi,
So I'm having a hard time getting my anchored regex to work the way I want on my array of tags (I disabled analyzing)

{
   "doc": {
      "mappings": {
         "info": {
            "properties": {
               "tags": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "query": {
                    "properties": {
                        "regexp": {
                            "properties": {
                                "tags": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        }
                    }
                }
         }
    }
}

Querying the tags without anchor works fine and returns what I expect which is all tags matching .linux.

GET /doc/info/_validate/query?explain
{
  "query": {
    "regexp": {
      "tags": ".*linux.*"
      }
    }
}

But say I have a tag "linux_x86" and a tag "linux", I need to anchor my regex to return only an exact match on ^linux$

GET /doc/info/_validate/query?explain
{
  "query": {
    "regexp": {
      "tags": "^linux$"
      }
    }
}

Which doesn't work (returns empty) on array of strings, the explain isn't really explicit about what it's doing, I'm guessing it's looking for an exact anchored match across all tags instead of anchored match per tag in the array...

Hopefully someone could explain why and help me finding a workaround

Thanks

I recently saw someone doing this in another thread and the comment from one of the ES team was that we don't apply anchors as Lucene always anchors them.

Take a look at Regexp support for matching at the beginning (^) and the end ($) for more

2 Likes

Thanks a lot ! You saved my day :wink:

Yeah I was creating a double anchor by adding it in the pattern. Works better without them.