regexmongodblaravel-5robo3tjenssegers-mongodb

How to apply regex properly in Jenssegers raw() function


I tried to implement a diacritic insensitive full word search in one of my application. I wrote this query and is working fine in the MongoDB terminal (I used Robo3T).

[ Here I passed the Unicode conversion of the word 'Irène' ]

db.getCollection('rvh_articles').aggregate([
  {
    "$match":{
       "art_xml_data.article.article_title":{
          "$regex":/( |^)[i\x{00ec}\x{00ed}\x{00ee}\x{00ef}]r[e\x{00e8}\x{00e9}\x{00ea}\x{00eb}\x{00e6}][n\x{00f1}][e\x{00e8}\x{00e9}\x{00ea}\x{00eb}\x{00e6}]( |$)/,
          "$options":"I"
       }
    }
  }
])

When I tried to implement this query in jenssegers raw() function, I wrote a PHP function to build a regular expression corresponding to the search string. Which will convert each letter in the string to the corresponding Unicode and returns the regular expression.

public function makeComp($input) 
{
    $accents = array(
        /*
            I include json_encode here because:
            json_encode used in the jenssegers building query function converts diacritic charectes to 
            hexadecimal(\u). But '\u' is not supported with regex mongodb. It shows this error:
            "Regular expression is invalid: PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u"

            So I first used json_encode for each string conversion and then replaced '{\u' with '{\x'. Problem solved.
        */
        "a" => json_encode('[a{à}{á}{â}{ã}{ä}{å}{æ}]'),
        "c" => json_encode('[c{ç}]'),
        "e" => json_encode('[e{è}{é}{ê}{ë}{æ}]'),
        "i" => json_encode('[i{ì}{í}{î}{ï}]'),
        "n" => json_encode('[n{ñ}]'),
        "o" => json_encode('[o{ò}{ó}{ô}{õ}{ö}{ø}]'),
        "s" => json_encode('[s{ß}]'),
        "u" => json_encode('[u{ù}{ú}{û}{ü}]'),
        "y" => json_encode('[y{ÿ}]'),
    );
    $out = strtr($input, $accents); // replacing all possible accented characters in the input string with $accents array key value
    $out = str_replace('{\u', '\x{', $out); // replace all {\u to \x{ because PCRE does not support the \uXXXX syntax. Use \x{XXXX}.
    $out = str_replace('"', "", $out); // replace all double quotes
    return '/( |^)' . $out . '( |$)/';
}

Here is the function that I applied the MongoDB query in jenssegers raw() function.

public function getall_articles(Request $request)
{
    extract($request->all());

    if (!empty($search_key)) {
        DB::connection()->enableQueryLog();

        $search_key = $this->makeComp($search_key);

        $data = Article::raw()->aggregate([
            array(
                '$match' => array(
                    "art_xml_data.article.article_title" => array(
                        '$regex' => $search_key,
                        '$options' => 'i'
                    )
                )
            )
        ])->toArray();

        dd(DB::getQueryLog());
    }
}

This is the query log printed:

array:1 [
    0 => array:3 [
        "query" => rvh_articles.aggregate([{
            "$match":{
                "art_xml_data.article.article_title":{
                    "$regex":"\/( |^)[i\\x{00ec}\\x{00ed}\\x{00ee}\\x{00ef}]r[e\\x{00e8}\\x{00e9}\\x{00ea}\\x{00eb}\\x{00e6}][n\\x{00f1}][e\\x{00e8}\\x{00e9}\\x{00ea}\\x{00eb}\\x{00e6}]( |$)\/",
                    "$options":"i"
                }
            }
        }])
        "bindings" => []
        "time" => 620.14
    ]
]

The regular expression that I applied is not placed as it is. So the mongo returns zero results. Can anyone help me to solve this issue? I need an alternative solution to apply diacritic insensitive and case insensitive search using jenssegers raw() function.


Solution

  • In your public function makeComp($input) method, you need to use

    return '(?<!\S)' . $out . '(?!\S)';
    

    In case $out can (potentially, in future) contain multiple alternatives separated with |, you should group the pattern,

    return '(?<!\S)(?:' . $out . ')(?!\S)';
    #              ^^^            ^
    

    Note that (?<!\S) is a left-hand whitespace boundary that matches a location not immediately preceded with a non-whitespace char, and (?!\S) is a right-hand whitespace boundary that matches a location not immediately followed with a non-whitespace char.