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

发布于 2024-11-02 10:26:31 字数 1036 浏览 1 评论 0原文

我安装了 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 过滤孩子

I have Sphinx Search installed as my search engine and I'm trying to add a few extra features to the search using setFilter() and SetSelect() which should allow me to do WHERE/AND clauses. But whenever I try a search, it returns no results instead of results.

Here is my sphinx.conf: http://pastebin.com/M6Kd71u0

And here's the PHP code:

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

How can I search by type = 4, filter by select1 with cartoon and finally on label6 with children?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深已缘浅 2024-11-09 10:26:31

我相信您尝试做的是将字符串作为属性进行过滤。他们参考 Sphinx 常见问题解答,概述了该过程

如何过滤、排序或分组
没有字符串的字符串列
属性?

您可以执行所有这些操作,除了
精确任意长度排序
多个索引。

要过滤和分组,您可以替换
具有唯一数字 ID 的字符串。
有时可以创建一个
在数据库中查找字典(例如
对于固定的城市列表或
国家),甚至使用现有的
一、将字符串替换为它们的 ID
该字典,然后过滤并分组
在那个身份证上。如果没有,您可以随时
将字符串替换为其校验和,
例如。 CRC32() 或(任意)64 位
在索引时来自 MD5() (不需要
更改表格!),使用
sql_attr_uint 或 sql_attr_bigint
分别,然后过滤或分组
在该校验和属性上。
(请注意
有一定几率出现CRC32()
如果你有数百万个,就会发生碰撞
字符串,但几乎为零的机会
MD5() 冲突。)

所以,在我的 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 有一个恼人的问题(bug ?)当转换为crc32时...涉及无符号整数或其他内容的东西..

我使用以下函数来正确转换

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' )));

I believe what you're attempting to do is to filter strings as attributes. Referring to the Sphinx FAQ, they outline the procedure

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.)

So, in my sphinx.conf, I might have the following...

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

sql_attr_uint = string_field

Then in PHP, I would apply a filter on the field like so...

$sphinx->SetFilter('string_field', array(crc32( 'filter_string' ));

--

Unfortunately, PHP has an annoying problem(bug?) when converting to crc32... something involving unsigned integers or something..

I use the following function to convert correctly

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

--

Be careful of character case! You may choose to convert the column to lower case while indexing eg.

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

and searching...

$sphinx->SetFilter('string_field', array(crc32(strtolower( 'Filter_String' )));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文