I have created the following table fruits -
CREATE TABLE `fruits` (
`id` tinyint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `ft_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Then I entered the following values in table fruits -
SELECT * FROM fruits;
+----+---------------+
| id | name |
+----+---------------+
| 1 | apple, orange |
| 2 | apple, mango |
| 3 | mango, kiwi |
| 4 | mango, guava |
| 5 | apple, banana |
+----+---------------+
Now I run the following three SQL queries -
Query 1:
SELECT id, name FROM fruits
-> WHERE MATCH(name) AGAINST
-> ('+apple' IN BOOLEAN MODE);
+----+---------------+
| id | name |
+----+---------------+
| 1 | apple, orange |
| 2 | apple, mango |
| 5 | apple, banana |
+----+---------------+
Query 2:
SELECT id, name FROM fruits
-> WHERE MATCH(name) AGAINST
-> ('+apple -orange' IN BOOLEAN MODE);
+----+---------------+
| id | name |
+----+---------------+
| 2 | apple, mango |
| 5 | apple, banana |
+----+---------------+
Query 3:
SELECT id, name FROM fruits
-> WHERE MATCH(name) AGAINST
-> ('+apple ~orange' IN BOOLEAN MODE);
+----+---------------+
| id | name |
+----+---------------+
| 1 | apple, orange |
| 2 | apple, mango |
| 5 | apple, banana |
+----+---------------+
As per MySQL developer website following is the function of ~ (tilde) operator in 'Boolean Full-Text Searches'
https://dev.mysql.com/doc/refman/8.0/en/fulltext-boolean.html
- '+apple ~macintosh'
Find rows that contain the word “apple”, but if the row also contains the word “macintosh”, rate it lower than if row does not. This is “softer” than a search for '+apple -macintosh', for which the presence of “macintosh” causes the row not to be returned at all.
I have tried the ~ (tilde) operator in 'Query 3' but the output is certainly not what is expected. Here, the expected behavior is row with id = 1 coming at last.
P.S. I am using MySQL version - 8.0.26-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))
Though there is no documentation regarding my answer anywhere, after thorough experimentation I have arrived at this most logical conclusion -
Presence of '+' operator nullifies any effect of '~' operator
I have updated my table
fruitswith the following values -Query 1:
Query 2:
Query 3:
Here, in the queries 1, 2 & 3 no operator precedes the values
appleandmangoand~operator precedes the valuepomegranate. This makes sure that the rows having the wordpomegranateare ranked lower than others.Query 4:
Query 5:
Query 6:
Here, in the queries 4, 5 & 6
+operator precedes the valuesappleandmangoand~operator precedes the valuepomegranate. Clearly the presence of+operator nullifies any effect of~operator.Query 7:
Query 8:
Query 9:
Here, in the queries 7, 8 & 9
+operator precedes the valuesappleandmangoand<operator precedes the valuepomegranate. This makes sure that the rows having the wordpomegranateare ranked lower than others.Thus, what can be deduced from here is that - if
+operator is present, use<operator instead of~operatorUPDATE
Upon extensive calculation I have created the table
fruits_score_countwhich shows thescoreof eachfruitwhen done Boolean FULLTEXT search.Query 1:
Query 2:
Query 3:
Query 4:
Query 5:
Query 6:
Query 7:
Query 8:
Query 9:
Here, Query 1, Query 2, Query 3, Query 7, Query 8, Query 9 behaves as expected.
But from Query 4, Query 5, Query 6 it is clear that -
In the presence of
+operator preceding a value with the~operator basically makes the value invisible.Also careful observation reveals that -
x ~yand+x <yare equivalentFURTHER EXPERIMENTATION
Query 1:
id = 3gets maximum score which is the sum of scores ofmangoandapple.id = 4gets second maximum score which is the score ofmango. Presence of+operator in front ofmangomakes~pomegranateof search phrase irrelevant.id = 5gets the same score as that of Row 2. But it is placed lower than Row 2 because when scores are same, rows are ranked in increasing order ofprimary key, hereidisprimary key.id = 2gets lowest score and hence comes last. Here since the wordappleis present and in the search phrase there is no+operator precedingapple, hence~pomegranatein search phrase is taken into consideration, which lowers the score significantly.Query 2:
This again illustrates that
<operator takes effect even in the presence of+operator.This further reinforces my earlier observation that -
if
+operator is present, use<operator instead of~operator