简体   繁体   中英

How to convert MySQL query MATCH/AGAINST to LIKE to get around Stop Word List

Scenario: I am searching a MySql table on two columns, ProductName and Description, in a specific category using this query:

$query = "SELECT * FROM Products WHERE MATCH(ProductName,Description) AGAINST ('+$terms[0]* +$terms[1]* +$terms[2]* +$terms[3]* +$terms[4]*' IN BOOLEAN MODE) AND category_description='".$search_dept."' OR MATCH(ProductName,Description) AGAINST ('+$terms[0]* +$terms[1]* +$terms[2]* +$terms[3]* +$terms[4]*' IN BOOLEAN MODE) AND category2='".$search_dept."' ORDER BY $orderby LIMIT $offset, $rowsPerPage";

The search criteria $terms[x] is from a text input field in a form and formatted using:

$slash_term = addslashes($_POST['term']);
$var = @$slash_term;
$trimmed = trim($var);
$terms = explode(' ',$trimmed);

The routine works great until you use a stop word, then obviously the query is killed.

I am on a shared server and cannot disable the stop word checking. So from what I have been researching, it may be possible to use LIKE and the % wildcard to get around this problem.

So how would I convert the above query to a LIKE query, I assume it would be something similar to the following, but it is not working.

    $query = "select * from Products where category_description='".$search_dept."' AND Description like \"%$trimmed%\" OR category_description='".$search_dept."' AND ProductName like \"%$trimmed%\" ORDER BY $orderby LIMIT $offset, $rowsPerPage";

And would I get each individual word from the search phrase wildcarded by just using %$trimmed%? Or is that even the way I should go about it? The search will almost always contain multiple words.

Since the first query seems to work almost flawlessly, would it be worthwhile for me to just add a subroutine that checks the user's input for stop words and remove them from the phrase before searching it?

Ok so here's my solution. I check for and remove any stop words from the user's input then proceed with the original search query. Works perfectly.

// format user's input
$slash_term = addslashes($_POST['term']);
$var = @$slash_term;
$trimmed = trim($var);
$terms = explode(' ',$trimmed);
// check for stop words and remove
$stop_words_file  = "list-of-english-stop-words.txt"; // load stop words file
$contents = addslashes(file_get_contents($stop_words_file)); // escape special characters
$stop_words = explode(',', $contents); // create array
foreach($terms as $key => $value) { // search user input for stop words
    if(in_array($value, $stop_words)) { // stop word found
        unset($terms[$key]); // remove it from array
    }
}
$terms = array_values($terms); // remove empty/NULL values from array
// perform search
$query = "SELECT * FROM Products WHERE MATCH(ProductName,Description) AGAINST ('+$terms[0]* +$terms[1]* +$terms[2]* +$terms[3]* +$terms[4]*' IN BOOLEAN MODE) AND category_description='".$search_dept."' OR MATCH(ProductName,Description) AGAINST ('+$terms[0]* +$terms[1]* +$terms[2]* +$terms[3]* +$terms[4]*' IN BOOLEAN MODE) AND category2='".$search_dept."' ORDER BY $orderby LIMIT $offset, $rowsPerPage";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM