简体   繁体   English

全文mysql搜索并匹配确切的字符串

[英]fulltext mysql search and match exact string

Ok this is doing my grey matter in I've read and been through mysql and looked at various options and I can only think its so obvious I'm just blind. 好的,这是我读过并通过mysql并查看了各种选项的灰色问题,我只能认为它是如此明显,我只是盲目。

In short i'm making a tag cloud and part of the process is to verify if the word exists and if so +1 but it has to be the exact phrase. 简而言之,我正在制作标签云,该过程的一部分是验证单词是否存在,如果存在,则+1,但它必须是确切的短语。

So I took the view that fulltext search would be the best option. 因此,我认为全文搜索将是最佳选择。

So as an example. 举个例子。

$string = "hair dryer";

$string = '"'.$string.'"'; //enclose string in quotation marks to define must be an exact match



 //$string = mysqli_real_escape_string($mysqli,$string); // removed to see if this was effecting the result.

 $sql = "SELECT * FROM `search_keyword` WHERE Match (`keyword`) Against ('$string' IN BOOLEAN MODE) LIMIT 1";
 $result=mysqli_query($mysqli,$sql);

So the test was in the DB I have a line with the word "hair dryer" and it matches it - good result 因此,测试是在数据库中,我在一行中加上了“吹风机”一词,并且与之匹配-很好的结果

However if I change the string to "hair" it still pulls out "hair dryer" which it shouldn't do as its not an exact match. 但是,如果我将字符串更改为“ hair”,它仍然会拉出“ hairdryer”,这不应该做,因为它不完全匹配。

I've read all over and every way I try this I get the wrong result. 我已经阅读了所有内容,并且尝试每种方式都得到了错误的结果。

(
[0] => Array
    (
        [keyword] => hair dryer
        [id] => 25
    )

 )  /// should be empty and hair as a single row is not found

Please any advice is welcomed :-) 请任何建议,欢迎:-)

You could do it with php. 你可以用PHP做到这一点。 First transform your string into an array and then compare both arrays to see if there is a match. 首先将您的字符串转换为数组,然后比较两个数组以查看是否存在匹配项。

      $string = explode(" ",$string); 
      $sql = "SELECT keyword FROM search_keyword";
     $result=mysqli_query($mysqli,$sql);
$compareArrays = array_intersect($result,$string);
       if (is_array($compareArrays) && count($compareArrays) > 0) {

Do something with the result as if $compareArrays is greter than one, it means you have an exact match. 对结果执行某些操作,好像$ compareArrays比1更出色,这意味着您具有完全匹配的内容。 array_intersect compares 2 arrrays to see if there is an exact match. array_intersect比较两个错误,以查看是否存在完全匹配。 This way I don't think you would get a match with 'hair' as a string but you would get one with 'hair dryer'. 这样,我认为您不会以“ hair”作为字符串来匹配,但是会以“ hairdryer”来匹配。 Does this work? 这样行吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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