如何使用MySQL MATCH AGAINST在BOOLEAN MODE中返回关键字字符串中至少1个匹配关键字的结果?

时间:2022-09-19 17:57:41

I have this query for searching database entries based on keywords entered on a search box.

我有这个查询,用于根据搜索框中输入的关键字搜索数据库条目。

// function to get search keywords from url
$q = furl($_GET['q']);

$sExp = preg_split('/\s+/',$q); 
$secure_keywords = array(); 
foreach ($sExp as $key=>$keyword){
    if (strlen($keyword) >= 3){ 
    // cut off words that are less than 3 chars
    $secure_keywords[] = $keyword;
    }
}
$kwords = count($secure_keywords); 
foreach ($secure_keywords as $key=>$keyword) 
{ 
// function to prevent sql injection
$keyword = sql_proof($keyword); 

$query="SELECT * FROM listings WHERE MATCH (meta_keywords) AGAINST ('$keyword' IN BOOLEAN MODE) ORDER BY id"; 
} 

When 2 or more words are used, if at least 1 word is misspelled then the query will not return any results (even if results do exist for the rest of the words in the keywords entered). For example, I enter the word "good" and I get at least 1 result, but if I use "good point" the query doesn't return any results, so it totally ignores the matching results for the word "good". Is there a way for the query to match results if there is at least 1 matching word in the keywords entered? Thanks

当使用2个或更多单词时,如果拼写错误至少1个单词,则查询将不会返回任何结果(即使输入的关键字中的其余单词确实存在结果)。例如,我输入单词“good”并且我得到至少1个结果,但如果我使用“good point”,则查询不返回任何结果,因此它完全忽略单词“good”的匹配结果。如果输入的关键字中至少有1个匹配的单词,是否有一种方法可以使查询匹配结果?谢谢

2 个解决方案

#1


0  

Instead of using 'In boolean mode' try

而不是使用'在布尔模式'尝试

SELECT * FROM articles WHERE MATCH (title, body) AGAINST ('database test')

It returns result even if you have no word like 'test' after database

即使在数据库之后没有像'test'这样的单词,它也会返回结果

SELECT * FROM tablename WHERE MATCH (col1, col2) AGAINST ('string to be searched')

#2


0  

Check out the below solution for a way to match the results against ANY of the keywords used. Also check the docs for more info on operators and boolean fst http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

请查看以下解决方案,以便将结果与所使用的任何关键字进行匹配。另请查看文档以获取有关运算符和boolean fst的更多信息http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

// function to get search keywords from url
$q = furl($_GET['q']);

$sExp = preg_split('/\s+/',$q); 
$secure_keywords = array(); 
foreach ($sExp as $key=>$keyword){
    if (strlen($keyword) >= 3){ 
    // cut off words that are less than 3 chars
    $secure_keywords[] = $keyword;
    }
}
$kwords = count($secure_keywords); 

//init empty keywords array
$keywords = array();

//secure all keywords
$keywords = array_map(function($item){ return sql_proof($item); }, $secure_keywords); 

//implode the words with a space between
$keywords = implode(" ",$keywords);

//just one query with match against ANY of the words from initial keyword array
$query="SELECT * FROM listings WHERE MATCH (meta_keywords) AGAINST ('$keywords' IN BOOLEAN MODE) ORDER BY id"; 

#1


0  

Instead of using 'In boolean mode' try

而不是使用'在布尔模式'尝试

SELECT * FROM articles WHERE MATCH (title, body) AGAINST ('database test')

It returns result even if you have no word like 'test' after database

即使在数据库之后没有像'test'这样的单词,它也会返回结果

SELECT * FROM tablename WHERE MATCH (col1, col2) AGAINST ('string to be searched')

#2


0  

Check out the below solution for a way to match the results against ANY of the keywords used. Also check the docs for more info on operators and boolean fst http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

请查看以下解决方案,以便将结果与所使用的任何关键字进行匹配。另请查看文档以获取有关运算符和boolean fst的更多信息http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

// function to get search keywords from url
$q = furl($_GET['q']);

$sExp = preg_split('/\s+/',$q); 
$secure_keywords = array(); 
foreach ($sExp as $key=>$keyword){
    if (strlen($keyword) >= 3){ 
    // cut off words that are less than 3 chars
    $secure_keywords[] = $keyword;
    }
}
$kwords = count($secure_keywords); 

//init empty keywords array
$keywords = array();

//secure all keywords
$keywords = array_map(function($item){ return sql_proof($item); }, $secure_keywords); 

//implode the words with a space between
$keywords = implode(" ",$keywords);

//just one query with match against ANY of the words from initial keyword array
$query="SELECT * FROM listings WHERE MATCH (meta_keywords) AGAINST ('$keywords' IN BOOLEAN MODE) ORDER BY id";