3

我问了一些类似的问题,但可能没有提供所需的所有信息。

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

下面是连接到 Sphinx API 的脚本,它带有我想执行搜索的字符串“教学” - 目前我有四个索引(请参阅此处的配置 - http://pastebin.com/DGgheqYm)我' d 需要能够确定每个结果来自哪个索引,因为这将确定我需要在哪个表上执行查询

例如..

// {$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

4

1 回答 1

5

每个索引设置一个“常量”属性....

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
    }
}
于 2012-10-24T11:21:40.190 回答