I am using sphinx to return results to an autocomplete list. When returned the results are being cached client side to avoid unnecessary hits to the server.
The logic is as follows:
- Perform client side fuzzymatch algorithm on cached results and display in autocomplete
- if matches.length < 4 perform ajax request and use sphinx to get more results up to a total of 4
- return results and append to autocomplete list
Currently in the ajax request I am sending an array of ID’s that were matched in the client side cache and using PHP to loop through sphinx’s results and remove them from the matches.
This is not only inefficient but of course gives me problems with regards to limiting the results correctly.
What I would like to be able to do is exclude these particular ID’s from the Sphinx matches themselves so that the limit clause is actually relevant.
So the question actually is, how can I exclude certain ID’s within a Sphinx query using the PHP api. e.g. “find all matches with the username John except if they have any of the following ID’s….”
I’ve been right through the sphinx documentation and have read a fair amount on “extended mode” matching and using the various operators however I have found myself getting quite confused and am unsure on how to proceed.
Any help would be greatly appreciated.
Here is the good way to do it with sphinx:
“Currently in the ajax request I am sending an array of ID’s that were matched in the client side cache and using PHP to loop through sphinx’s results and remove them from the matches.”
You need to use setfilter in sphinx
Here $cl is the object of sphinx class
$attribute is attribute name on which you want to apply filter.
$values must be a plain array containing integer values. (here you can pass your array)
exclude this is very important parameter, if you want to exclude all ids from results you provided in array (3rd parameter) then set it as true.
Ex:
$cl->SetFilter ( $attribute, $values, true);
Hope this help!!