sql >> Database teknologi >  >> RDS >> MariaDB

Sådan fungerer MATCH MOD I MariaDB

I MariaDB, MATCH AGAINST er en speciel konstruktion, der bruges til at udføre en fuldtekstsøgning på et fuldtekstindeks.

Syntaks

Syntaksen ser sådan ud:

MATCH (col1,col2,...) AGAINST (expr [search_modifier])

Eksempel

Antag, at vi har en tabel kaldet Products der inkluderer følgende data:

+----+---------------------------------+-----------------------------------------+
| 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.        |
+----+---------------------------------+-----------------------------------------+

Denne tabel har et fuldtekstindeks på dens ProductDescription kolonne. Det betyder, at vi kan bruge MATCH AGAINST at lave en fuldtekstsøgning mod den kolonne.

Eksempel:

SELECT 
    ProductId AS "Id",
    ProductName,
    ProductDescription
FROM Products 
WHERE MATCH(ProductDescription) AGAINST('includes')
ORDER BY ProductId ASC;

Resultat:

+----+--------------------------+-----------------------------------------+
| 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.         |
+----+--------------------------+-----------------------------------------+

Få scoren

Vi kan inkludere MATCH AGAINST i SELECT liste for at returnere relevansscore for søgeordet i den/de søgte søjler:

SELECT 
    ProductDescription,
    MATCH(ProductDescription) AGAINST ('includes') AS Score
FROM Products 
WHERE MATCH(ProductDescription) AGAINST ('includes')
ORDER BY Score DESC;

Resultat:

+-----------------------------------------+---------------------+
| ProductDescription                      | Score               |
+-----------------------------------------+---------------------+
| Orange. Includes spare fingers.         |  0.4883610010147095 |
| Blue. Includes right handed carry box.  |  0.4883610010147095 |
| Purple. Includes left handed carry box. | 0.48305025696754456 |
+-----------------------------------------+---------------------+

I dette tilfælde brugte vi også en ORDER BY klausul for at sortere efter score i faldende rækkefølge (dvs. mest relevant først).

Kolonner uden et fuldtekstindeks

Grunden til, at det forrige eksempel virkede, er, at jeg tidligere havde oprettet et fuldtekstindeks på ProductDescription kolonne. Hvis jeg ikke havde gjort dette, ville jeg have modtaget en fejl.

Her er, hvad der sker, når vi forsøger at bruge MATCH AGAINST mod en kolonne, der ikke har et fuldtekstindeks:

SELECT 
    ProductId,
    ProductName,
    ProductPrice
FROM Products 
WHERE MATCH(ProductName) AGAINST('screwdriver')
ORDER BY ProductId ASC;

Resultat:

ERROR 1191 (HY000): Can't find FULLTEXT index matching the column list

Lad os tilføje et fuldtekstindeks på ProductName kolonne:

ALTER TABLE Products
ADD FULLTEXT(ProductName);

Kør nu forespørgslen igen:

SELECT 
    ProductId,
    ProductName,
    ProductPrice
FROM Products 
WHERE MATCH(ProductName) AGAINST('screwdriver')
ORDER BY ProductId ASC;

Resultat:

+-----------+--------------------------+--------------+
| ProductId | ProductName              | ProductPrice |
+-----------+--------------------------+--------------+
|         1 | Left handed screwdriver  |        25.99 |
|         2 | Right handed screwdriver |        25.99 |
+-----------+--------------------------+--------------+

Denne gang virkede det.

Fuldtekstindeks på flere kolonner

Vi kan tilføje fuldtekstindekser på flere kolonner.

Eksempel:

ALTER TABLE Products
ADD FULLTEXT(ProductName, ProductDescription);

Nu kan vi køre MATCH AGAINST mod det fuldtekstindeks.

SELECT 
    ProductId AS Id, 
    ProductName, 
    ProductDescription
FROM Products 
WHERE MATCH(ProductName, ProductDescription) AGAINST ('weight')
OR MATCH(ProductName, ProductDescription) AGAINST ('ceramic')
ORDER BY Id ASC;

Resultat:

+----+---------------------------------+---------------------------------------+
| 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.      |
+----+---------------------------------+---------------------------------------+


  1. JSON_CONTAINS_PATH() Eksempler i MySQL

  2. Evalueringsrækkefølge for Oracle SQL-sætning

  3. Oracle Streams trin for trin replikeringseksempel

  4. Sådan får du en liste over aktiverede/deaktiverede tjekbegrænsninger i SQL Server-databasen - SQL Server / TSQL-vejledning, del 86