I have a scenario where Projects have Schedules, Schedules have many various names and due dates.
Given I can search Projects and sort on Project field values and even polymorphic field values how can I sort the results based off of Schedules with a requested Schedule name.
In plain english, in Thinking-Sphinx, how can I order my search results by due date of a matching schedule name, eg. I wish to sort projects by the "Booking" (Schedule) due date.
This seems like it should be simple in Sphinx but its evading me. I've contemplated building a post-search sort which accounts for the Schedule name, but a Sphinx sort would be much preferred.
I've tried something like the following, but it doesn't account for the the Schedule name.
class Project
has_many :schedules
define_index do
has schedules.due_date, :type => :datetime, :as => :due_date
end
end
$: Project.search(query, :order => "due_date desc", :match_mode => :extended)
Interestingly I do get results with that (somewhat sorted) but its not accurate, probably because it doesn't account for many schedules beloning to one Project, thus not being field value specific. I've also considered using "with" but
Is there a way I can accomplish this with Thinking Sphinx? (I'm on Rails 2.3.x so I'm on thinking-sphinx 1.4.3.)
[EDIT] I've successfully sorted this post-search with the <=> operator but that requires a look up for the Schedules. I've also looked into Expression sorting but that's only for fields of the model being searching on, I don't know what I could index for that to become available.
From Pat Allan: It's not possible in the way I've described. Schedule should be searched on instead.
See Pat's answer on the Google Group discussion. "What you're trying to do is not possible with Sphinx in the way you're thinking: Sphinx does not have any understanding of relationships between different pieces of data within fields or attributes, so there's nothing linking a schedule date and schedule name together within a project index. I'd recommend you search on Schedule instead, and then, because there's only one schedule name and one schedule due date, you should be fine. From there, you can then use that schedule's project in the search results."