I faced trouble trying to narrow sphinx search result with custom sql condition. After 2 days of tries and frustration i even dont care how to do it: before search (through custom indexes conditions), inside search or after search.
I have two classes: Theme and Grade, and join table between them.
class Theme < ActiveRecord::Base
has_many :grades, through: :theme_grades
end
class Grade < ActiveRecord::Base
has_many :themes, through: :theme_grades
end
I want to filter sphinx search of themes with having special grade_id or not having grade_ids at all.
In ActiveRecord it should be like:
Theme.joins(:theme_grades).
where('theme_grades.grade_id = ? OR theme_grades.grade_id IS NULL', %ID%)
In sql it is going to be like:
SELECT "themes".* FROM "themes"
INNER JOIN "theme_grades" ON "theme_grades"."theme_id" = "themes"."id"
WHERE (theme_grades.grade_id = %ID% OR theme_grades.grade_id IS NULL)
But i dont understand how to use it with search results.
I tried:
sql: { join: '...' } in search options hash, rewriting my 'WHERE' as join condition. It doesnt work, resulting in error like "Sphinx found IDs, but ActiveRecord cannot find them", because AR look for them in our narrowed search and expects to find them.
After search simply rip out ids of found objects (because after search we receive array, not relation), and find them through AR, what gives us relation. And we can work with relation - just add another scopes. The problem lies in excerpts - i need them, but i dont understand how to handle them to my relation.
The last idea that i have is to make 6 different indices for every one of my grade (i have 6 different grades). And somehow divide themes between them, but remembering that themes without grades should be in every index.
has theme.grade_ids, as: :grade_idswith one grade_id, through'with' => {}in search. - Anton Kovalev