dongqing6661 2011-10-03 15:40
浏览 27
已采纳

根据关键字外观对搜索结果进行排名

I am creating a search function in PHP/mySQL.

I need to be able to search for several keywords in several fields, and then rank the results by how many keywords match, and where the keywords match (for example, it should rank higher for keywords appearing in the post_title when compared to the post_content, all other things being equal.

I then need it to fall back onto ascending post date, all other things being equal.

Ideally, I could do this in the mySQL query. What methods are available to me?

  • 写回答

1条回答 默认 最新

  • duandai2178 2011-10-03 17:21
    关注

    If you use MyISAM you can set a fulltext index on your text fields.

    See this question for a howto: Keyword search using PHP MySql?

    Now you can do a query like:

    SELECT 
      MATCH(`data`) AGAINST('word1 word2 word3') AS relevance  
      , field1
      , field2
    FROM table1
    WHERE MATCH(`data`) AGAINST('word1 word2 word3')
    ORDER BY relevance DESC
    

    You can repeat the MATCH AGAINST clause in your select, where it will give you a relevence number.
    You need to restate it in your where clause. But luckily you can use an alias in your group by, having and order by clauses.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示
  • ¥15 MATLAB有限差分法解一维边值问题
  • ¥200 内网渗透测试 横向渗透 Windows漏洞 Windows权限维持
  • ¥15 数据结构图的相关代码实现
  • ¥15 python中aiohttp.client_exceptions.ContentTypeError
  • ¥30 DeepLung肺结节检测生成最大froc值对应的epoch报错
  • ¥15 信号发生器如何将频率调大,步尽值改成10
  • ¥15 keil 5 编程智能家具,风扇台灯开关,人体感应等
  • ¥100 找一名渗透方面的专家