Sphinx PHP 搜索
我正在进行狮身人面像搜索,但发现了一些非常奇怪的结果。任何帮助表示赞赏。
例如,如果我输入“50”,我会得到:
- 50 Cent
- 50 Lions
- 50 Foot Wave 等。
这很棒,但是当我搜索“50 Ce”时,我会得到:
- Ryczące Dwudziestki
- Spisek
- Bernhard Gal
- Cowabunga Go-Go
等疯狂的结果。另外,当我搜索“50 Cent”时,正确的结果位于顶部,但下面是随机结果。有什么想法吗?
PHP 代码:
$query = $_GET['query'];
if (!empty($query))
{
$sphinx->SetMatchMode(SPH_MATCH_ALL);
$sphinx->AddQuery($query, 'artists');
$sphinx->AddQuery($query, 'variations');
$sphinx->SetFilter('name', array(3));
$sphinx->SetLimits(0, 10);
$result = $sphinx->RunQueries();
echo '<pre>';
switch ($result)
{
case false:
echo 'Query failed: ' . $sphinx->GetLastError() . "\n";
break;
default:
if ($sphinx->GetLastWarning())
{
echo 'WARNING: ' . $sphinx->GetLastWarning() . "\n";
}
if (is_array($result[0]['matches']) && count($result[0]['matches']))
{
foreach ($result[0]['matches'] as $value => $info)
{
$artist = artistDetails($value);
echo $artist['name'] . "\n";
}
}
}
}
Sphinx 索引和来源:
source artists
{
type = mysql
sql_host = localhost
sql_user = user
sql_pass = pass
sql_db = db
sql_port = 3300
sql_query = \
SELECT \
id, name \
FROM artists;
#UNIX_TIMESTAMP(time)
#sql_attr_uint = group_id
#sql_attr_timestamp = time
sql_query_info = SELECT id,name FROM artists WHERE id=$id
}
index artists
{
source = artists
path = /var/sphinx/artists
docinfo = extern
charset_type = utf-8
}
I'm doing a Sphinx search but turning up some really weird results. Any help is appreciated.
So for example if I type "50", I get:
- 50 Cent
- 50 Lions
- 50 Foot Wave, etc.
This is great, but when I search "50 Ce", I get:
- Ryczące Dwudziestki
- Spisek
- Bernhard Gal
- Cowabunga Go-Go
And other crazy results. Also when I search for "50 Cent", the correct result is at the top, but then random results below. Any ideas why?
PHP code:
$query = $_GET['query'];
if (!empty($query))
{
$sphinx->SetMatchMode(SPH_MATCH_ALL);
$sphinx->AddQuery($query, 'artists');
$sphinx->AddQuery($query, 'variations');
$sphinx->SetFilter('name', array(3));
$sphinx->SetLimits(0, 10);
$result = $sphinx->RunQueries();
echo '<pre>';
switch ($result)
{
case false:
echo 'Query failed: ' . $sphinx->GetLastError() . "\n";
break;
default:
if ($sphinx->GetLastWarning())
{
echo 'WARNING: ' . $sphinx->GetLastWarning() . "\n";
}
if (is_array($result[0]['matches']) && count($result[0]['matches']))
{
foreach ($result[0]['matches'] as $value => $info)
{
$artist = artistDetails($value);
echo $artist['name'] . "\n";
}
}
}
}
Sphinx Index and Source:
source artists
{
type = mysql
sql_host = localhost
sql_user = user
sql_pass = pass
sql_db = db
sql_port = 3300
sql_query = \
SELECT \
id, name \
FROM artists;
#UNIX_TIMESTAMP(time)
#sql_attr_uint = group_id
#sql_attr_timestamp = time
sql_query_info = SELECT id,name FROM artists WHERE id=$id
}
index artists
{
source = artists
path = /var/sphinx/artists
docinfo = extern
charset_type = utf-8
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 min_prefix_len 索引配置选项来告诉 sphinx 您希望它对部分单词进行索引和匹配。您可能还需要将enable_star设置为1
http:// www.sphinxsearch.com/docs/current.html#conf-min-prefix-len
启用前缀索引后,您将能够搜索“50 Ce*”等内容以获得部分单词匹配。如果您希望允许部分单词匹配,而不要求您的用户知道如何添加 * 本身,您可能必须在将搜索字符串传递给 sphinx 之前以编程方式修改它。
You need to use the min_prefix_len index config option to tell sphinx that you want it to index and match on partial words. You'll probably also need to set enable_star to 1
http://www.sphinxsearch.com/docs/current.html#conf-min-prefix-len
after enabling prefix indexing you'll be able to search for stuff like "50 Ce*" to get partial word matches. If you want partial word matches to be allowed without requiring your users to know about adding the * themselves you'll probably have to modify the search string programmatically before passing it to sphinx.