0

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.

yekta
  • 3,363
  • 3
  • 35
  • 50

2 Answers2

2

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

yekta
  • 3,363
  • 3
  • 35
  • 50
1

I guess you have to use your query like

By default the sort mode is asc for changing that you need to specify

Project.search <query>, :order => <attribute_name>, :sort_mode => :desc, :match_mode =>:extended

If you want to use multiple attributes, or Sphinx’s ranking scores, then you’ll need to use the :extended sort mode.

Project.search <query>, :sort_mode => :extended, :order => "attribute_name DESC/ASC, @relevance DESC/ASC", :match_mode=> :extended

http://pat.github.com/ts/en/searching.html#sorting

mohit
  • 1,913
  • 3
  • 16
  • 23
  • Thanks for the answer, I mentioned in the [EDIT] that I've considered using Sphinx extended and expression sorting. How does this apply to my scenario? I mean, I don't believe you can specify `..,:order => "schedules.attribute_name ASC/DESC"` – yekta Feb 08 '13 at 15:05
  • @yekta Refer to the documentation link i have provided. you are creating your attribute as due_date. so you should be able to specfy ... `:order=> :due_date, :sort_mode=>:desc,:match_mode=>:extended` – mohit Feb 08 '13 at 19:34
  • I did refer to the documentation, but again that does not address my scenario. (I pointed out match mode extended and sort mode in my Edit too) How do you get *Schedules* only matching a certain :name to sort with that? Remember the search is from `Project.search...` That's the scenario of my problem. – yekta Feb 13 '13 at 17:44