我安装了 Sphinx Search 作为我的搜索引擎,我正在尝试使用 setFilter()
和 SetSelect()
为搜索添加一些额外的功能,这应该请允许我执行 WHERE/AND
子句。但每当我尝试搜索时,它只会返回结果而不返回任何结果。
这是我的 sphinx.conf:http://pastebin.com/M6Kd71u0
这是 PHP 代码:
require("sphinxapi.php");
$host = "localhost";
$port = 9312;
$index = "llgenre";
$select1 = "cartoon";
$label6 = "children";
$type = 4;
$limit = 20;
$ranker = SPH_RANK_PROXIMITY_BM25;
$mode = SPH_MATCH_ALL;
$sphinx = new SphinxClient();
$sphinx->setServer($host, $port);
$sphinx->setConnectTimeout(0);
$sphinx->setMatchMode($mode);
$sphinx->setRankingMode($ranker);
$sphinx->setSelect('*, select1="'.$select1.'" AND label6="'.$label6.'" AS mycond');
$sphinx->setFilter('mycond', array(1));
$res = $sphinx->query($type, $index);
die(var_dump($res));
我如何通过 type = 4
搜索,通过 select1
和 cartoon
过滤,最后在 label6
上使用 child
?
最佳答案
我相信您正在尝试做的是将字符串过滤为属性。引用Sphinx FAQ , 他们概述了程序
How do I filter, sort, or group by string column without string attributes?
You can do all of this, except for precise arbtrary-length sorting over several indexes.
To filter and group, you can replace the string with an unique numeric ID. Sometimes its possible to create a lookup dictionary in the database (eg. for fixed lists of cities or countries), or even use an existing one, replace strings with their IDs in that dictionary, then filter and group on that ID. If not, you can always replace the string with its checksum, eg. CRC32() or (any) 64 bits taken from MD5() at indexing time (no need to alter the tables!), store it using sql_attr_uint or sql_attr_bigint respectively, and then filter or group on that checksum attribute. (Note that there's a certain chance of CRC32() collisions if you have millions of strings but practically zero chance of MD5() collisions.)
所以,在我的 sphinx.conf 中,我可能有以下...
sql_query = SELECT CRC32(string_field) AS `string_field` FROM `table`
sql_attr_uint = string_field
然后在 PHP 中,我会像这样在字段上应用过滤器...
$sphinx->SetFilter('string_field', array(crc32( 'filter_string' ));
--
不幸的是,PHP 在转换为 crc32 时有一个烦人的问题(bug?)...涉及无符号整数或其他东西..
我使用下面的函数来正确转换
class Encode {
public static function crc32($val){
$checksum = crc32($val);
if($checksum < 0) $checksum += 4294967296;
return $checksum;
}
}
--
注意字符大小写!您可以选择在索引时将列转换为小写,例如。
sql_query = SELECT CRC32(LOWER(string_field)) AS `string_field` FROM `table`
并搜索...
$sphinx->SetFilter('string_field', array(crc32(strtolower( 'Filter_String' )));
关于php - 使用文本/字符串值在 Sphinx 中创建过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5736037/