-
Du kan inte definiera fulltextindex (eller någon form av index) över flera tabeller i MySQL. Varje indexdefinition refererar till exakt en tabell. Alla kolumner i ett givet fulltextindex måste vara från samma tabell.
-
Kolumnerna namngivna som argument till
MATCH()
funktion måste vara en del av ett enda fulltextindex. Du kan inte använda ett enda anrop tillMATCH()
för att söka i alla kolumner som ingår i alla fulltextindex i din databas. -
Fulltext indexerar endast indexkolumner definierade med
CHAR
,VARCHAR
ochTEXT
datatyper. -
Du kan definiera ett fulltextindex i varje tabell.
Exempel:
CREATE TABLE categories (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
FULLTEXT INDEX ftcat (name)
);
CREATE TABLE host_types (
id SERIAL PRIMARY KEY,
category_id BIGINT UNSIGNED,
name VARCHAR(100),
FULLTEXT INDEX ftht (name)
);
CREATE TABLE hosts (
id SERIAL PRIMARY KEY,
host_id BIGINT UNSIGNED,
category_id BIGINT UNSIGNED,
name VARCHAR(100),
FULLTEXT INDEX fthost (name)
);
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
keywords VARCHAR(100),
uid VARCHAR(100),
description VARCHAR(100),
quantity INTEGER,
price NUMERIC(9,2),
host_id BIGINT UNSIGNED,
FULLTEXT INDEX ftprod (name, keywords, description, uid)
);
Och sedan kan du skriva en fråga som använder varje respektive fulltextindex:
SELECT ...
MATCH(categories.name) AGAINST('search term') as cscore,
MATCH(host_types.name) AGAINST('search term') as htscore,
MATCH(hosts.name) AGAINST('search term') as hscore,
MATCH(products.name, products.keywords, products.description, products.uid)
AGAINST('search term') as score
FROM products
LEFT JOIN hosts ON products.host_id = hosts.id
LEFT JOIN host_types ON hosts.host_id = host_types.id
LEFT JOIN categories ON host_types.category_id = categories.id
WHERE
MATCH(categories.name) AGAINST('search term') OR
MATCH(host_types.name) AGAINST('search term') OR
MATCH(hosts.name) AGAINST('search term') OR
MATCH(products.name, products.keywords, products.description, products.uid)
AGAINST('search term')
ORDER BY score DESC;