使用带有多个索引的PHP的Sphinx

我问过几个类似的问题,但可能没有提供所需的全部信息.

我已经设置了最新版本的Sphinx – 并创建了索引(虽然我不是100%我已经优化了这个配置)(请参阅底部的pastebin链接以获取此配置)

下面是连接到Sphinx API的脚本,其中包含我想要执行搜索的字符串’teaching’ – 目前我有四个索引(请参阅此处的配置 – http://pastebin.com/DGgheqYm)我需要能够确定哪个每个结果来自的索引,因为这将确定我需要执行查询的表

例如..

// {$table}/{$pk} would vary depending on which index we are using (eg if articles index - then it would use the table called 'articles'
$IDs = implode(",",array_keys($result["matches"]));
$sql = "SELECT * FROM {$table} WHERE {$pk} IN ($IDs) ORDER BY FIELD(`id`,$IDs)"

有了它返回的ID数组,任何人都可以建议最好的方法: –

>确定结果来自哪个索引以确定查询
>并且如果我正在做这个’正确的方式’ – 与Spinx的一个菜鸟:)

<?php
include('api/sphinxapi.php');
$cl = new SphinxClient();
$cl->SetServer( "localhost", 9312 );
$cl->SetMatchMode( SPH_MATCH_ANY  );
$result = $cl->Query( "teaching" );

if ( $result === false ) {
echo "Query failed: " . $cl->GetLastError() . ".\n";
}
else {
if ( $cl->GetLastWarning() ) {
  echo "WARNING: " . $cl->GetLastWarning() . "
";
}

if ( ! empty($result["matches"]) ) {
  foreach ( $result["matches"] as $doc => $docinfo ) {
        echo "$doc\n";
  }

  print_r( $result );
 }
 }

 exit;
 ?>

http://pastebin.com/rDYa3MUj
http://pastebin.com/DGgheqYm

更新:这里仍有问题是Sphinx API的输出 – 我无法理解为什么我使用Barry帖子的帮助添加的属性似乎没有出现
http://pastebin.com/jzBexCBq

解决方法:

每个索引都设置一个’常量’属性….

source articles
...
sql_query = SELECT ID, 1 AS table_id, Title, Description FROM articles
sql_attr_uint = table_id
...

source publications
...
sql_query = SELECT Title_ID, 2 AS table_id, Title, PageTitle, PageContent, Description FROM publications
sql_attr_uint = table_id
...

//etc

然后

foreach ( $result["matches"] as $doc => $docinfo ) {
    switch($docinfo['attrs']['table_id']) {
        case 1: echo "Article:$doc\n"; break;
        case 2: echo "Publication:$doc\n"; break;
        case 3: echo "Library Content:$doc\n"; break;
        //etc
    }
}
上一篇:php – Sphinx搜索Reindex


下一篇:php – Sphinx_Query失败:没有启用本地索引进行搜索