开发者

mySQL fulltext learning

开发者 https://www.devze.com 2023-04-09 23:23 出处:网络
I am creating an online FAQ type system using php and mySQL.The following SQL is what is used to find relevant questions based on what the user entered as $term.

I am creating an online FAQ type system using php and mySQL. The following SQL is what is used to find relevant questions based on what the user entered as $term.

When a user searches for something and clicks on a relevant question to display the answer they get the chance to rate the question given based on what they searched for.

The first part of the SQL gets relevant question ID's from the actual questions table. The second part looks at the ratings table and tries to match what people have previously searched for to find relevant question ID's.

The union of these results is then used to get the actual question titles stored in the database.

(SELECT id_question, 
MATCH(question, tags) AGAINST ('%$term%') as rank
FROM question
WHERE MATCH(question, tags) AGAINST ('%$term%')
AND category = '$category')

UNION

(SELECT id_question,  
MATCH(customer_search_query) AGAINST ('%$term%') as rank
FROM rating
WHERE MATCH(customer_search_query) AGAINST ('%$term%')
AND (customer_rating = 1))
ORDER BY rank DESC LIMIT 5;");

The problem I'm having is the system isn't really learning correctly. For example, if I type "three users", the system will find a match in the questions table.

If I type "3 users", the system will find a match in the questions table (based on the keyword "users"). If I click "yes this answered my question" it will store "3 users" in the ratings table associating it with the question "three users".

The issue is that "3" only becomes associated wi开发者_如何学JAVAth "three users". Is there a way to associate the number 3 in this case with every instance of the word "three".


You don't use wildcards in match against in natural language mode (the default).

Use this query instead:

(SELECT id_question, 
MATCH(question, tags) AGAINST ('$term') as rank
FROM question
WHERE MATCH(question, tags) AGAINST ('$term')
AND category = '$category')

UNION ALL   <<-- faster than UNION.

(SELECT id_question,  
MATCH(customer_search_query) AGAINST ('$term') as rank
FROM rating
WHERE MATCH(customer_search_query) AGAINST ('$term')
AND (customer_rating = 1))
ORDER BY rank DESC LIMIT 5;");

You can use search modifiers in boolean mode.

Also match against will not search for stopwords and words shorter than 4 chars.
You when you use $term = "3 users", MySQL will only look for users and ignore the 3.

If you want to search for 3 you'll have to revert to LIKE '% 3 %'

See:
http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html
http://dev.mysql.com/doc/refman/5.0/en/fulltext-stopwords.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号