简体   繁体   English

如何使用Doctrine_RawSql进行全文搜索和相关性排序

[英]How to use Doctrine_RawSql for a fulltext search and sorting by relevance

I'm trying to get fulltext searches to be sorted by relevance in a Doctrine_RawSql query. 我正在尝试全文搜索,以在Doctrine_RawSql查询中按相关性排序。

This code will perform the search: 此代码将执行搜索:

$q = new Doctrine_RawSql();

$q->select('{p.*}')
  ->from('cms_page p')
  ->where('match(p.content) against (?)', $user_query)
  ->addComponent('p', 'CmsPage p');

This will execute. 这将执行。 I would like the results to be sorted by relevance 我希望结果按相关性排序

The real sql would have to look something like: 真正的sql必须看起来像:

select 
  p.id, 
  match(p.content) against (?) as score 
from 
  cms_page as p
order by 
  score desc;

So I need to get that match ... against clause in the select... I think. 因此,我需要在select中获取匹配项...对抗子句。

My crapshoot guess at accomplishing this was: 我对完成此操作的猜测是:

$q->select("{p.id}, match({p.content}) against ('$escaped_user_query') as score")
  ->from('cms_page p')
  ->orderBy('score DESC')
  ->addComponent('p', 'CmsPage p');

That doesn't work. 那不行 Any pointers? 有指针吗?

Thanks in advance! 提前致谢!

According to the MySQL fulltext natural language search docs: 根据MySQL全文自然语言搜索文档:

When MATCH() is used in a WHERE clause, as in the example shown earlier, the rows returned are automatically sorted with the highest relevance first . 如前面的示例所示,在WHERE子句中使用MATCH()时,返回的行将自动按照相关性最高的顺序进行排序 Relevance values are nonnegative floating-point numbers. 相关性值是非负浮点数。 Zero relevance means no similarity. 零相关性意味着没有相似性。 Relevance is computed based on the number of words in the row, the number of unique words in that row, the total number of words in the collection, and the number of documents (rows) that contain a particular word. 关联性是根据行中的单词数,该行中唯一单词的数量,集合中单词的总数以及包含特定单词的文档(行)的数量来计算的。

Does this not work for you? 这对您不起作用吗?

You can read more about natural fulltext search in MySQL here. 您可以在此处阅读有关MySQL中自然全文搜索的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM