智能mysql搜索查询

时间:2015-01-28 21:56:43

标签: mysql string search full-text-search

我有一个mysql InooDB 表,其中包含以下列 id, title 。我已经 70.000 行并且正在增加。

您将在下面找到一些示例行。当我搜索exp。 " 大汉堡"这就是我希望它按顺序找到的东西。在这种情况下,id 5不会包含在结果中

我的订单逻辑不好吗?是否有一些资源可以帮助我?我该怎么做?

+------+--------------------------+-----+------------------------------------+
|id    |title                     |order|explanation                         |
+----------------------------------------------------------------------------+
|1     |burger place              |  6  |has the second word in search       |
+----------------------------------------------------------------------------+
|2     |big burgers               |  1  |This is what is searched            |
+----------------------------------------------------------------------------+
|3     |burgers big               |  4  |identical just word order different |
+----------------------------------------------------------------------------+
|4     |big city hall             |  5  |has the first word in search        |
+----------------------------------------------------------------------------+
|5     |red offices               |  /  |not included in results             |
+----------------------------------------------------------------------------+
|6     |johns big burgers         |  3  |has what was searched               |
+----------------------------------------------------------------------------+
|7     |big burgers chicago       |  2  |starts with what is searched        |
+----------------------------------------------------------------------------+
|8     |burgerslicious            |  8  |resebles 1 word in search           |
+----------------------------------------------------------------------------+
|9     |bigmans burgers           |  7  |resembles multiple words in search  |
+------+--------------------------+-----+------------------------------------+

2 个答案:

答案 0 :(得分:0)

以下是您如何通过相关性得分进行FULLTEXT搜索和排序。

select id, title, 
       match (title) against ('big burger' with query expansion) score
  from samples 
  where match (title) against ('big burger'  with query expansion)
  order by 3 desc

在MySQL FULLTEXT搜索中,您对评分标准没有多少控制权。因此,无法保证您在问题中显示的订单。 FULLTEXT基本上是围绕向用户展示一些可能的选择并允许她选择一个。

要添加全文索引,表格将使用MyISAM访问方法,除非您使用的是MySQL 5.6或更高版本。

答案 1 :(得分:-1)

SELECT id, title
  FROM samples
 WHERE title like '%big%' or title like '%burger%'
 GROUP BY title
 ORDER BY CASE WHEN title like '%big burgers%' THEN 0
               WHEN title like '%burgers big%' THEN 1
               WHEN title like '%big %'  THEN 2
               WHEN title like '%burger %' THEN 3
               ELSE 4
          END, title

还要确保标题已编入索引。