0

I'm trying to do full-text search in MongoDB with $searchBeta (aggregation) and I'm using the 'regex' operator to do so. Here's the portion of the $searchBeta I have that isn't working how I expecting it would:

$searchBeta: {
    regex: {
        query: '\blightn', // '\b' is the word boundary metacharacter
        path: ["name", "set_name"],
        allowAnalyzedField: true
    }
}

Here's an example of two documents that I'm expecting to get matched by the expression:

{
    "name": "Lightning Bolt"
    "set_name": "Masters 25"
},
{
    "name": "Chain Lightning",
    "set_name": "Battlebond"
}

What I actually get:

[] //empty array

If I use an expression like:

$searchBeta: {
    regex: {
        query: '[a-zA-Z]'
        path: ["name", "set_name"],
        allowAnalyzedField: true
    }
}

then I get results back.

I can't get any expression that has regex metacharacters and/or options in it to work, so I'm pretty sure I'm just entering it wrong in my query string. The $searchBeta regex documentation doesn't really cover how to format metacharacters into your query string. Also, the $searchBeta regex operator is different from $regex because it doesn't require slashes (i.e. "/your expression/" ). Really pulling my hair out on something so simple that I can't figure out.

Doug
  • 14,387
  • 17
  • 74
  • 104
chataolauj
  • 99
  • 1
  • 15

1 Answers1

0

$searchBeta uses Lucene for regular expressions, which is not Perl Compatible (PCRE) and doesn't support \b. You can read about the Lucene regex syntax here and also Elastic's docs on it are also helpful.

Here is a similar question for ElasticSearch and includes some workarounds.

Doug
  • 14,387
  • 17
  • 74
  • 104