Reputation: 1753
I want to implement greater than and less than condition in sphinx search
I have one table contain job
id | job_title | min_experience | max_experience
101 | php Developer | 4 | 10
102 | PHP Developer | 6 | 9
103 | PHP Developer | 4 | 5
If user search with 7 year to 8 year experience jobs then 2 (101 and 102) records will display because in these records range are between them. How can I implement this logic on sphinx search. Please if you have an idea share.
There is a setFilterRange range but it will work on one field.
If I will implement this
$sphinx->setFilterRange("min_experience", 7 , 8);
// no result because both are less than 7
$sphinx->setFilterRange("min_experience", 7 , 8);
// no result will display because max experience is 10 or 9
But both records are valid.
Upvotes: 2
Views: 731
Reputation: 155
I had a similar case but with prices. This works for me.
if ($min_price - 1 > 0) {
$sph->setFilterRange('price_min', 0, $min_price - 1, true);
}
$sph->setFilterRange('price_max', $max_price + 1, 10 ** 10, true);
Upvotes: 0
Reputation: 21091
Actully think was wrong, this CAN be implemented with normal positive filters
$sphinx->setFilterRange("min_experience", 0 , 7); //7 is lower user experience
$sphinx->setFilterRange("max_experience", 8 , 900000); // 8 is the upper upper
This says the matching job, needs between 0 and 7 minimum. The user has 7, so would match any job needing 7 or less. If the user had 5, it would match 101 and 103 as they only need 4, but not 102 as that needs 6.
And the max filter says, 8 or more. The user has 8, so matches 101 and 102, which is ok, as 8 is less than 9/10. Again 103 does not match, as there user has TOO much experience.
(same logic as the previous answer, but avoiding the negative filters so easier to understand!)
Upvotes: 0
Reputation: 21091
If user search with 7 year to 8 year experience
Its confusing to work out the logic, but think this will work...
$sphinx->setFilterRange("max_experience", 0 , 6, true);
$sphinx->setFilterRange("min_experience", 8 , 900000, true);
Saying you 7-8 experience, is saying you DONT want with 0-6 years upper bound. Ie exclude jobs with not enough experience. ie the first rule excludes #103
And you dont want with too little, so the second rule would exclude a job say 10->15 years, because the users experience is too low.
Note: It would logically be possible to implement the rules with 'OR', but as can't with sphinx (it only has 'AND'), have to invert the logic, which means using some NOT rules (the last true param)!
Upvotes: 0