Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6472819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:23:30+00:00 2026-05-25T06:23:30+00:00

I’m having some problem searching by group when using Sunspot. Here is an example:

  • 0

I’m having some problem searching by group when using Sunspot.

Here is an example:

# == Schema Information
#
# Table name: movies
#
#  id              :integer(4)      not null, primary key
#  title           :string(255)

class Movie < ActiveRecord::Base
  has_and_belongs_to_many :actors

  searchable do
    text :title

    integer :ages, multiple: true do
      actors.map(&:age)
    end

    text :names, multiple: true do
      actors.map(&:name)
    end
  end
end

# == Schema Information
#
# Table name: actors
#
#  id              :integer(4)      not null, primary key
#  name            :string(255)
#  age             :integer(30)

class Actor < ActiveRecord::Base
  has_and_belongs_to_many :movies

  searchable do
    integer :age
    text :name
  end
end

I want to find every movie that has an actor named John at age 30.

Movie.search do
  with(:names).equal_to("John")
  with(:ages).equal_to(30)
  with(:title).equal_to("...")
  # ...
end

The problem is here that it may find a movie that has two actors; one named John and one at age 30. Is there a way to somehow group this together so that the movie found have an actor named John at age 30?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:23:30+00:00Added an answer on May 25, 2026 at 6:23 am

    The solution, just like Maurício Linhares wrote in his comment, is to go through the actors model and group by movies.

    The problem is that Sunspot doesn’t support Solr 3.3 or 4.0, which is the only Solr versions that support grouping.

    Here is my solution using Sunspot 1.2.1 and Solr 3.3.

    In my example movie_id is placed in the actors table, this isn’t done in my real application.

    # == 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 < 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
    

    Cred to alindeman for his example gist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.