Sphinx Search Multiple Tables and Aggregate Results Using PHP API

梦想的初衷 提交于 2019-12-11 19:52:49

问题


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"}} }


回答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.



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/10024841/sphinx-search-multiple-tables-and-aggregate-results-using-php-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!