0

我在 localhost (wamp) 上安装了 sphinx search。例如,我想在一个简单的 html 表格中显示结果。我试图用 php 连接到 sphinx 搜索,我认为它做到了,但是当我打印结果时它是 0.... 但这不是真的。我不太明白在查询部分我需要输入我想要搜索的词吗?我尝试在命令窗口中搜索,效果很好,只是我不知道如何在网页上显示它。我想使用mysql。所以我的PHP看起来像这样:

require ( "sphinx/api/sphinxapi.php" );
$s = new SphinxClient;
$s->setServer("localhost", 9306);
$s->setMatchMode(SPH_MATCH_ANY);
$s->setMaxQueryTime(3);

$result = $s->query("test");

var_dump($result);

这是我的迷你 conf 文件:

source src1
{
    type            = mysql

    sql_host        = localhost
    sql_user        = root
    sql_pass        =
    sql_db          = test
    sql_port        = 3306  # optional, default is 3306

    sql_query       = \
        SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
        FROM documents

    sql_attr_uint       = group_id
    sql_attr_timestamp  = date_added

    sql_query_info      = SELECT * FROM documents WHERE id=$id
}


index test1
{
    source          = src1
    path            = C:/wamp/www/sphinx/data/test1
    docinfo         = extern
    charset_type        = sbcs
}

indexer
{
    mem_limit       = 32M
}


searchd
{
    listen          = 9312
    listen          = 9306:mysql41
    log         = C:/wamp/www/sphinx/log/searchd.log
    query_log       = C:/wamp/www/sphinx/log/query.log
    read_timeout        = 5
    max_children        = 30
    pid_file        = C:/wamp/www/sphinx/log/searchd.pid
    max_matches     = 1000
    seamless_rotate     = 1
    preopen_indexes     = 1
    unlink_old      = 1
    workers         = threads # for RT to work
    binlog_path     = C:/wamp/www/sphinx/data
}
4

2 回答 2

2

您的连接和查询可能如下所示:

$link = mysql_connect ( '127.0.0.1:9306', '', '' );
$db_selected = mysql_select_db ( 'main', $link );

然后通常是mysql查询(就像你通常做的那样)

$sql = "select * from main WHERE MATCH('$queryterm') order by date desc LIMIT 0,50";
$result = mysql_query ( $sql );

或者您可以根据需要检索一些元信息(有关元数据,请参阅 Sphinx 文档)

$metainfo = mysql_query('SHOW META');
while ( $meta = mysql_fetch_array ( $metainfo ) ) {
        $srchmeta[]=$meta;
//or whatever you want here
}
于 2013-04-05T11:07:28.947 回答
1

$s->setServer("localhost", 9306);

那是 mysql 协议端口 - sphinxQL。

在您的配置文件中,您可以使用 sphinxAPI 协议在 9312 上侦听 sphinx。因此,将您的 setServer 行更改为使用 9312

于 2013-04-05T11:02:40.653 回答