sql >> Database teknologi >  >> RDS >> Mysql

`~` (tilde) operator i boolsk fuldtekstsøgning i MySQL opfører sig ikke som angivet på MySQL-udviklerwebstedet

Selvom der ikke er dokumentation for mit svar nogen steder, er jeg efter grundige eksperimenter nået frem til denne mest logiske konklusion -

Tilstedeværelsen af ​​"+"-operatoren ophæver enhver effekt af "~"-operatoren

Jeg har opdateret min tabel fruits med følgende værdier -

SELECT * FROM fruits;
+----+-------------------------+
| id | name                    |
+----+-------------------------+
|  1 | apple orange watermelon |
|  2 | apple mango pomegranate |
|  3 | apple mango banana      |
|  4 | mango kiwi pomegranate  |
|  5 | mango guava watermelon  |
|  6 | apple banana kiwi       |
+----+-------------------------+
 

Forespørgsel 1:

SELECT id, name FROM fruits
    -> WHERE MATCH(name) AGAINST
    -> ('apple mango ~pomegranate'
    -> IN BOOLEAN MODE);
+----+-------------------------+
| id | name                    |
+----+-------------------------+
|  3 | apple mango banana      |
|  1 | apple orange watermelon |
|  5 | mango guava watermelon  |
|  6 | apple banana kiwi       |
|  2 | apple mango pomegranate |
|  4 | mango kiwi pomegranate  |
+----+-------------------------+
 

Forespørgsel 2:

SELECT id, name FROM fruits
    -> WHERE MATCH(name) AGAINST
    -> ('apple ~pomegranate'
    -> IN BOOLEAN MODE);
+----+-------------------------+
| id | name                    |
+----+-------------------------+
|  1 | apple orange watermelon |
|  3 | apple mango banana      |
|  6 | apple banana kiwi       |
|  2 | apple mango pomegranate |
+----+-------------------------+
 

Forespørgsel 3:

SELECT id, name FROM fruits -> WHERE MATCH(name) AGAINST -> ('mango ~pomegranate' -> IN BOOLEAN MODE); +----+-------------------------+ | id | name | +----+-------------------------+ | 3 | apple mango banana | | 5 | mango guava watermelon | | 2 | apple mango pomegranate | | 4 | mango kiwi pomegranate | +----+-------------------------+

Her, i forespørgslerne 1, 2 og 3 går ingen operator forud for værdierne apple og mango og ~ operatoren går forud for værdien pomegranate . Dette sikrer, at rækkerne med ordet pomegranate er placeret lavere end andre.

Forespørgsel 4:

SELECT id, name FROM fruits -> WHERE MATCH(name) AGAINST -> ('+apple +mango ~pomegranate' -> IN BOOLEAN MODE); +----+-------------------------+ | id | name | +----+-------------------------+ | 2 | apple mango pomegranate | | 3 | apple mango banana | +----+-------------------------+

Forespørgsel 5:

SELECT id, name FROM fruits
    -> WHERE MATCH(name) AGAINST
    -> ('+apple ~pomegranate'
    -> IN BOOLEAN MODE);
+----+-------------------------+
| id | name                    |
+----+-------------------------+
|  1 | apple orange watermelon |
|  2 | apple mango pomegranate |
|  3 | apple mango banana      |
|  6 | apple banana kiwi       |
+----+-------------------------+
 

Forespørgsel 6:

SELECT id, name FROM fruits
    -> WHERE MATCH(name) AGAINST
    -> ('+mango ~pomegranate'
    -> IN BOOLEAN MODE);
+----+-------------------------+
| id | name                    |
+----+-------------------------+
|  2 | apple mango pomegranate |
|  3 | apple mango banana      |
|  4 | mango kiwi pomegranate  |
|  5 | mango guava watermelon  |
+----+-------------------------+
 

Her, i forespørgslerne 4, 5 og 6 + operatoren går forud for værdierne apple og mango og ~ operatoren går forud for værdien pomegranate . Klart tilstedeværelsen af ​​+ operatør annullerer enhver effekt af ~ operatør.

Forespørgsel 7:

SELECT id, name FROM fruits -> WHERE MATCH(name) AGAINST -> ('+apple +mango <pomegranate' -> IN BOOLEAN MODE); +----+-------------------------+ | id | name | +----+-------------------------+ | 3 | apple mango banana | | 2 | apple mango pomegranate | +----+-------------------------+

Forespørgsel 8:

SELECT id, name FROM fruits -> WHERE MATCH(name) AGAINST -> ('+apple <pomegranate' -> IN BOOLEAN MODE); +----+-------------------------+ | id | name | +----+-------------------------+ | 1 | apple orange watermelon | | 3 | apple mango banana | | 6 | apple banana kiwi | | 2 | apple mango pomegranate | +----+-------------------------+

Forespørgsel 9:

SELECT id, name FROM fruits -> WHERE MATCH(name) AGAINST -> ('+mango <pomegranate' -> IN BOOLEAN MODE); +----+-------------------------+ | id | name | +----+-------------------------+ | 3 | apple mango banana | | 5 | mango guava watermelon | | 2 | apple mango pomegranate | | 4 | mango kiwi pomegranate | +----+-------------------------+

Her, i forespørgslerne 7, 8 og 9 + operatoren går forud for værdierne apple og mango og < operatoren går forud for værdien pomegranate . Dette sikrer, at rækkerne med ordet pomegranate er placeret lavere end andre.

Det, der kan udledes herfra, er, at -hvis + operatør er til stede, brug < operator i stedet for ~ operatør

OPDATERING

Efter omfattende beregning har jeg lavet tabellen fruits_score_count som viser score af hver fruits når du er færdig med Boolean FULLTEXT-søgning.

SELECT * FROM fruits_score_count; +----+-------------+---------------------+----------------------+ | id | fruit_name | row_numbers_matched | score | +----+-------------+---------------------+----------------------+ | 1 | apple | 4 | 0.031008131802082062 | | 2 | banana | 2 | 0.22764469683170319 | | 3 | guava | 1 | 0.6055193543434143 | | 4 | kiwi | 2 | 0.22764469683170319 | | 5 | mango | 4 | 0.031008131802082062 | | 6 | orange | 1 | 0.6055193543434143 | | 7 | pomegranate | 2 | 0.22764469683170319 | | 8 | watermelon | 2 | 0.22764469683170319 | +----+-------------+---------------------+----------------------+

Forespørgsel 1:

SELECT id, name, score FROM
    -> (SELECT id, name, MATCH(name) AGAINST
    -> ('apple mango ~pomegranate' IN BOOLEAN MODE)
    -> AS score FROM fruits ORDER BY score DESC)
    -> AS temp WHERE score != 0;
+----+-------------------------+----------------------+
| id | name                    | score                |
+----+-------------------------+----------------------+
|  3 | apple mango banana      | 0.062016263604164124 |
|  1 | apple orange watermelon | 0.031008131802082062 |
|  5 | mango guava watermelon  | 0.031008131802082062 |
|  6 | apple banana kiwi       | 0.031008131802082062 |
|  2 | apple mango pomegranate |  -0.7103390693664551 |
|  4 | mango kiwi pomegranate  |  -0.7413471937179565 |
+----+-------------------------+----------------------+
 

Forespørgsel 2:

SELECT id, name, score FROM -> (SELECT id, name, MATCH(name) AGAINST -> ('apple ~pomegranate' IN BOOLEAN MODE) -> AS score FROM fruits ORDER BY score DESC) -> AS temp WHERE score != 0; +----+-------------------------+----------------------+ | id | name | score | +----+-------------------------+----------------------+ | 1 | apple orange watermelon | 0.031008131802082062 | | 3 | apple mango banana | 0.031008131802082062 | | 6 | apple banana kiwi | 0.031008131802082062 | | 2 | apple mango pomegranate | -0.7413471937179565 | +----+-------------------------+----------------------+

Forespørgsel 3:

SELECT id, name, score FROM
    -> (SELECT id, name, MATCH(name) AGAINST
    -> ('mango ~pomegranate' IN BOOLEAN MODE)
    -> AS score FROM fruits ORDER BY score DESC)
    -> AS temp WHERE score != 0;
+----+-------------------------+----------------------+
| id | name                    | score                |
+----+-------------------------+----------------------+
|  3 | apple mango banana      | 0.031008131802082062 |
|  5 | mango guava watermelon  | 0.031008131802082062 |
|  2 | apple mango pomegranate |  -0.7413471937179565 |
|  4 | mango kiwi pomegranate  |  -0.7413471937179565 |
+----+-------------------------+----------------------+
 

Forespørgsel 4:

SELECT id, name, score FROM -> (SELECT id, name, MATCH(name) AGAINST -> ('+apple +mango ~pomegranate' IN BOOLEAN MODE) -> AS score FROM fruits ORDER BY score DESC) -> AS temp WHERE score != 0; +----+-------------------------+----------------------+ | id | name | score | +----+-------------------------+----------------------+ | 2 | apple mango pomegranate | 0.062016263604164124 | | 3 | apple mango banana | 0.062016263604164124 | +----+-------------------------+----------------------+

Forespørgsel 5:

SELECT id, name, score FROM
    -> (SELECT id, name, MATCH(name) AGAINST
    -> ('+apple ~pomegranate' IN BOOLEAN MODE)
    -> AS score FROM fruits ORDER BY score DESC)
    -> AS temp WHERE score != 0;
+----+-------------------------+----------------------+
| id | name                    | score                |
+----+-------------------------+----------------------+
|  1 | apple orange watermelon | 0.031008131802082062 |
|  2 | apple mango pomegranate | 0.031008131802082062 |
|  3 | apple mango banana      | 0.031008131802082062 |
|  6 | apple banana kiwi       | 0.031008131802082062 |
+----+-------------------------+----------------------+
 

Forespørgsel 6:

SELECT id, name, score FROM
    -> (SELECT id, name, MATCH(name) AGAINST
    -> ('+mango ~pomegranate' IN BOOLEAN MODE)
    -> AS score FROM fruits ORDER BY score DESC)
    -> AS temp WHERE score != 0;
+----+-------------------------+----------------------+
| id | name                    | score                |
+----+-------------------------+----------------------+
|  2 | apple mango pomegranate | 0.031008131802082062 |
|  3 | apple mango banana      | 0.031008131802082062 |
|  4 | mango kiwi pomegranate  | 0.031008131802082062 |
|  5 | mango guava watermelon  | 0.031008131802082062 |
+----+-------------------------+----------------------+
 

Forespørgsel 7:

SELECT id, name, score FROM -> (SELECT id, name, MATCH(name) AGAINST -> ('+apple +mango <pomegranate' IN BOOLEAN MODE) -> AS score FROM fruits ORDER BY score DESC) -> AS temp WHERE score != 0; +----+-------------------------+----------------------+ | id | name | score | +----+-------------------------+----------------------+ | 3 | apple mango banana | 0.062016263604164124 | | 2 | apple mango pomegranate | -0.7103390693664551 | +----+-------------------------+----------------------+

Forespørgsel 8:

SELECT id, name, score FROM -> (SELECT id, name, MATCH(name) AGAINST -> ('+apple <pomegranate' IN BOOLEAN MODE) -> AS score FROM fruits ORDER BY score DESC) -> AS temp WHERE score != 0; +----+-------------------------+----------------------+ | id | name | score | +----+-------------------------+----------------------+ | 1 | apple orange watermelon | 0.031008131802082062 | | 3 | apple mango banana | 0.031008131802082062 | | 6 | apple banana kiwi | 0.031008131802082062 | | 2 | apple mango pomegranate | -0.7413471937179565 | +----+-------------------------+----------------------+

Forespørgsel 9:

SELECT id, name, score FROM -> (SELECT id, name, MATCH(name) AGAINST -> ('+mango <pomegranate' IN BOOLEAN MODE) -> AS score FROM fruits ORDER BY score DESC) -> AS temp WHERE score != 0; +----+-------------------------+----------------------+ | id | name | score | +----+-------------------------+----------------------+ | 3 | apple mango banana | 0.031008131802082062 | | 5 | mango guava watermelon | 0.031008131802082062 | | 2 | apple mango pomegranate | -0.7413471937179565 | | 4 | mango kiwi pomegranate | -0.7413471937179565 | +----+-------------------------+----------------------+

Her opfører Query 1, Query 2, Query 3, Query 7, Query 8, Query 9 sig som forventet.

Men fra forespørgsel 4, forespørgsel 5, forespørgsel 6 er det klart, at -

I nærværelse af + operator foran en værdi med ~ operatør gør dybest set værdien usynlig.

Også omhyggelig observation afslører, at -

x ~y og +x <y er tilsvarende

YDERLIGERE EKSPERIMENTATION

Forespørgsel 1:

SELECT id, name, score FROM -> (SELECT id, name, MATCH(name) AGAINST -> ('+mango apple ~pomegranate' IN BOOLEAN MODE) -> AS score FROM fruits ORDER BY score DESC) -> AS temp WHERE score != 0; +----+-------------------------+----------------------+ | id | name | score | +----+-------------------------+----------------------+ | 3 | apple mango banana | 0.062016263604164124 | | 4 | mango kiwi pomegranate | 0.031008131802082062 | | 5 | mango guava watermelon | 0.031008131802082062 | | 2 | apple mango pomegranate | -0.7103390693664551 | +----+-------------------------+----------------------+
  • Række 1 med id = 3 får maksimal score, som er summen af ​​scores af mango og apple .
  • Række 2 med id = 4 får anden maksimumscore, som er scoren for mango . Tilstedeværelse af + operatør foran mango laver ~pomegranate søgesætning er irrelevant.
  • Række 3 med id = 5 får samme score som række 2. Men den er placeret lavere end række 2, fordi når scores er ens, rangeres rækker i stigende rækkefølge efter primary key , her id er primary key .
  • Række 4 med id = 2 får laveste score og kommer dermed sidst. Her siden ordet apple er til stede, og i søgesætningen er der ingen + operator foran apple , derfor ~pomegranate i søgesætningen tages i betragtning, hvilket sænker scoren markant.

Forespørgsel 2:

SELECT id, name, score FROM -> (SELECT id, name, MATCH(name) AGAINST -> ('+mango apple <pomegranate' IN BOOLEAN MODE) -> AS score FROM fruits ORDER BY score DESC) -> AS temp WHERE score != 0; +----+-------------------------+----------------------+ | id | name | score | +----+-------------------------+----------------------+ | 3 | apple mango banana | 0.062016263604164124 | | 5 | mango guava watermelon | 0.031008131802082062 | | 2 | apple mango pomegranate | -0.7103390693664551 | | 4 | mango kiwi pomegranate | -0.7413471937179565 | +----+-------------------------+----------------------+

Dette illustrerer igen, at < operatøren træder i kraft selv i nærvær af + operatør.

Dette forstærker yderligere min tidligere observation, at -

hvis + operatør er til stede, brug < operator i stedet for ~ operatør




  1. MySQL - Sådan vises rækkeværdi som kolonnenavn ved hjælp af concat og group_concat

  2. Hvordan kan jeg få forfædre-id'er til vilkårlig rekursionsdybde i én SQL-forespørgsel?

  3. PostgreSQL 8.4 giver DML-privilegier på alle tabeller til en rolle

  4. På virkningen af ​​helsides skriver