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

标签 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));

我如何通过 type = 4 搜索,通过 select1cartoon 过滤,最后在 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/

相关文章:

php - Azure 上的 SimplePie 不解析 https feed

php - 从 PHP 中的私有(private)文件夹渲染 HTML 页面

php - 在匹配使用变量 : PHP:preg_match 形成的模式时转义字符的特殊含义

php - SphinxQL-查询生成器。 PHP 警告 : Packets out of order. 预计收到 0 1。数据包大小=0

php - 从 PHP 调用 sphinx 索引器

php - Joomla 将 mysql 表重新索引为函数

javascript - 使 DIV 从中心 div 向外扩展而不是向内扩展

php - 如何使用 Group By 和 Group_Concat 进行 Sphinx 搜索?

php - sphinx表在其他基础上查表

php - 如何在 YII 中使用 Sphinx 搜索?