mysql search. Require all keywords
I have this sql query:
SELECT DISTINCT url FROM paragraphs WHERE
MATCH (title) AGAINST('$query') ORDER BY
MATCH (title) AGAINST('$query') DESC
What happens though is when I search, for example, for "john smith" I get "john" "john chambers" ...etc. How can I make both words requi开发者_Go百科red?
Using +
in a Boolean search should make all words required:
<?php
$query = str_replace(" "," +",$query);
$sql = "SELECT DISTINCT url FROM paragraphs WHERE
MATCH (title) AGAINST('$query' IN BOOLEAN MODE))
ORDER BY score DESC;"
?>
Dev.MySQL.com: 11.9.2. Boolean Full-Text Searches
You could add an additional LIKE
clause to your where to filter out all unwanted matches:
SELECT DISTINCT url FROM paragraphs WHERE
MATCH (title) AGAINST('$query') AND title LIKE "%$query%"
ORDER BY MATCH (title) AGAINST('$query') DESC
Thus you have scoring and only the results matching exactly.
精彩评论