Coroico / AdvSearch

Dynamic content search add-on for MODx Revolution that supports results highlighting, faceted search and search in custom packages
21 stars 14 forks source link

REGEXP in queryHook #63

Closed goldsky closed 10 years ago

goldsky commented 10 years ago

I am changing the REGEXP in https://github.com/Coroico/AdvSearch/blob/Development/core/components/advsearch/model/advsearch/advsearchhooks.class.php#L202

from

case 'MATCH':  // operator with exact matching between word1||word2||word3
    $condition = "({$classField} REGEXP '(^|\\\|)+{$val}(\\\||$)+' )";
    break;
case 'REGEXP': // operator with exact pattern matching. eg: ptrn= '%s[0-9]*'
    // MATCH is equivalent to ptrn =  '(^|\\\|)+%s(\\\||$)+'
    $ptrn = sprintf($ptrn, $val);
    $condition = "({$classField} REGEXP '{$ptrn}' )";
    break;

to be

case 'MATCH':  // operator with exact matching between word1||word2||word3
    $val = addslashes($val);
    $condition = "({$classField} REGEXP '{$val}' )";
    break;
case 'REGEXP': // operator with exact pattern matching. eg: ptrn= '%s[0-9]*'
    $val = addslashes($val);
    $ptrn = str_replace('%s', $val, $ptrn);
    $condition = "({$classField} REGEXP '{$ptrn}' )";
    break;

The MATCH in the second one looks simpler.

The sprintf in the REGEXP in the first one fails to process my case,

$asform = isset($_REQUEST['asform']) ? $_REQUEST['asform'] : '';
if (!empty($asform)) {
    $requests = json_decode($asform, 1);
}

if (isset($requests['sub-category[]']) && !empty($requests['sub-category[]'])) {
    $andConditions = array(
        'tv.category:MATCH' => 'sub-category'
    );
} elseif (isset($requests['catId']) && !empty($requests['catId'])) {
    $andConditions = array(
        'tv.category:REGEXP:([^-]%s|^%s){1}(-[0-9])?' => 'catId'
    );
}

against: TV data like this:

2||1-2||5-4||2-3||6-1
2-1||3||6-2||2||3-2
3||3-2||2-3
2-4
4-2||2

If that's accepted in all cases, i'll implement that. What do you think?

goldsky commented 10 years ago

or should I just add qhVersion as 1.3 ?