MySQL 搜索(按相关性排序)
任何人都可以帮助我如何按以下标准的相关性对行进行排序吗?
`tbluser`
- - - - - - -
First Name
Last Name
`tbleduc`
- - - - - - -
School
College
University
在搜索表单上,用户具有以下字段,
Name
School
College
University
其中学校学院和大学是可选的。
名称被分成2个单词(中间的其他单词被省略),第一个单词被视为第一个名字,最后一个单词被视为姓氏。
现在我想实现基于相关性的搜索。
感谢您的帮助:)
Can any one help me how to sort rows by relevance for the following criterion ?
`tbluser`
- - - - - - -
First Name
Last Name
`tbleduc`
- - - - - - -
School
College
University
On the search form the user has following fields
Name
School
College
University
Where School College and University are Optional..
And Name is split into 2 words (other words in middle are omitted), first word is taken as first anme and last word as last name..
Now I would like to implement search based on relevance.
Thanks for the help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,我在 Postgres 上为你尝试了这个(我这里没有 MySQL,所以可能有点不同):
我使用这些 DDL 语句来创建表:
还有一点示例数据:
查询可以简化有点,如果 tbluser 和 tbleduc 之间的关系是 1:1。
不要忘记将
%a%
,%b
, ... 替换为您的变量(我建议使用准备好的语句)。我希望这个模板可以作为一个基本解决方案有所帮助 - 您可以根据需要调整它:-) 您还可以删除最外面的 select 语句,以获取各个结果的计数器。
Okay, I tried this for you on Postgres (I don't have MySQL here, so maybe it's a little bit different):
I used these DDL statements to create the tables:
And a little bit of example data:
The query can be simplified a bit, if the relationship between
tbluser
andtbleduc
is 1:1.Don't forget to replace the
%a%
,%b
, ... with your variables (I recommend using a prepared statement).I hope this template helps as a basic solution - you can tweak it as much as you like :-) You can also remove the outermost select statement, to get the counters of the individual results.
步骤1:定义计算“相关性”的方法。
步骤 2:编写一个查询,使用步骤 1 中的计算来确定其结果的顺序。
不幸的是,您还没有说明是什么使一条记录比另一条记录“更相关”或“不太相关”,因此我们目前只能告诉您这些。
Step 1: Define a method of calculating "relevance".
Step 2: Write a query which uses the calculation from step 1 to determine the order of its results.
Unfortunately, you haven't stated what makes one record "more relevant" or "less relevant" than another, so that's about as much as we can tell you at this point.
您是否尝试过在您的表上进行“匹配 - 反对”查询。结果集默认按相关性排序。另外它还可以进行全文搜索。
Have you tried the Match - Against query on your table. The result set is sorted by relevance y default. Plus it does Full text search.
您有时间和资源来尝试实施 Solr 吗?它对于相关性排名搜索要好得多,并且非常容易上手。
Do you have the time and resources to try to implement Solr? It is much better for relevancy-ranked searching, and is surprisingly easy to get started with.