无法使用全文搜索返回完全匹配?

时间:2014-10-16 09:16:46

标签: mysql full-text-search

我有2张桌子是歌和歌词。现在我使用全文搜索来搜索他们想要的歌曲或歌词。但是完全匹配并没有出现在第一个结果中。

示例:我想搜索歌曲或歌词名称" Love and Let Love" 。但它会像这样回归:

        [0] => Array
            (
                [song_title] => This Is Love
                [type] => songs
                [point] => 3.9282567501068115
            )

        [1] => Array
            (
                [song_title] => I Will Love Again 2014
                [type] => songs
                [point] => 3.8840975761413574
            )

        [2] => Array
            (
                [song_title] => A Love I Think Will Last
                [type] => lyric
                [point] => 3.811438798904419
            )

        [3] => Array
            (
                [song_title] => Love and Let Love
                [type] => lyric
                [point] => 3.811438798904419
            )

这是我的代码:

public function searchSongs2($keyword, $start, $limit,$deleted=0)
{
    $sql = " (SELECT s.song_title, 'songs' as type ,MATCH(s.song_title) AGAINST ('".$keyword."' IN NATURAL LANGUAGE MODE) as point
              FROM song s
              JOIN category_song cs ON cs.song_id = s.song_id
              JOIN category c ON c.category_id = cs.category_id
              WHERE s.song_type ='publish' AND MATCH(s.song_title) AGAINST ('".$keyword."' IN NATURAL LANGUAGE MODE) 
              GROUP BY s.song_title  )  
       UNION
              (SELECT l.lyric_name, 'lyric' as type  ,MATCH(l.lyric_name) AGAINST ('".$keyword."' IN NATURAL LANGUAGE MODE) as point
              FROM ml_lyric l
              JOIN ml_singer s ON s.singer_id = l.singer_id
              JOIN ych_category_lyric cl ON cl.lyric_id = l.lyric_id
              JOIN ych_category c ON c.category_id = cl.category_id
              WHERE MATCH(l.lyric_name) AGAINST ('".$keyword."' IN NATURAL LANGUAGE MODE)
          GROUP BY l.lyric_name  ) 
  ORDER BY point DESC  LIMIT ".$start.",".$limit;
    return Yii::app()->db->createCommand($sql)->queryAll();
}

有没有让字符串的人总是在第一名搜索?

1 个答案:

答案 0 :(得分:1)

已编辑,因此确实有效:

可能不是最优雅,但只是经过测试,它的确有效。我确定其他人可以稍微改善它。

$myarray = array(
         array
            (
                'song_title' => 'This Is Love',
                'type' => 'songs',
                'point' => '3.9282567501068115'
            ),

        array
            (
                'song_title' => 'I Will Love Again 2014',
                'type' => 'songs',
                'point' => '3.8840975761413574'
            ),

        array
            (
                'song_title' => 'A Love I Think Will Last',
                'type' => 'lyric',
                'point' => '3.811438798904419'
            ),

        array
            (
                'song_title' => 'Love and Let Love',
                'type' => 'lyric',
                'point' => '3.811438798904419'
            ),
     );
$new_array = array();
foreach ( $myarray as $key => $value ) {
   if ( $value['song_title'] == 'Love and Let Love' ) {
        $new_array[0]['song_title'] = $value['song_title'];
        $new_array[0]['type'] = $value['type'];
        $new_array[0]['point'] = $value['point'];
                unset($myarray[$key]);
    }
}
     $new_array = array_merge($new_array, $myarray);
    print_r ( $new_array );

返回:

Array
(
    [0] => Array
        (
            [song_title] => Love and Let Love
            [type] => lyric
            [point] => 3.811438798904419
        )

    [1] => Array
        (
            [song_title] => This Is Love
            [type] => songs
            [point] => 3.9282567501068115
        )

    [2] => Array
        (
            [song_title] => I Will Love Again 2014
            [type] => songs
            [point] => 3.8840975761413574
        )

    [3] => Array
        (
            [song_title] => A Love I Think Will Last
            [type] => lyric
            [point] => 3.811438798904419
        )

)