簡體   English   中英

在mysql表中搜索5個關鍵字

[英]Search 5 keywords in mysql table

我正在嘗試從用戶在MySQL表中輸入的標簽中搜索關鍵字,並根據匹配數返回最佳結果。

碼:

MySQL結構:

 id    | keywords       | phrase
 1     | apple king pearl   | I was eating an apple when the king hit me
 2     | brush brute fancy  | you fancy this brush?   
 3     | king queen kingdom | shall the queen obey the king or the kingdom?

PHP:

 $keywords_raw='me wall like king apple'   //define keywords based on the tags the user inputs
 $keywords=explode(' ', $keywords_raw);

....那就是我被困住的地方。 我的想法是:

  1. 搜索將針對每個關鍵字(例如“ me”,“ wall”,“ like”等)執行

  2. 對於每個關鍵字,它將在表的每一行中搜索“關鍵字”和“短語”兩列,並返回找到的匹配項數。 例如,搜索針對第一行輸入的關鍵字將返回關鍵字“ me”具有0個匹配項,“ wall”具有0個匹配項,“ like” 0,“ king” 2和“ apple” 2。因此,總匹配項將為2 +2 = 4

  3. 最后,比較在所有行中找到的匹配總數,然后選擇匹配度最高的前3行。

第二個問題是如何忽略包含搜索關鍵字的單詞,例如包含“ king”但又是另一個單詞的“ kingdom”。


更新:

按照有用的答案,我使用了全文搜索。

    $keywords='bb';

    $data['recommendation']=$this->db->query
    ("SELECT *, MATCH(keywords, phrase) AGAINST ('$keywords') as score 
    FROM game
    WHERE MATCH(keywords, phrase) AGAINST ('$keywords') 
    ORDER BY score 
    LIMIT 3");

    var_dump($data['recommendation']);
    die;

由於某種原因,var_dump返回空結果,未找到任何行。 但是我在下表的至少兩行中確實有短語“ bb”。

 id    | keywords       | phrase
 1     | bb king        | I was eating an apple when bb the king hit me
 2     | bb             | you fancy this brush?   

正如Barmar所說,您可以使用全文功能:

SELECT id, customer_id, phrase 
FROM table
WHERE MATCH(phrase) AGAINST ('me wall like king apple');

如果也需要搜索另一列,則將其添加到MATCH

SELECT id, customer_id, phrase 
FROM table
WHERE MATCH(phrase,keywords) AGAINST ('me wall like king apple');

編輯:

對於第3點,您可以使用相同的功能:

SELECT id, customer_id, phrase, MATCH(phrase,keywords) AGAINST ('me wall like king apple') as score 
FROM table
WHERE MATCH(phrase,keywords) AGAINST ('me wall like king apple') 
ORDER BY score 
LIMIT 3;

此查詢將返回三個最佳匹配

有關更多信息,請參閱手冊

更新:

根據手冊:

在全文搜索中,某些單詞會被忽略:

  • 任何太短的單詞都會被忽略。 全文搜索發現的默認最小單詞長度為四個字符。

  • 停用詞列表中的單詞將被忽略。 停用詞是一個非常普遍的詞,例如“ the”或“ some”,以至於它被認為具有零語義值。 有一個內置的停用詞列表,但是可以由用戶定義的列表覆蓋。

您正在使用一個簡短的單詞進行搜索,這就是為什么您沒有得到任何結果的原因。

更新09-09-14:

文檔

如果您修改影響索引的全文變量( ft_min_word_lenft_max_word_lenft_stopword_file ),或者更改了停用詞文件本身,則必須在進行更改並重新啟動服務器后重新FULLTEXT索引。 在這種情況下,要重建索引,只需執行QUICK修復操作即可:

mysql> 修復表tbl_name快速;

因此,在執行ALTER TABLE table ADD FULLTEXT(phrase, keywords); 您必須執行REPAIR TABLE tbl_name QUICK; 只有一次

  $keywords=explode(' ', $keywords_raw);
  if(count($keywords)>1)
                 {
                     $stmt.="(";
                     $i=0;
                     foreach($arr_s1 as $kv)
                     {
                        if($i>0)
                        {
                                $stmt.=" AND ";
                        }
                        $stmt.=" phrase like '%".$kv."%'";                       
                        $i++;
                     }
                     $stmt.=")";                    

                 }

希望它對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM