gpt4 book ai didi

mysql - MySQL 中的全文搜索

转载 作者:行者123 更新时间:2023-11-29 03:12:49 28 4
gpt4 key购买 nike

我是 MySQL 中“搜索”的新手,我有一些任务,我不知道如何以最好的方式完成它们。

我的数据库中有以下 MySQL 表。

delimiter $$

CREATE TABLE `authors` (
`id` int(11) NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`count` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`name`),
FULLTEXT KEY `name_fulltext` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci$$

任务是:在 html 表单中我想输入一个名字,假设是“John Doe”。应在此表中的“名称”列中查找此名称。我希望查询返回所有类似的名称,如“John Due”或“John Doé”等。因此用户可以从列表中选择正确的名称。有时人们想查找像“John van Doe”(荷兰风格)这样的名字。这也应该显示在列表中。

如何最好地实现这一点?或者我应该更好地问。这可能吗? =) 顺便说一下,我正在使用 python cgi 脚本,因此 python 可以提供的任何模块都是可用的。

另一个问题是:如何只查找“John”或“Dow”?将显示其中包含“John”的每个名称。我试过“WHERE name LIKE “John””,但这太慢了。有没有更快的方法?

感谢您的任何建议。

最佳答案

The task is: In a html form I want to enter a name, let's say "John Doe". This Name should be looked up in this table, in the column "name". I want the query to return all similar names like "John Due" or "John Doé" and so on. So the user can pick from a list the right name. Sometimes People want to look up a name like "John van Doe" (Netherland style). This should be displayed too in the list.

MySQL 不支持同义词字典,所以你应该自己提供一个。

Yahoo API 提供拼写更正服务,您可以通过提交类似于以下的查询来使用该服务:

SELECT  *
FROM search.spelling
WHERE query='juhn doe'

使用此 URL:

http://query.yahooapis.com/v1/public/yql?q=SELECT%20%20*%20%20FROM%20search.spelling%20WHERE%20query%20%3D%20'juhn%20doe'&format=json&diagnostics=true&callback=cbfunc

一旦收到同义词列表,您就可以使用以下查询来搜索它们 MySQL:

SELECT  *
FROM authors
WHERE MATCH(name) AGAINST ('(+juhn +doe) (+john +doe)' IN BOOLEAN MODE)

John Doé 将由此返回,因为您使用的是不区分大小写和重音的 UTF8_GENERAL_CI

如果您只想查找 John,请使用此查询:

SELECT  *
FROM authors
WHERE MATCH(name) AGAINST ('+john' IN BOOLEAN MODE)

此外,请确保在 my.cnf 中将参数 ft_min_word_len 设置为合理的值(1 最好)。

默认值为 4,这意味着不会对三字母姓氏(如 Doe)编制索引。

关于mysql - MySQL 中的全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5513066/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com