Is there a way by default to require ALL words in full text searching?

69 Views Asked by At

I'm trying to find a way to make it so when users do a search that by default ALL words are required.

This seemed easy enough at the beginning, just break the words up and add a + sign to the start of each word; however when you start to try and implement the other operators it get's complicated.

This is what I have so far..

function prepareKeywords($str) {

    // Remove any + signs since we add them ourselves
    // Also remove any operators we don't allow
    // We don't allow some operators as they would have no use in the search since we don't order our results by relevance
    $str = str_replace(array('+','~','<','>'), '', $str);

    // Remove anything more than once space
    $str = preg_replace('/\s{2,}/', ' ', $str);

    // Now break up words into parts
    $str = str_getcsv($str, ' ', '"');

    // Now prepend a + sign to the front of each word

    foreach ($ks as $key => $word) {

        // First we check to make sure the start of the word doesn't already contain an operator before we add the + sign to the start

        if (in_array($word{0}, array('-','<','>','~'))) {

        } else {
            $ks[$key] = '+' . $word;   
        }

    }

    // Now put word back to string
    //$ks = implode(' ', $ks);    

}

As you can see it only gets so far at the moment, respects quoted strings, but then I start thinking about not breaking up () as well, and then what if that contains nested double quotes or vice versa.... it starts getting very hairy.

So I'm trying to figure out if there is a way that I can do what I want without messing with the string and just making all words required by default UNLESS the user specifically negates them with a -.

1

There are 1 best solutions below

1
markdwhite On

Surely you can make use of preg_match() and \b in the pattern as the word boundary?

You search terms can then be split with something like

preg_match_all('/\b(.*)\b/', $matches);

I might be on the wrong thinking as it's late here, but it might give you something to go on