在Sphinx中使用文本/字符串值创建过滤器

Rou*_*ute 8 php sphinx

我安装了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));
Run Code Online (Sandbox Code Playgroud)

我如何才能通过搜索type = 4,过滤由select1cartoon最后在label6children

Jer*_*lix 16

我相信你要做的是将字符串过滤为属性.参考Sphinx FAQ,他们概述了程序

如何在没有字符串属性的情况下按字符串列进行筛选,排序或分组?

除了对多个索引进行精确的arbtrary-length排序外,您可以执行所有这些操作.

要过滤和分组,可以使用唯一的数字ID替换字符串.有时可以在数据库中创建查找字典(例如,对于城市或国家的固定列表),或者甚至使用现有字典,在该字典中用其ID替换字符串,然后对该ID进行过滤和分组.如果没有,您总是可以用其校验和替换字符串,例如.在索引时从MD5()获取CRC32()或(任意)64位(无需更改表!),分别使用sql_attr_uint或sql_attr_bigint存储它,然后对该校验和属性进行过滤或分组.(请注意,如果您有数百万个字符串但MD5()碰撞几乎没有机会,则存在CRC32()碰撞的可能性.)

所以,在我的sphinx.conf中,我可能会有以下内容......

sql_query = SELECT CRC32(string_field) AS `string_field` FROM `table`

sql_attr_uint = string_field
Run Code Online (Sandbox Code Playgroud)

然后在PHP中,我会在字段上应用过滤器,如此...

$sphinx->SetFilter('string_field', array(crc32( 'filter_string' ));
Run Code Online (Sandbox Code Playgroud)

-

不幸的是,当转换为crc32时,PHP有一个恼人的问题(bug?)...涉及无符号整数或其他东西.

我使用以下函数正确转换

class Encode {
    public static function crc32($val){
        $checksum = crc32($val);
        if($checksum < 0) $checksum += 4294967296;
        return $checksum;
    }
}
Run Code Online (Sandbox Code Playgroud)

-

小心字符大小写!您可以选择在索引时将列转换为小写,例如.

sql_query = SELECT CRC32(LOWER(string_field)) AS `string_field` FROM `table`
Run Code Online (Sandbox Code Playgroud)

和搜索......

$sphinx->SetFilter('string_field', array(crc32(strtolower( 'Filter_String' )));
Run Code Online (Sandbox Code Playgroud)