Using Sphinx with PHP with multiple indexes
full-text-search, php, search, sphinx
Solution
Each index sets an 'constant' attribute....
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
Then
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
}
}
Problem
I've asked a few similar questions but probably didn't provide all the info required. I have setup the latest version of Sphinx - and have created the indexes (although I'm not 100% i've optimised this configuration) (see the pastebin link at the bottom for this config) Below is the script that connects to the Sphinx API with the string 'teaching' that i'd like to perform a search on - currently I have four indexes (see the config here - http://pastebin.com/DGgheqYm) i'd need to be able to determine which index that each results come from as this will determine which table that i'll need to perform the query on eg.. ``` // {$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)" ``` With the array of IDs it returns can anyone suggest the best way to :- Determine which index a result is coming from to determine the query And..if I am doing this the 'right way' - bit of a noob with 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 UPDATE : Still having problems here is the output from Sphinx API - I cannot understand why my attributes that i've added using the help from Barry's post doesn't seem to appear http://pastebin.com/jzBexCBq