MorphiaOrg / MorphiaOrg/morphia

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

Open
#4,304 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
1.7k
Forks
449
Avg merge
7m
Merged PRs (30d)
10

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with RegexFilter creation and follow how the passed java.util.regex.Pattern is converted into the MongoDB regex query. Reproduce the example using Pattern.CASE_INSENSITIVE and verify that a case-insensitive match is returned and the resulting query includes the equivalent option, without requiring the (?i) workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.