本文介绍了CakePHP全文搜索MySQL的评级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过,为了能够排序搜索结果,你可以这样查询MySQL:

I've read that, to be able to rank search results you may query MySQL like this:

SELECT * , 
MATCH (title, body) AGAINST ('$search') AS rating 
FROM posts 
WHERE MATCH (title, body) AGAINST ('$search') 
ORDER BY rating DESC

在CakePHP 2.X中有办法做到这一点吗?
另外,我需要这样做,同时分页。所以我想我需要为分页器写条件,而不是直接的'查询'。

Is there a way to do this in CakePHP 2.X?Also, I need to do this while paginating at the same time. So I think I would need to write condition for the paginator, not a direct 'query'.

感谢您的帮助!

推荐答案

好的,它花了我一些时间...因为,关键的问题是得到一个评级的结果匹配,这个查询的复杂部分是特定字段:

Ok, it took me some time... Since, the key issue was to get a rating on the resulting matches, the complicated part in this query was the specific field:

MATCH(标题,正文)AGAINST('$ search')AS评分

我想我应该把这个字段写在分页数组中的field选项中。
生成的代码如下:

I figured that I should just write that field in the "field" option, in the pagination array.The resulting code was the following:

    $this->paginate = array(
            'limit' => 15,
            'fields' => array('*', "MATCH (data) AGAINST ('$q') AS rating"),
            'conditions' =>  "MATCH(SearchIndex.data) AGAINST('$q' IN BOOLEAN MODE)",
            'order' => array(
                'rating' => 'desc',
            ),
    );
    $paginatedResults = $this->paginate('SearchIndex');

这样无缝工作!

认为这是使用Cake实现真正搜索结果的最佳方式。除非有更好的替代方案:)

I think this is the best way to achieve real search results using Cake. Unless someone has a better alternative :)

在双引号之间搜索短语会给你应有的结果!

Searching phrases in between double quotes will give you the results you should expect!

这篇关于CakePHP全文搜索MySQL的评级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:41