MYSQL自定义查询顺序按搜索令牌发布


MYSQL custom query order by search token issue

现在我正在使用php和mySql构建一个自定义查询。假设我有一个字符串你好,我是。对于这个

select distinct(user_primary.id) as id 
from user_primary, 
user_sec, 
user_location, 
user_ind_cat 
where 
( 
 user_primary.status != 3 
 and 
 (
  user_primary.id = user_sec.job_id
 ) 
 and 
 (
  user_primary.id = user_ind_cat.job_id
 )
) 
and 
( 
 (
  user_primary.name like "%Hello%"
 ) 
 or 
 (
  user_primary.name like "%this%"
 ) 
 or 
 (
  user_primary.name like "%is%"
 ) 
 or 
 (
  user_primary.name like "%me%"
 ) 
 and 
 (
  user_primary.name like "%Hello this is me%"
 )
) 
and 
(
 user_primary.login <= 1415426357
) 
limit 0, 150

到目前为止,它一直运行良好,直到最近我发现了一个问题。每当我运行这种查询时,它都会生成包含用户全名的结果,如搜索令牌和其他由搜索令牌生成的匹配令牌。但与所提供的令牌"你好,这是我"完全匹配的实际行没有显示在顶部。

让我解释一下如果我运行当前查询的结果,

  1. "你好"
  2. "我是尼克松"
  3. "你好,我是"
  4. "哦,你好"
  5. "我就是我"
  6. "你好"

我想在顶部显示实际结果,所以结果会像这样,

  1. "你好,我是"
  2. "你好"
  3. "我是尼克松"
  4. "哦,你好"
  5. "我就是我"
  6. "你好"

有人能告诉我这里出了什么问题吗?还是应该删除或添加查询?

提前感谢

Nixon

这听起来像是全文索引的工作!

 ALTER TABLE `user_primary` ADD FULLTEXT INDEX (`name`);

MySQL现在为文本创建了一个比OR链更模糊的搜索索引。因此,您的部分查询将看起来像:

 SELECT name, MATCH(name) AGAINST ('Hello this is me') as confidence 
     FROM user_primary
     WHERE MATCH(name) AGAINST ('Hello this is me')
     ORDER BY confidence DESC

比赛越好,confidence就越高,所以"你好,这是我"应该是最重要的。

这可能是您的查询,已清理并未经测试:

select distinct(user_primary.id) as id , MATCH(name) AGAINST ('Hello this is me') as confidence
from user_primary, user_sec, user_location, user_ind_cat 
WHERE 
 user_primary.status != 3 
 and user_primary.id = user_sec.job_id 
 and user_primary.id = user_ind_cat.job_id
 and MATCH(name) AGAINST ('Hello this is me')
 and  user_primary.login <= 1415426357
ORDER BY confidence DESC
limit 0, 150

添加Match分数并按其排序:

select distinct(user_primary.id) as id,
CASE user_primary.name 
  WHEN user_primary.name like "%Hello this is me%" THEN 100
  WHEN user_primary.name like "%Hello%" THEN 50
  WHEN user_primary.name like "%this%" THEN 40
  WHEN user_primary.name like "%is%" THEN 30
  WHEN user_primary.name like "%me%" THEN 10
  ELSE 0
END as sortScore

from user_primary, 
user_sec, 
-- user_location, 
user_ind_cat 
where 
user_primary.status != 3 
and user_primary.id = user_sec.job_id
and user_primary.id = user_ind_cat.job_id 
and 
( 
 user_primary.name like "%Hello this is me%"
  or user_primary.name like "%Hello%"
  or user_primary.name like "%this%"
  or user_primary.name like "%is%"
  or user_primary.name like "%me%"
)
and user_primary.login <= 1415426357
order by sortScore
limit 0, 150