简体   繁体   English

PHP MySQL搜索全文

[英]php mysql search fulltext

Table structure: 表结构:

Companies: id, name, city, branch, keywords, country

The keywords field contains the name city and branch together, I now just use a LIKE with wildcards for example '%music london%' will return 'musicfactory london' 关键字字段包含城市名称和分支名称,我现在仅使用带有通配符的LIKE,例如'%music london%'将返回'musicfactory london'

But it is not precise enough for example if the user wants to search Vini & Magraine in London (a restaurant) and the input was 'vini londen' it was not found, however if the user inserts 'magraine london' it will get found. 但这还不够精确,例如,如果用户要搜索伦敦(一家餐馆)的Vini&Magraine,并且输入的内容是“ vini londen”,则找不到该输入,但是如果用户插入“ magraine london”,则会被找到。

So the problem is prob the order the keywords are, now I'm planning to converting to a fulltext search, I've created the index on the keywords field, also is the keywords field a smart idea? 所以问题是可能是关键字的顺序,现在我打算转换为全文搜索,我已经在关键字字段上创建了索引,关键字字段也是一个好主意吗?

My primary question is, I created a fulltext search with this query: 我的主要问题是,我使用以下查询创建了全文搜索:

SELECT id, name, branch FROM companies WHERE MATCH(keywords) AGAINST(? IN BOOLEAN MODE) AND country = ? LIMIT 7

However it ain't returning the correct results? 但是,它没有返回正确的结果吗? What is the best and most of all working solution? 什么是最好,最有效的解决方案?

Thanks for reading. 谢谢阅读。

This is a good break down of standard match against operators. 这是对运营商的标准匹配的很好分解。 It should clarify HOW to write your query: 它应该阐明如何编写查询:

http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

If I read your question correctly you're asking if a "keywords" column in the companies table is a good idea - I say it depends on how you populate it but sure, it's not bad. 如果我正确地阅读了您的问题,则说明您在companies表中的“关键字”列是否是个好主意-我说这取决于您的填充方式,但是可以肯定,这还不错。 I used a collective keyword column that contained all standard text from the collection of relevant fields before and it worked out well. 我以前使用的是一个集体关键字列,该列以前包含相关字段集合中的所有标准文本,并且效果很好。

A couple other notes - 其他几个注意事项-

1) I think your LIKE example is flawed and by that rule I'm surprised you got any results. 1)我认为您的LIKE示例存在缺陷,根据该规则,我很惊讶您获得了任何结果。 % is a wildcard that is directional. %是定向的通配符。 so '%music london%' should NOT return 'musicfactory london'. 因此,“%music london%”不应返回“ musicfactory london”。 you would need to write that out as WHERE( keywords LIKE '%music%' AND keywords LIKE '%london%') for that to work. 您必须将其写为WHERE( keywords LIKE'%music%'和keywords LIKE'%london%')才能正常工作。

2) full text searches can only be used on MyISAM tables, so if you use a framework that depends on innoDB you will need to create a relational workaround. 2)全文搜索只能在MyISAM表上使用,因此,如果使用依赖于innoDB的框架,则需要创建一个关系解决方法。 (or any other reason why you would use innoDB) (或您使用innoDB的任何其他原因)

3) match against has a default minimum character count of 4 by default ini settings. 3)默认ini设置中,match对的默认最小字符数为4。 You will need to set that lower in your config, then rebuild your full text indices if you need a lower min character range. 您需要在配置中设置较低的值,然后如果需要较低的最小字符范围,则重建全文索引。 Consider words like "art" and you'll know why it is suggested to lower this value. 考虑一下“艺术”之类的词,您会知道为什么建议降低此值。 Otherwise you may have to write a conditional to switch between match against and LIKE mode for shorter strings. 否则,对于较短的字符串,您可能必须编写条件才能在对匹配和LIKE模式之间切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM