Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The solution, just like <a href="https://stackoverflow.com/users/293686/mauricio-linhares">Maurício Linhares</a> wrote in his comment, is to go through the actors model and group by movies.</p> <p>The problem is that Sunspot doesn't support Solr 3.3 or 4.0, which is the only Solr versions that support grouping.</p> <p>Here is my solution using Sunspot 1.2.1 and Solr 3.3. </p> <p><em>In my example <code>movie_id</code> is placed in the actors table, this isn't done in my real application.</em></p> <pre><code># == Schema Information # # Table name: actors # # id :integer(4) not null, primary key # name :string(255) # created_at :datetime # updated_at :datetime # movie_id :integer(4) # class Actor &lt; ActiveRecord::Base searchable do # We need to store the movie_id as an string # So it can be sorted. We also need to pass the # stored: true params string :movie_id, stored: true do movie_id.to_s end end def search_using_solr scoped = Sunspot.new_search(Actor) scoped.build do adjust_solr_params do |params| params[:group] = true params[:"group.field"] = "movie_id_s" params[:"group.format"] = "simple" end end # Sunspot 1.2.1 doesn't support grouping, so we need to do some hacking. def scoped.hits @hits ||= @solr_result["grouped"].values.first["doclist"]["docs"].map do |doc| Sunspot::Search::Hit.new(doc, nil, self) end end def scoped.total @total ||= @solr_result["grouped"]["movie_id_s"]["matches"] || 0 end # Here we'll only fetch the stored data from Solr it self, # and then pass it manualy to ActiveRecord. Movie.where({ id: scoped.execute.hits.map{ |h| h.stored(:movie_id) } }) end end </code></pre> <p>Cred to <a href="https://github.com/alindeman" rel="nofollow noreferrer">alindeman</a> for his <a href="https://gist.github.com/f987013b2feec5b28456" rel="nofollow noreferrer">example gist</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload