re1man
re1man

Reputation: 2367

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 required?

Upvotes: 0

Views: 148

Answers (2)

Brendan Bullen
Brendan Bullen

Reputation: 11819

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

Upvotes: 1

arnep
arnep

Reputation: 6241

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.

Upvotes: 0

Related Questions