SphinxQL和SPH_匹配任何
我正在使用SphinxQL查询Sphinxsearch引擎。我想模拟在php API中实现的SPH_MATCH_ANY,如下所示:SphinxQL和SPH_匹配任何,sphinx,Sphinx,我正在使用SphinxQL查询Sphinxsearch引擎。我想模拟在php API中实现的SPH_MATCH_ANY,如下所示: $cl->SetMatchMode(SPH_MATCH_ANY); $cl->Query("test query", "index"); =>搜索与“测试”或“查询”匹配的文档 因此,我编写了一个函数(php),用管道(|)替换空格和其他特殊字符,以便与SphinxQL一起使用: function formatQuery($str) { retu
$cl->SetMatchMode(SPH_MATCH_ANY);
$cl->Query("test query", "index");
=>搜索与“测试”或“查询”匹配的文档
因此,我编写了一个函数(php),用管道(|)替换空格和其他特殊字符,以便与SphinxQL一起使用:
function formatQuery($str) {
return trim(preg_replace('/[^-_\'a-z0-9]+/', '|', $str), ' |');
}
$str = "test query";
$sql = "SELECT * FROM index WHERE MATCH('" . addslashes(formatQuery($str)) . "')";
=>从匹配的索引中选择*('test | query')
问题是,对于某些字符,如-(减号),它可能会中断查询,例如:
$str = "i-phone is great";
$sql = "SELECT * FROM index WHERE MATCH('" . addslashes(formatQuery($str)) . "')";
=>从匹配的索引中选择*('i-phone |很棒')
=>好的
=>从匹配的索引中选择*('i |-| phone |是|伟大的')
=>由于“|-|”而中断的查询
您知道更好的方法使SphinxQL查询在SPH_MATCH_ANY模式下工作吗?还是一个更好的regexp使其适用于所有情况
我知道我可以使用更严格的regexp,如下所示:
preg_replace('/[^a-z0-9]+/', '|', $str)
但它会在“i | phone | is | great”中拆分字符串,比如“i-phone是伟大的”,我不希望这样
谢谢,,
Nico一种方法可能是使用quorom
$sql = "SELECT * FROM index WHERE MATCH('\"" . addslashes($str) . "\"/1')";
您需要将-添加到charset_表tho中,使其成为单词的一部分 另一个选项是
$query = preg_replace('/(\w+?)[-\'](\w+?)/','$1~$2',$query);
$query = preg_replace('/[^\w\~]+/','|',$query);
$query = preg_replace('/(\w+~\w[\w~]*)/e','"\"".str_replace("~"," ","$1")."\""',$query);
把它变成一个短语 为什么不使用Sphinx API方法呢?很好的技巧。对于使用Unicode字符串的字符串(例如带有重音字符如“èèù”),可以在regexp中使用“u”修饰符:
$query=preg_replace('/(\w+?)[-\'](\w+?)/u','1~$2',$query)
$query=preg_replace('/[^\w\~]+/u','|',$query)代码>$query=preg\u replace('/(\w+~\w[\w~]*)/ue','\''.str\u replace(“~”,'',“$1”)。“\'',$query)代码>
$query = preg_replace('/(\w+?)[-\'](\w+?)/','$1~$2',$query);
$query = preg_replace('/[^\w\~]+/','|',$query);
$query = preg_replace('/(\w+~\w[\w~]*)/e','"\"".str_replace("~"," ","$1")."\""',$query);