加权关键词搜索
您好:我想对带有关键字标记的产品进行“加权搜索”。 (所以:不是全文搜索,而是 n 到 m 关系)。所以这就是:
Table 'product':
sku - the primary key
name
Table 'keywords':
kid - keyword idea
keyword_de - German language String (e.g. 'Hund','Katze','Maus')
keyword_en - English language String (e.g. 'Dog','Cat','Mouse')
Table 'product_keyword' (the cross-table)
sku \__ combined primary key
kid /
我想要的是获得至少“包含”一个相关关键字的所有产品的分数。如果我搜索 ('Dog','Elephant','Maus') 我希望
Dog 的得分为 1.003, 1.002 的大象 Maus of 1.001
所以最不重要的搜索词从 1.001 开始,其他都是 0.001++。这样,分数下限 3.0 将等于“AND”查询(必须找到所有三个关键字),分数下限 1.0 将等于“OR”。介于两者之间的任何东西都或多或少匹配。特别是根据这个分数排序,最相关的搜索结果将排在第一位(无论下限如何)...
我想我可能必须
IF( keyword1 == 'dog', 1.001, 0) + IF...
在 SUM() 内部做一些事情,并且可能在末尾使用 GROUP BY通过交叉表进行 JOIN,是吗?但我相当不知道如何解决这个问题。
可行的方法是预先从关键字中获取关键字 id。这是一个廉价的查询。因此,关键字表可以被忽略,而这一切都与交叉表和乘积表的另一个有关...
我手头有 PHP 可以自动准备相当长的 PHP 语句,但我想避免进一步的多个 SQL 语句。特别是因为我将限制分页模式结果的查询结果(最常见的是“LIMIT 0, 20”),所以通过脚本循环大量的中间结果是不好的......
DANKESCHÖN,如果可以的话帮我解决这个问题:-)
Hello: I want to do a "weighted search" on product that are tagged with keywords.
(So: not fulltext search, but n-to-m-relation). So here it is:
Table 'product':
sku - the primary key
name
Table 'keywords':
kid - keyword idea
keyword_de - German language String (e.g. 'Hund','Katze','Maus')
keyword_en - English language String (e.g. 'Dog','Cat','Mouse')
Table 'product_keyword' (the cross-table)
sku \__ combined primary key
kid /
What I want is to get a score for all products that at least "contain" one relevant keyword. If I search for ('Dog','Elephant','Maus') I want that
Dog credits a score of 1.003,
Elephant of 1.002
Maus of 1.001
So least important search term starts at 1.001, everything else 0.001++. That way, a lower score limit of 3.0 would equal "AND" query (all three keywords must be found), a lower score limit of 1.0 would equal an "OR". Anything in between something more or less matching. In particular by sorting according to this score, most relevant search results would be first (regardless of lower limit)...
I guess I will have to do something with
IF( keyword1 == 'dog', 1.001, 0) + IF...
maybe inside a SUM() and probably with a GROUP BY at the end of a JOIN over the cross table, eh? But I am fairly clueless how to tackle this.
What would be feasible, is to get the keyword id's from the keywords beforehand. That's a cheap query. So the keywords table can be left ignored and it's all about the other of the cross and product table...
I have PHP at hand to automatically prepare a fairly lengthy PHP statement, but I would like to avoid further multiple SQL statements. In particular since I will limit the query outcome (most often to "LIMIT 0, 20") for paging mode results, so looping a very large number of in between results through a script would be no good...
DANKESCHÖN, if you can help me on this :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为其中很多内容都在 Lucene 引擎 (http://lucene.apache.org/java/docs/index.html) 中,它可用于 Zend Framework 中的 PHP: http://framework.zend.com/manual/en/zend.search.lucene.html。
编辑:
如果你想做你正在谈论的加权事情,我想你可以使用这样的东西:(
编辑2:忘记
group by
子句。)I think a lot of this is in the Lucene engine (http://lucene.apache.org/java/docs/index.html), which is available for PHP in the Zend Framework: http://framework.zend.com/manual/en/zend.search.lucene.html.
EDIT:
If you want to do the weighted thing you are talking about, I guess you could use something like this:
(Edit 2: forgot the
group by
clause.)