MySQL: Finding multiple words in full text search - exact matches only
I'm writing some code to automatically tag some articles.
I have an array of tags and a table of articles. I run the following query to check for headlines matching a tag:
SELECT headline
FROM `news`
WHERE MATCH(headline) AGAINST ("+Green +Day" IN BOOLEAN MODE)
This finds all articles with the exact phrase 'Green Day' in the headline - without the first +, I get articles that contain just the word 'Green'.
This isn't perfect and some tags result in inaccurate results - eg a tag called开发者_StackOverflow Die! Die! Die!
(don't ask) returns every headline with the word 'die' in it.
Is there something obvious I'm missing here? All I want is to get headlines which contain the entire phrase, in the exact way it's entered.
As far as I can see in the docs, using quotes should be enough. From the examples on the docs page:
"some words"
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 quotation marks that enclose the search string itself.
You have to put extra single quote around double quote which means exact word query. +
operator just means AND
logic.
SELECT headline
FROM `news`
WHERE MATCH(headline) AGAINST ('"+Green +Day"' IN BOOLEAN MODE)
@Matt - The cause of the match for "Terror" using Mitch's code is because you didn't left space after the first % character. The correct syntax of exact search for a single word would look like this
SELECT headline FROM news WHERE headline LIKE '% Green Day %'
This will manage your request of retrieving searched words entered in the exact same way.
If you want to match the entire phrase then you should do something like:
SELECT headline FROM news WHERE headline LIKE '%Green Day%'
that will return you results with the phrase "Green Day" in the headline.
精彩评论