Di MariaDB, MATCH AGAINST
adalah konstruksi khusus yang digunakan untuk melakukan pencarian teks lengkap pada indeks teks lengkap.
Sintaks
Sintaksnya seperti ini:
MATCH (col1,col2,...) AGAINST (expr [search_modifier])
Contoh
Misalkan kita memiliki tabel bernama Products
yang mencakup data berikut:
+----+---------------------------------+-----------------------------------------+ | 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. | +----+---------------------------------+-----------------------------------------+
Tabel ini memiliki indeks teks lengkap pada ProductDescription
kolom. Itu artinya kita bisa menggunakan MATCH AGAINST
untuk melakukan pencarian teks lengkap terhadap kolom tersebut.
Contoh:
SELECT
ProductId AS "Id",
ProductName,
ProductDescription
FROM Products
WHERE MATCH(ProductDescription) AGAINST('includes')
ORDER BY ProductId ASC;
Hasil:
+----+--------------------------+-----------------------------------------+ | 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. | +----+--------------------------+-----------------------------------------+
Dapatkan Skor
Kami dapat menyertakan MATCH AGAINST
di SELECT
list untuk mengembalikan skor relevansi kata kunci dalam kolom yang dicari:
SELECT
ProductDescription,
MATCH(ProductDescription) AGAINST ('includes') AS Score
FROM Products
WHERE MATCH(ProductDescription) AGAINST ('includes')
ORDER BY Score DESC;
Hasil:
+-----------------------------------------+---------------------+ | ProductDescription | Score | +-----------------------------------------+---------------------+ | Orange. Includes spare fingers. | 0.4883610010147095 | | Blue. Includes right handed carry box. | 0.4883610010147095 | | Purple. Includes left handed carry box. | 0.48305025696754456 | +-----------------------------------------+---------------------+
Dalam hal ini kami juga menggunakan ORDER BY
klausa untuk mengurutkan berdasarkan skor dalam urutan menurun (yaitu yang paling relevan terlebih dahulu).
Kolom Tanpa Indeks Teks Lengkap
Alasan contoh sebelumnya berhasil adalah karena saya sebelumnya telah membuat indeks teks lengkap di ProductDescription
kolom. Jika saya tidak melakukan ini, saya akan menerima kesalahan.
Inilah yang terjadi ketika kami mencoba menggunakan MATCH AGAINST
terhadap kolom yang tidak memiliki indeks teks lengkap:
SELECT
ProductId,
ProductName,
ProductPrice
FROM Products
WHERE MATCH(ProductName) AGAINST('screwdriver')
ORDER BY ProductId ASC;
Hasil:
ERROR 1191 (HY000): Can't find FULLTEXT index matching the column list
Mari tambahkan indeks teks lengkap pada ProductName
kolom:
ALTER TABLE Products
ADD FULLTEXT(ProductName);
Sekarang jalankan kueri lagi:
SELECT
ProductId,
ProductName,
ProductPrice
FROM Products
WHERE MATCH(ProductName) AGAINST('screwdriver')
ORDER BY ProductId ASC;
Hasil:
+-----------+--------------------------+--------------+ | ProductId | ProductName | ProductPrice | +-----------+--------------------------+--------------+ | 1 | Left handed screwdriver | 25.99 | | 2 | Right handed screwdriver | 25.99 | +-----------+--------------------------+--------------+
Kali ini berhasil.
Indeks Teks Lengkap pada Beberapa Kolom
Kita dapat menambahkan indeks teks lengkap pada beberapa kolom.
Contoh:
ALTER TABLE Products
ADD FULLTEXT(ProductName, ProductDescription);
Sekarang kita dapat menjalankan MATCH AGAINST
terhadap indeks teks lengkap itu.
SELECT
ProductId AS Id,
ProductName,
ProductDescription
FROM Products
WHERE MATCH(ProductName, ProductDescription) AGAINST ('weight')
OR MATCH(ProductName, ProductDescription) AGAINST ('ceramic')
ORDER BY Id ASC;
Hasil:
+----+---------------------------------+---------------------------------------+ | 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. | +----+---------------------------------+---------------------------------------+