如何在使用案例PDO全文搜索时按相关性排序

时间:2014-03-13 02:20:33

标签: php mysql pdo full-text-search

我在以下几个案例中有一个查询:

$search = $_GET['search'];


$array = $pdo->prepare("SELECT * , CASE 
     WHEN CHAR_LENGTH(index.product_description) > 32 THEN CONCAT(SUBSTRING(index.product_description, 1, 32), '...') ELSE index.product_description
   END AS product_description, `product_title`, CASE 
     WHEN CHAR_LENGTH(index.product_title) > 18 THEN CONCAT(SUBSTRING(index.product_title, 1, 18), '...')
     ELSE index.product_title
   END AS product_title from `index` where MATCH (`product_title`) AGAINST (:search IN BOOLEAN MODE) and `category_name` = 'category1' LIMIT :start, :limit");
$array->execute(array(':search' => $search, ':start' => $start, ':limit' => $limit));

我想按相关性添加订单,我在不使用案例的情况下单独工作:

$search = $_GET['search'];
$search2 = $_GET['search'];


$array = $pdo->prepare("SELECT * , MATCH (`product_title`) AGAINST (:search2 IN BOOLEAN MODE) AS relevance 
FROM `index` where MATCH (`product_title`) AGAINST (:search IN BOOLEAN MODE) and `category_name` = 'category1' order by relevance DESC LIMIT :start, :limit");
$array->execute(array(':search' => $search, ':search2' => $search2, ':start' => $start, ':limit' => $limit));

我尝试添加:

SELECT * , MATCH (`product_title`) AGAINST (:search2 IN BOOLEAN MODE) AS relevance 
FROM `index`

在案件之前和之后它似乎没有用。

在第一个查询中,我会按相关性添加订单,或者在使用案例时应该如何构建?

由于

1 个答案:

答案 0 :(得分:0)

您需要order by子句按相关性排序。您可以将MATCH语句直接放在order by

SELECT index.*,
       (CASE WHEN CHAR_LENGTH(index.product_description) > 32
             THEN CONCAT(SUBSTRING(index.product_description, 1, 32), '...')
             ELSE index.product_description
        END) AS product_description, `product_title`,
       (CASE WHEN CHAR_LENGTH(index.product_title) > 18
             THEN CONCAT(SUBSTRING(index.product_title, 1, 18), '...')
             ELSE index.product_title
        END) AS product_title
from `index`
where MATCH (`product_title`) AGAINST (:search IN BOOLEAN MODE) and `category_name` = 'category1'
order by MATCH (`product_title`) AGAINST (:search IN BOOLEAN MODE) desc
LIMIT :start, :limit;