开发者

MySQL Natural Lanquage Search not working as I'd hoped

I have a table of people's Full Names. I'd like users to be able to search it by partial names and misspelled names. So a search for 'Andrew' should also return 'Andrea', etc. I thought FULLTEXT search was the answer but it doesn't seem to work any differently than if I'd searched using ... LIKE '%Andrew%'.

Is there a function or feature in MySQL that will search based on string similarity? Or will I have to roll my own on the PHP end using levenshtein() or something similar?

Given This Table:

CREATE TABLE `people` (
  `FullName` varchar(30) default NULL,
  `namesID` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`namesID`),
  FULLTEXT KEY `fulltext_FullName` (`FullName`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

LOCK TABLES `people` WRITE;
/*!40000 ALTER TABLE `people` DISABLE KEYS */;
INSERT INTO `people` (`FullName`,`namesID`)
VALUES
    ('Mark Peters',1),
    ('Bob Jackson',2),
    ('Steve Kipp',3),
    ('Joe Runty',4),
    ('Tina Mardell',5),
    ('Tim Havers',6),
    ('Rich Beckett',7),
    ('Mary Dalson',8),
    ('Maria Grento',9),
    ('Michael Colt',10),
    ('Andrew Peters',11),
    ('Andre Bison',12),
    ('Andrea Masters',13),
    ('Marla Tool',14);

/*!40000 ALTER TABLE `people` ENABLE KEYS */;
UNLOCK TABLES;

And This Query:

SELECT *
FROM people
WHERE MATCH(FullName) AGAINST('Andrew');

I only get:

FullName        namesID
Andrew Peters   11

When I'd like to also ge开发者_如何学运维t:

Andre Bison
Andrea Masters

etc.


FULLTEXT indices are nothing more than indices on the full text. They only allow searches on the text you actually have.

MySQL does have a SOUNDEX() function, and a shorthand x SOUNDS LIKE y operator, which is the same as SOUNDEX(x) = SOUNDEX(y).

If soundex doesn't meet your needs, you would indeed need to involve a programming language like PHP to accomplish what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜