0

I am trying to search several MySQL tables with different fields using Sphinx and combine all of the results into a single set based on relevance.

I have configured Sphinx with an index for each table and am successfully combining the results by searching all of the indexes at once.

When I query using SEARCH through the shell I get all of the result information back as expected. However, when I use the PHP API the result comes back only with the IDs of the rows and therefore no way to tell which table it came from.

Is there a way to get the PHP API to tell me which table/index it came from so that I can go in and query for the actual data?

The alternative I am considering is attempting to process the output from the shell script but that seems messy.

Here is the PHP: $search = $_GET['query']; // Connection Information $sphinxClient = new SphinxClient(); $sphinxClient->SetServer( 'localhost', 9312 ); $sphinxClient->SetMaxQueryTime(5000);

//Sphinx Result Configuration
$sphinxClient->SetMatchMode(SPH_MATCH_ANY);
$sphinxClient->SetRankingMode( SPH_RANK_PROXIMITY_BM25 );
$sphinxClient->SetLimits(0, 20);

// Give me back the results as an array
$sphinxClient->SetArrayResult(true);

$searchResults = $sphinxClient->Query( $search, 'user model' );

The shell script is simply:

./search SEARCHTERM

where SEARCHTERM is the search

which outputs something that looks like:

Sphinx 2.0.3-release (r3043) Copyright (c) 2001-2011, Andrew Aksyonoff Copyright (c) 2008-2011, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/usr/local/sphinx/etc/sphinx.conf'... index 'user': query 'NEWTON ': returned 10 matches of 10 total in 0.000 sec

displaying matches: 1. document=1, weight=2629, time=Thu Jan 1 00:33:32 1970 id=1 first_name=Joe last_name=Shmo company=Acme

The PHP API Output in JSON format: { "error":"", "warning":"", "status":"good", "fields": ["name","code_name","code","description","rating","angles","published","key_words","referenced_num","approved","used_num","avg_runtime","examples","editor","published_time"], "attrs":{"time":2}, "matches": [ {"id":1,"weight":"1","attrs":{"time":2012}} ], "total":"1", "total_found":"1", "time":"0.000", "words":{"posuere":{"docs":"1","hits":"2"}} }

2
  • 1
    Please add the code where you perform the PHP API search so it's more clear how you do that. Next to that, please document the shell command-line you do, so it can be compared with each other.
    – hakre
    Apr 5, 2012 at 8:10
  • ./search shell script wasn't developed for production use. It is tool to debug and monitor status and content of the indexes. Your should use Sphinx API instead. Apr 5, 2012 at 11:22

3 Answers 3

3

I am pretty sure that when searching though the API you don't only get the Ids of the documents matching but also all other int values of the document found.

So you could try adding in your source something like

SELECT id, "1" as type FROM table1

sql_attr_uint = type

and type field now tells you which table the id is from

Please note however that there are a few issues with searching multiple indexes of different tables at once.

  1. You need to make sure that the id does not appear more that once in your result set (Usually the suggested solution would be to pad the id by 1000000 or by some similar way - which personally I find awful)

  2. The results contain only the columns from the first index being searched. You need to make sure that all of your sources return the same columns.

Personally every time I thought of searching multiple indexes at once, I ended up searching each one separately and presenting the results as such.

Update: Added the sql_attr_uint needed

2
  • Thanks for the reply, its very helpful. How would one go about padding the id values to ensure uniqueness? In the SQL query itself? Also,could you explain the second part a little more? The output that I am getting from the API which I will post above does not return the values from any of the columns so I assumed this would not be an issue. Apr 5, 2012 at 8:37
  • @BrianPeacock, do post your Sources definition as well and I will answer you
    – mobius
    Apr 5, 2012 at 8:55
1

It's normal that Sphinx returns you object (row) ids. The problem is in your model. If you can't determine by id which object it is, your model is wrong. The possible options are:

  • create a separate sphinx index per a object type (table or a group of linked tables)
  • improve your objects numeration to make possible objects identification, by some prefixing for example.
0

if the tables have the same structure you can use union with your sql queries

SELECT * FROM table1 WHERE id IN (ids,from,sphinx)
UNION
SELECT * FROM table2 WHERE id IN (ids,from,sphinx)
...
UNION
SELECT * FROM tableN WHERE id IN (ids,from,sphinx)

beware that this is cpu intensive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.