如何在搜索查询中使用连字符进行全文搜索 [英] How to allow fulltext searching with hyphens in the search query
问题描述
我有一些关键字,比如some-or-other,其中的连字符在我的mysql数据库搜索中很重要。我目前正在使用全文本功能。
有没有办法逃避连字符?
我知道一个选项是在 myisam / ftdefs.h 文件中注释 #define HYPHEN_IS_DELIM
,但不幸的是我的主机确实不允许这样做。是否还有其他选择?
编辑3-8-11
下面是我现在的代码:
$ 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分数
FROM table_name
WHERE MATCH(coln1,coln2,coln3)AGAINST('$ keyword_safe_fix')
ORDER BY得分DESC
;
From here http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html p>
用破折号或连字符查找单词的一种解决方法是在布尔模式中使用全文搜索,并用连字符/短划线用双引号将单词括起来。
或者从这里 http://bugs.mysql.com /bug.php?id=2095
还有另一种解决方法。它最近被添加到手册中:
修改字符集文件:这不需要重新编译,true_word_char()宏
使用字符类型表来区分字母和数字其他
字符您可以编辑其中一个字符集XML
文件中的内容,以指定' - '是一个字母。然后使用给定的字符集为您的
FULLTEXT索引。
还没有尝试过。
编辑:更多来自此处的其他信息 http://dev.mysql.com/doc/refman/5.0 /en/fulltext-boolean.html
包含在双引号()字符中的短语仅匹配字面上包含该短语的行,因为它是键入的。全文引擎将短语拆分为单词并在FULLTEXT索引中执行单词搜索。在MySQL 5.0.3之前,引擎会执行一个substrin g搜索找到的记录中的短语,因此匹配必须在短语中包含非单词字符。从MySQL 5.0.3开始,非字符字符不需要完全匹配:短语搜索仅需要匹配包含与短语完全相同的单词并且顺序相同。例如,测试短语与MySQL 5.0.3中的测试,短语匹配,但不匹配。
如果短语不包含索引中的单词,结果是空的。例如,如果所有单词都是停用词或短于索引单词的最小长度,则结果为空。
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.
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?
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
";
From here 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.
Or from here http://bugs.mysql.com/bug.php?id=2095
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. "
Have not tried it on my own.
Edit: Here is some more additional info from here http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html
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.
这篇关于如何在搜索查询中使用连字符进行全文搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!