mysql 不是一个好的搜索引擎
我正在尝试为我们的图书馆创建一个搜索引擎。目前我有一个名为 book_tittle 的表,其中列包含:
id (int)
book_tittle (varchar 256)
book_description (varchar 256)
author_id
当有人搜索书名时,例如他输入:“bigbird”,我只需从 book_title 像 '%bigbird%'
的表中搜索。
如果我有一本名为“大鸟飞”的书,这个简单的查询就足够了,但不幸的是,当我有一本名为“大蓝鸟”的书时,查询将不会返回任何内容。
有人可以建议我如何解决它吗?这样,如果有人搜索“大鸟”,他将得到“大鸟飞”书和“大蓝鸟”书。
I'm trying to create a search engine for our library. Currently I have table call book_tittle where the column contain:
id (int)
book_tittle (varchar 256)
book_description (varchar 256)
author_id
When someone search book title, for example he type: "big bird", I simply search from table where book_title like '%big bird%'
.
If I have a book titled "big bird fly" this simple query was enough, but unfortunately when I have a book titled "big blue bird", the query will return nothing.
Can someone suggest me how to solve it? So that if someone search for "big bird", he will get "big bird fly" book and "big blue bird" book.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果将字段修改为 TEXT 字段而不是 VARCHAR,则可以使用 FULLTEXT 索引。然后,您可以使用 MATCH 而不是 LIKE 来匹配字符串。这种搭配就灵活多了。您可以匹配
bigbird
以匹配单词big
和bird
,也可以匹配"bigbird"
如果你想将单词匹配在一起。您还可以使用通配符,例如bir*
来匹配bird
和birth
。If you modify your field to a TEXT field instead of VARCHAR, you can use a FULLTEXT index. Then, you can use MATCH instead of LIKE to match a string. This match is much more flexible. You can match for
big bird
to match the wordsbig
andbird
or you can match for"big bird"
if you want to match the words together. You can also use wildcards, likebir*
to matchbird
andbirth
.您应该使用搜索引擎。查看 Lucene。它适用于 PHP、.NET、Java 等。
You should use a search engine. Check out Lucene. It is available for PHP, .NET, Java etc.
您可以重写您的查询,如下所示:
这可以在您的代码中轻松完成。我不知道你的图书表有多大,全文索引可能会更快。
MySQL也可以实现全文,需要使用MyISAM引擎。 Lucene 更高效、更强大。
You can rewrite your query like:
This can easly be done in your code. I don't know how large your book table is, a full-text index will probally be faster.
MySQL can also implement full-text, you will need to use the MyISAM engine. Lucene is more efficiënt tough.
你是对的——关系数据库不能成为好的搜索引擎。
你需要像 Lucene 这样的东西。您必须要求它为您的数据建立索引,以便您可以进行搜索。
You're right - relational databases don't make good search engines.
You need something like Lucene. You'll have to ask it to index your data so you can do searching.
您是否尝试过 MySQL 的内置全文支持?
Have you tried MySQL's built-in fulltext support?