Question

I have just set up a full text index on my MySQL database, but unfortunately it's not returning any results.

This seems to be a common issue, and the typical answers are use the MyISAM engine, and the 'IN BOOLEAN MODE' function. Well neither of these seem to work for me.

Here is my example code:

DROP TABLE IF EXISTS parentregionlist;
CREATE TABLE parentregionlist
(
    RegionID INT NOT NULL,
    RegionType VARCHAR(50),
    RelativeSignificance VARCHAR(3),
    SubClass VARCHAR(50),
    RegionName VARCHAR(255),
    RegionNameLong VARCHAR(510),
    ParentRegionID INT,
    ParentRegionType VARCHAR(50),
    ParentRegionName VARCHAR(255),
    ParentRegionNameLong VARCHAR(510),
    CountryCode VARCHAR(2),
  TimeStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (RegionID),
    FULLTEXT (RegionNameLong)
) ENGINE = MyISAM;

INSERT INTO parentregionlist
VALUES (575, 
"City", 
"", 
"", 
"Birmingham", 
"Birmingham, England, United kingdom", 
6023342, 
"Multi-City (Vicinity)", 
"Birmingham (and Vicinity)", 
"Birmingham (and vicinity), England, United Kingdom", 
"", 
now());

SELECT * 
FROM parentregionlist
WHERE MATCH(regionnamelong) 
AGAINST ('Birm' IN BOOLEAN MODE);

Any ideas?

P.S. In the real table I have 200k+ rows. I know the search won't work with just one row, it's just an example.

Was it helpful?

Solution

With the current query you are looking for a full word match. So for instance to get the row you show inserted above, you would need to do:

SELECT * 
FROM parentregionlist
WHERE MATCH(regionnamelong) 
AGAINST ('Birmingham (and vicinity), England, United Kingdom' IN BOOLEAN MODE);

However if you want to search for all results that start with 'Birm' you will need to add the '*' modifier:

SELECT * 
FROM parentregionlist
WHERE MATCH(regionnamelong) 
AGAINST ('Birm*' IN BOOLEAN MODE);

Alternatively you could also use LIKE:

SELECT * 
FROM parentregionlist
WHERE regionnamelong LIKE 'Birm%';

The MySQL docs give good MATCH examples to work from: http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top