Skip to content

java.util.regex.Pattern flags (for example CASE_INSENSITIVE) are ignored when RegexFilter is created #4304

Description

@per-johnsson-aurora

Describe the bug

When creating a RegexFilter, flags from passed java.util.regex.Pattern are ignored.

To Reproduce

Create a query with a RegexFilter created from a java.regex.Pattern with CASE_INSENSITIVE flag set, for example:

    Query<Unit> query = datastore.find(Unit.class);
    String patternString = String.format(".*%s.*", searchString);
    Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
    query.filter(regex("name", pattern));

Perform the query with a string matching case-insensitively but not case-sensitively.

Expected behavior

Entity (matching case-insensitively) is returned.

Please complete the following information:

  • Server Version: 4.4.28
  • Driver Version: 5.6.1
  • Morphia Version: 2.5.1

Additional context

In Morphia 1.3.2 the corresponding filter creation

    Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
    query.filter("name", pattern);

added case-insensitive option to regex in resulting query.

{ query: {"name": {"$regex": ".*any.*", "$options": "i"}} }

Workaround

Add case-insensitivity to the pattern string by prepending (?i).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions