简体   繁体   中英

PHP MySQL Match() Against() Fulltext search not working

I've been trying to get a FULLTEXT search to work these last couple of weeks, but nothing seems to be working. I'm using GoDaddy hosting and a myisam table. I followed the instructions on the SQL developer website to create my database with a Fulltext called "search", containing the columns "firstname, lastname, username". Despite having 11 rows in my table, the fulltext search will only search if I type the entire username/firstname/lastname.

SQL:

    SELECT * FROM users
    WHERE MATCH (username, firstname, lastname) AGAINST ('$key');

I have tried in natural language mode, in boolean mode, and just regular like before. The index created is a FULLTEXT index and the engine is MYISAM. I see no reason why this isn't working.

The reason I need it is because I would like users to be able to search parts of usernames/names and have them ordered by relevance, eg "Tom" and "Tomas" would both be returned...

Searching a full username, eg "bisoncode" will return the row, but searching "bisonco" doesn't.

AJAX script

function getStates(value) {
    $.post("search.php", {name:value},function(data){
        $("#results").html(data);
    }
    ); 
}

Search HTML

<input type="text" class="search" placeholder="Search..." autocomplete="off" onkeyup="getStates(this.value)">
<div id="results"></div>

Execution of query (posted above) results

if($result->num_rows == 0){
    echo "no results!";
} else {
while($row = $result->fetch_assoc()){
       $array[] = $row['id'];
       $array[] = $row['username'];
       $array[] = $row['firstname'];
       echo json_encode($array)."<br>";
}

If your MySQL version is less than 5.6 then I understand using MyISAM otherwise I suggest using InnoDB so the example below would work as shown.

Example in this site nearly looks like what you're trying to do: http://www.inanzzz.com/index.php/post/hfo7/how-to-do-full-text-search-in-myssql

TABLE

# Create table.
CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `middlename` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `surname` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `code` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT(`name`,`middlename`,`surname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

# Populate table.
INSERT INTO `person` (`name`, `middlename`, `surname`, `code`) VALUES
('John', 'Joseph', 'Travolta', 'JJT'),
('John', '', 'Lenon', 'JL'),
('John', '', 'Wayne', 'JW'),
('John', 'Paul', 'John', 'JPJ'),
('Robert', '', 'DeNiro', 'RD'),
('Elton', '', 'John', 'EJ'),
('Abi', 'John John', '', 'AJ'),
('Johny', '', '', 'J'),
('John', 'John', 'John', 'JJJ');

BASIC QUERY

# Basic select query.
SELECT *
FROM person
WHERE
MATCH(`name`, `middlename`, `surname`) AGAINST ('John' IN NATURAL LANGUAGE MODE);

+----+-------+------------+----------+------+
| id | name  | middlename | surname  | code |
+----+-------+------------+----------+------+
|  9 | John  | John       | John     | JJJ  |
|  4 | John  | Paul       | John     | JPJ  |
|  7 | Abi   | John John  |          | AJ   |
|  1 | John  | Joseph     | Travolta | JJT  |
|  2 | John  |            | Lenon    | JL   |
|  3 | John  |            | Wayne    | JW   |
|  6 | Elton |            | John     | EJ   |
+----+-------+------------+----------+------+
7 rows in set (0.00 sec)

QUERY with RELEVANCE

SELECT *,
MATCH(`name`, `middlename`, `surname`) AGAINST ('John' IN NATURAL LANGUAGE MODE) AS score
FROM person
ORDER BY score DESC;

+----+--------+------------+----------+------+----------------------+
| id | name   | middlename | surname  | code | score                |
+----+--------+------------+----------+------+----------------------+
|  9 | John   | John       | John     | JJJ  |    0.035737544298172 |
|  4 | John   | Paul       | John     | JPJ  | 0.023825030773878098 |
|  7 | Abi    | John John  |          | AJ   | 0.023825030773878098 |
|  1 | John   | Joseph     | Travolta | JJT  | 0.011912515386939049 |
|  2 | John   |            | Lenon    | JL   | 0.011912515386939049 |
|  3 | John   |            | Wayne    | JW   | 0.011912515386939049 |
|  6 | Elton  |            | John     | EJ   | 0.011912515386939049 |
|  5 | Robert |            | DeNiro   | RD   |                    0 |
|  8 | Johny  |            |          | J    |                    0 |
+----+--------+------------+----------+------+----------------------+
9 rows in set (0.00 sec)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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