如何允许在搜索查询中使用连字符进行全文搜索
我有诸如some-or-other"之类的关键字,其中连字符在通过我的 mysql 数据库进行搜索时很重要.我目前正在使用全文功能.
I have keywords like "some-or-other" where the hyphens matter in the search through my mysql database. I'm currently using the fulltext function.
有没有办法转义连字符?我知道一种选择是在 myisam/ftdefs.h 文件中注释掉 #define HYPHEN_IS_DELIM
,但不幸的是我的主机不允许这样做.还有其他选择吗?
Is there a way to escape the hyphen character?
I know that one option is to comment out #define HYPHEN_IS_DELIM
in the myisam/ftdefs.h file, but unfortunately my host does not allow this. Is there another option out there?
编辑 3-8-11这是我现在拥有的代码:
Edit 3-8-11 Here's the code I have right now:
$search_input = $_GET['search_input'];
$keyword_safe = mysql_real_escape_string($search_input);
$keyword_safe_fix = "*'"" . $keyword_safe . ""'*";
$sql = "
SELECT *,
MATCH(coln1, coln2, coln3) AGAINST('$keyword_safe_fix') AS score
FROM table_name
WHERE MATCH(coln1, coln2, coln3) AGAINST('$keyword_safe_fix')
ORDER BY score DESC
";
推荐答案
从这里 http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
查找带有破折号或连字符的单词的一种解决方案是使用布尔模式中的全文搜索,并将带有连字符/破折号的单词用双引号括起来.
One solution to find a word with a dashes or hyphens in is to use FULL TEXT SEARCH IN BOOLEAN MODE, and to enclose the word with the hyphen / dash in double quotes.
或者从这里http://bugs.mysql.com/bug.php?id=2095
还有另一种解决方法.它最近添加到手册中:"修改字符集文件:这不需要重新编译.true_word_char() 宏使用字符类型"表来区分字母和数字人物..您可以在其中一种字符集 XML 中编辑内容文件来指定-"是一个字母".然后使用给定的字符集为您全文索引."
There is another workaround. It was recently added to the manual: " Modify a character set file: This requires no recompilation. The true_word_char() macro uses a "character type" table to distinguish letters and numbers from other characters. . You can edit the contents in one of the character set XML files to specify that '-' is a "letter." Then use the given character set for your FULLTEXT indexes. "
我自己没有尝试过.
这里有更多附加信息 http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html
包含在双引号 ("") 字符中的短语仅匹配字面上包含该短语的行,因为它是键入的.全文引擎将短语拆分为单词并在 FULLTEXT 索引中执行搜索单词.在 MySQL 5.0.3 之前,引擎然后对找到的记录中的短语执行子字符串搜索,因此匹配必须包含短语中的非单词字符.从 MySQL 5.0.3 开始,不需要非单词字符完全匹配:短语搜索只要求匹配包含与短语完全相同的单词,并且顺序相同.例如,测试短语"匹配 MySQL 5.0.3 中的测试,短语",但不匹配之前.
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 and performs a search in the FULLTEXT index for the words. Prior to MySQL 5.0.3, the engine then performed a substring search for the phrase in the records that were found, so the match must include nonword characters in the phrase. As of MySQL 5.0.3, nonword 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 "test, phrase" in MySQL 5.0.3, but not before.
如果短语不包含索引中的单词,则结果为空.例如,如果所有单词都是停用词或短于索引词的最小长度,则结果为空.
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.
相关文章