Sphinx PHP 搜索

发布于 2024-08-25 07:47:28 字数 1678 浏览 7 评论 0原文

我正在进行狮身人面像搜索,但发现了一些非常奇怪的结果。任何帮助表示赞赏。

例如,如果我输入“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 技术交流群。

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

发布评论

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

评论(1

拥抱影子 2024-09-01 07:47:28

您需要使用 min_prefix_len 索引配置选项来告诉 sphinx 您希望它对部分单词进行索引和匹配。您可能还需要将enable_star设置为1

http:// www.sphinxsearch.com/docs/current.html#conf-min-prefix-len

index artists
{
 source     = artists
 path     = /var/sphinx/artists
 docinfo     = extern
 charset_type   = utf-8
 min_prefix_len   = 2
 enable_star   = 1
}

启用前缀索引后,您将能够搜索“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

index artists
{
 source     = artists
 path     = /var/sphinx/artists
 docinfo     = extern
 charset_type   = utf-8
 min_prefix_len   = 2
 enable_star   = 1
}

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.

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