12.7.1. Boolean Full-Text Searches
MySQL can perform boolean full-text searches using the
IN BOOLEAN MODE modifier:
SELECT * FROM articles WHERE MATCH (title,body)
AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);
| id | title | body |
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
| 2 | How To Use MySQL Well | After you went through a ... |
| 3 | Optimizing MySQL | In this tutorial we will show ... |
| 4 | 1001 MySQL Tricks | 1. Never run mysqld as root. 2. ... |
| 6 | MySQL Security | When configured properly, MySQL ... |
indicate that a word is required to be present or absent,
respectively, for a match to occur. Thus, this query retrieves
all the rows that contain the word “MySQL” but that
do not contain the word
Boolean full-text searches have these characteristics:
They do not use the 50% threshold.
They do not automatically sort rows in order of decreasing
relevance. You can see this from the preceding query result:
The row with the highest relevance is the one that contains
“MySQL” twice, but it is listed last, not
They can work even without a
index, although a search executed in this fashion would be
The minimum and maximum word length full-text parameters
The stopword list applies.
The boolean full-text search capability supports the following
A leading plus sign indicates that this word
must be present in each row that is
A leading minus sign indicates that this word must
not be present in any of the rows that
- operator acts only to exclude
rows that are otherwise matched by other search terms. Thus,
a boolean-mode search that contains only terms preceded by
- returns an empty result. It does not
return “all rows except those containing any of the
By default (when neither
- is specified) the word is optional, but
the rows that contain it are rated higher. This mimics the
MATCH() ... AGAINST() without
IN BOOLEAN MODE modifier.
These two operators are used to change a word's contribution
to the relevance value that is assigned to a row. The
> operator increases the contribution
< operator decreases it. See
the example following this list.
Parentheses group words into subexpressions. Parenthesized
groups can be nested.
A leading tilde acts as a negation operator, causing the
word's contribution to the row's relevance to be negative.
This is useful for marking “noise” words. A row
containing such a word is rated lower than others, but is
not excluded altogether, as it would be with the
The asterisk serves as the truncation (or wildcard)
operator. Unlike the other operators, it should be
appended to the word to be affected.
Words match if they begin with the word preceding the
A phrase that is enclosed within double quote
"’) characters matches only
rows that contain the phrase literally, as it was
typed. The full-text engine splits the phrase
into words, performs a search in the
FULLTEXT index for the words. Non-word
characters need not be matched exactly: Phrase searching
requires only that matches contain exactly the same words as
the phrase and in the same order. For example,
"test phrase" matches
If the phrase contains no words that are in the index, the
result is empty. For example, if all words are either
stopwords or shorter than the minimum length of indexed
words, the result is empty.
The following examples demonstrate some search strings that use
boolean full-text operators:
Find rows that contain at least one of the two words.
Find rows that contain both words.
Find rows that contain the word “apple”, but
rank rows higher if they also contain
Find rows that contain the word “apple” but not
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
-macintosh', for which the presence of
“macintosh” causes the row not to be returned
'+apple +(>turnover <strudel)'
Find rows that contain the words “apple” and
“turnover”, or “apple” and
“strudel” (in any order), but rank “apple
turnover” higher than “apple strudel”.
Find rows that contain words such as “apple”,
“apples”, “applesauce”, or
Find rows that contain the exact phrase “some
words” (for example, rows that contain “some
words of wisdom” but not “some noise
words”). Note that the
"’ characters that enclose
the phrase are operator characters that delimit the phrase.
They are not the quotes that enclose the search string