现在我正在使用 php 和 mySql 构建自定义查询。假设我有一个字符串 Hello this is me。为此
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
到目前为止一切正常,直到最近我发现了一个问题。每当我运行这种查询时,它都会生成包含用户全名的结果,例如搜索 token 和从搜索 token 生成的其他匹配 token 。但是与提供的标记“你好,这是我”完美匹配的实际行并未显示在顶部。
如果我运行当前查询,让我解释一下结果,
- “你好,你好吗”
- “这是尼克松”
- “你好,我是”
- “哦你好”
- “我就是我”
- “你好”
我想在顶部显示实际结果,所以结果看起来像这样,
- “你好,我是”
- “你好,你好吗”
- “这是尼克松”
- “哦你好”
- “我就是我”
- “你好”
谁能告诉我这里有什么问题?还是应该删除或添加查询?
提前致谢
尼克松
最佳答案
这听起来像是全文索引的工作!
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
匹配越好,置信度
就越高,所以“Hello this is me”应该放在最前面。
这可能是您的查询,已清理且未经测试:
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
关于php - MYSQL 通过搜索 token 问题自定义查询顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26815291/