设计“基于相关性”的内容搜索?
在我的应用程序(PHP/MySQL/JS)中,我内置了搜索功能。搜索条件之一包含各种选项的复选框,因此,某些结果将比其他结果更相关,如果它们包含更多或更少的内容每个选项。
即选项是A和B,如果我搜索选项A和B,则仅包含选项A的结果1是50%相关,而包含选项A和B的结果2是100%相关。
之前,我只是根据表单输入进行简单的 SQL 查询,但是这个有点难,因为它不像像“%query%”这样的数据那么简单,而是一些结果对某些搜索查询更有价值,有些则不然。
我完全不知道从哪里开始......有人有相关的(哈!)阅读材料指导我吗?
编辑:经过深思熟虑,我在想一些涉及 SQL 脚本来获取原始数据,然后进行许多轮解析的事情是我必须做的......
没有可缓存的,尽管? :(
In my application (PHP/MySQL/JS), I have a search functionality built in. One of the search criteria contains checkboxes for various options, and as such, some results would be more relevant than others, should they contain more or less of each option.
i.e. Options are A and B, and if I search for both options A and B, Result 1 containing only option A is 50% relevent, while Result 2 containing both options A and B is 100% relevant.
Prior, I'd just be doing simple SQL queries based on form input, but this one's a little harder, since it's not as simple as data LIKE "%query%", but rather, some results are more valuable to some search queries, and some aren't.
I have absolutely no idea where to begin... does anybody have relevant (ha!) reading material to direct me to?
Edit: After mulling it over, I'm thinking something involving an SQL script to get the raw data, followed by many many rounds of parsing is something I'd have to do...
Nothing cacheable, though? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看 lucence 项目
它有多种语言版本,
这是 php 端口
http://framework.zend.com/manual/en/zend.search .lucene.html
它对要搜索的项目进行索引并返回相关的加权搜索结果,例如,比 select x from y where name like '%pattern%' 样式搜索更好
have a look at the lucence project
it is available in many languages
this is the php port
http://framework.zend.com/manual/en/zend.search.lucene.html
it indexes the items to search and returns the relevant weighted search results, eg better then select x from y where name like '%pattern%' style searching
你需要的是一个强大的搜索引擎,比如 solr。虽然您可以在 mysql 之上实现此功能,但它已经与其他工具一起提供了开箱即用的功能。
What you need is a powerful search engine, like solr. While you could implement this on top of mysql, it's already provided out of the box with other tools.
这是一个想法:进行比较并对结果求和。总和越高,匹配的条件就越多。
像这样的(愚蠢的)表怎么样:
查找与 3/15/1980 共享三个日期组件最多的人:
需要一个好的 WHERE 子句和索引来防止您进行表扫描,但是...
您甚至可以为列添加权重,例如
祝您好运。
Here's an idea: do the comparisons and sum the results. The higher the sum, the more criteria match.
How about a (stupid) table like this:
Find the person who shares the most of the three date components with 3/15/1980:
A good WHERE clause and index would be required to keep you from doing a table scan, but...
You could even add a weight to a column, e.g.
Good luck.
鉴于您对我的评论的回答,这里有一个关于如何执行此操作的示例:
首先是表格:
然后是一些项目和条件的示例:
这将创建 3 个项目和 3 个选项,并向它们分配选项。
现在有多种方式可以按一定的“强度”进行订购。最简单的是:
这将向您显示具有选项 1 或选项 3 的所有项目,但同时具有这两个选项的项目似乎排名“更高”。
如果您要搜索 2 个选项,这会很有效。但是让我们假设您对所有 3 个选项进行搜索,这就是为什么为选项分配“权重”很重要,
但如果您的查询不这样做,这可能对您没有帮助。并不总是分配相同的这可以通过以下查询在每个查询的基础上轻松实现:
尝试查询,看看它是否是您所需要的
。我建议您添加索引,使选项字段为整数,
如果您有任何问题或需要添加的内容,请留下评论。
Given your answer to my comment, here's an example on how you might do it:
First the tables:
Then an example of some items and criteria:
This would create 3 items and 3 options and assign options to them.
Now there are multiple way you can order by a certain "strength". The simplest of which would be:
This would show you all the items that have option 1 or option 3 but those with both options would appear to be ranked "higher.
This works well if you're doing a search on 2 options. But let's assume you make a search on all 3 options. All the items now share the same strength, this is why it's important to assign "weights" to options.
You could make the value your strength, but that might not help you if your queries don't always assign the same weights to the same options everywhere. This can be easily achieved on a per-query basis with the following query:
Try the queries out and see if it's what you need.
I would also like to note that this is not the best solution in terms of processing power. I'd recommend you add indexes, make the option field an integer, cache results wherever possible.
Leave a comment if you have any questions or anything to add.