W MariaDB, MATCH AGAINST to specjalna konstrukcja używana do wykonywania wyszukiwania pełnotekstowego w indeksie pełnotekstowym.
Składnia
Składnia wygląda tak:
MATCH (col1,col2,...) AGAINST (expr [search_modifier]) Przykład
Załóżmy, że mamy tabelę o nazwie Products który zawiera następujące dane:
+----+---------------------------------+-----------------------------------------+ | Id | ProductName | ProductDescription | +----+---------------------------------+-----------------------------------------+ | 1 | Left handed screwdriver | Purple. Includes left handed carry box. | | 2 | Right handed screwdriver | Blue. Includes right handed carry box. | | 3 | Long Weight (blue) | Approximate 45 minute waiting period. | | 4 | Long Weight (green) | Approximate 30 minute waiting period. | | 5 | Sledge Hammer | Wooden handle. Free wine glasses. | | 6 | Chainsaw | Orange. Includes spare fingers. | | 7 | Straw Dog Box | Tied with vines. Very chewable. | | 8 | Bottomless Coffee Mugs (4 Pack) | Brown ceramic with solid handle. | +----+---------------------------------+-----------------------------------------+
Ta tabela ma indeks pełnotekstowy w swoim ProductDescription kolumna. Oznacza to, że możemy użyć MATCH AGAINST aby przeprowadzić wyszukiwanie pełnotekstowe w tej kolumnie.
Przykład:
SELECT
ProductId AS "Id",
ProductName,
ProductDescription
FROM Products
WHERE MATCH(ProductDescription) AGAINST('includes')
ORDER BY ProductId ASC; Wynik:
+----+--------------------------+-----------------------------------------+ | Id | ProductName | ProductDescription | +----+--------------------------+-----------------------------------------+ | 1 | Left handed screwdriver | Purple. Includes left handed carry box. | | 2 | Right handed screwdriver | Blue. Includes right handed carry box. | | 6 | Chainsaw | Orange. Includes spare fingers. | +----+--------------------------+-----------------------------------------+
Zdobądź wynik
Możemy dołączyć MATCH AGAINST w SELECT listę w celu zwrócenia wyniku trafności słowa kluczowego w przeszukiwanej kolumnie (kolumnach):
SELECT
ProductDescription,
MATCH(ProductDescription) AGAINST ('includes') AS Score
FROM Products
WHERE MATCH(ProductDescription) AGAINST ('includes')
ORDER BY Score DESC; Wynik:
+-----------------------------------------+---------------------+ | ProductDescription | Score | +-----------------------------------------+---------------------+ | Orange. Includes spare fingers. | 0.4883610010147095 | | Blue. Includes right handed carry box. | 0.4883610010147095 | | Purple. Includes left handed carry box. | 0.48305025696754456 | +-----------------------------------------+---------------------+
W tym przypadku również użyliśmy ORDER BY klauzula sortowania według wyniku w kolejności malejącej (tj. najtrafniejsze jako pierwsze).
Kolumny bez indeksu pełnotekstowego
Powodem, dla którego poprzedni przykład działał, jest to, że wcześniej utworzyłem indeks pełnotekstowy w ProductDescription kolumna. Gdybym tego nie zrobił, otrzymałbym błąd.
Oto, co się dzieje, gdy próbujemy użyć MATCH AGAINST w stosunku do kolumny, która nie ma indeksu pełnotekstowego:
SELECT
ProductId,
ProductName,
ProductPrice
FROM Products
WHERE MATCH(ProductName) AGAINST('screwdriver')
ORDER BY ProductId ASC; Wynik:
ERROR 1191 (HY000): Can't find FULLTEXT index matching the column list
Dodajmy indeks pełnotekstowy do ProductName kolumna:
ALTER TABLE Products
ADD FULLTEXT(ProductName); Teraz uruchom zapytanie ponownie:
SELECT
ProductId,
ProductName,
ProductPrice
FROM Products
WHERE MATCH(ProductName) AGAINST('screwdriver')
ORDER BY ProductId ASC; Wynik:
+-----------+--------------------------+--------------+ | ProductId | ProductName | ProductPrice | +-----------+--------------------------+--------------+ | 1 | Left handed screwdriver | 25.99 | | 2 | Right handed screwdriver | 25.99 | +-----------+--------------------------+--------------+
Tym razem się udało.
Pełnotekstowy indeks w wielu kolumnach
Możemy dodać indeksy pełnotekstowe w wielu kolumnach.
Przykład:
ALTER TABLE Products
ADD FULLTEXT(ProductName, ProductDescription);
Teraz możemy uruchomić MATCH AGAINST w stosunku do tego indeksu pełnotekstowego.
SELECT
ProductId AS Id,
ProductName,
ProductDescription
FROM Products
WHERE MATCH(ProductName, ProductDescription) AGAINST ('weight')
OR MATCH(ProductName, ProductDescription) AGAINST ('ceramic')
ORDER BY Id ASC; Wynik:
+----+---------------------------------+---------------------------------------+ | Id | ProductName | ProductDescription | +----+---------------------------------+---------------------------------------+ | 3 | Long Weight (blue) | Approximate 45 minute waiting period. | | 4 | Long Weight (green) | Approximate 30 minute waiting period. | | 8 | Bottomless Coffee Mugs (4 Pack) | Brown ceramic with solid handle. | +----+---------------------------------+---------------------------------------+