0

我有一个查询根据与搜索词的相关性提取搜索结果:

SELECT *, 
( (1.3 * (MATCH(strTitle) AGAINST ('+john+smith' IN BOOLEAN MODE))) + (0.6 * (MATCH(txtContent) AGAINST ('+john+smith' IN BOOLEAN MODE)))) AS relevance 
FROM content 
WHERE (MATCH(strTitle,txtContent) AGAINST ('+john+smith' IN BOOLEAN MODE) ) 
ORDER BY relevance DESC

我还希望能够从描述中提取搜索匹配的第一个实例。因此,如果搜索“john smith”,我希望我的结果显示如下:

页面标题 1 ...与john smith匹配的片段...

页面标题 2 ...片段约翰匹配....

页面标题 3 ....匹配john的片段smith ...

等等等等

这怎么可能?

4

1 回答 1

1

您可以使用 case 语句执行此操作:

select *,
       ( (1.3 * (MATCH(strTitle) AGAINST ('+john+smith' IN BOOLEAN MODE))) + (0.6 * (MATCH(txtContent) AGAINST ('+john+smith' IN BOOLEAN MODE)))) AS relevance,
      (case when MATCH(strTitle) AGAINST ('+john+smith' IN BOOLEAN MODE)) > 0
            then 'snippet matching +john+smith'
            . . .
       end)

就个人而言,我会将匹配项作为单独的变量放在子查询中:

select
from (select t.*,
             MATCH(strTitle) AGAINST ('+john+smith' IN BOOLEAN MODE)) as match_join_smith,
             . . .
      from t
     ) t

然后将这些变量用于外部查询中的逻辑。

于 2013-02-22T15:04:51.910 回答