Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 无法使用全文搜索返回精确匹配?_Mysql_Full Text Search - Fatal编程技术网

Mysql 无法使用全文搜索返回精确匹配?

Mysql 无法使用全文搜索返回精确匹配?,mysql,full-text-search,Mysql,Full Text Search,我有两张桌子是歌曲和歌词。现在我使用全文搜索来搜索人们想要的歌曲或歌词,但第一次搜索结果中并没有出现精确的匹配 示例:我想搜索歌曲或歌词名称“Love and Let Love”。但它是这样返回的: [0] => Array ( [song_title] => This Is Love [type] => songs [point] =>

我有两张桌子是歌曲和歌词。现在我使用全文搜索来搜索人们想要的歌曲或歌词,但第一次搜索结果中并没有出现精确的匹配

示例:我想搜索歌曲或歌词名称“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();
}

还有什么方法可以让人们搜索的字符串始终排在第一位吗?

经过编辑,它实际上可以工作:

可能不是最优雅的,但经过测试,它可以工作。我相信其他人可以把它装饰一下

$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
        )

)

尝试从自然语言模式更改为布尔模式这与您的SQL查询有关,而不是Yii和PHP,我删除了它们的标记