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

  • Home
  • SEARCH
  • 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 7944901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:41:28+00:00 2026-06-04T00:41:28+00:00

I am converting a Rails app from using acts_as_solr to sunspot. The app uses

  • 0

I am converting a Rails app from using acts_as_solr to sunspot.

The app uses the field search capability in solr that was exposed in acts_as_solr. You could give it a query string like this:

title:"The thing to search"

and it would search for that string in the title field.

In converting to sunspot I am parsing out field specific portions of the query string and I need to dynamically generate the search block. Something like this:

Sunspot.search(table_clazz) do
  keywords(first_string, :fields => :title)
  keywords(second_string, :fields => :description)

  ...
  paginate(:page => page, :per_page => per_page)      
end

This is complicated by also needing to do duration (seconds, integer) ranges and negation if the query requires it.

On the current system users can search for something in the title, excluding records with something else in another field and scoping by duration.

In a nutshell, how do I generate these blocks dynamically?

  • 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-06-04T00:41:29+00:00Added an answer on June 4, 2026 at 12:41 am

    I have solved this myself. The solution I used was to compiled the required scopes as strings, concatenate them, and then eval them inside the search block.

    This required a separate query builder library that interrogates the solr indexes to ensure that a scope is not created for a non existent index field.

    The code is very specific to my project, and too long to post in full, but this is what I do:

    1. Split the search terms

    this gives me an array of the terms or terms plus fields:

    ['field:term', 'non field terms']

    2. This is passed to the query builder.

    The builder converts the array to scopes, based on what indexes are available. This method is an example that takes the model class, field and value and returns the scope if the field is indexed.

    def convert_text_query_to_search_scope(model_clazz, field, value)
      if field_is_indexed?(model_clazz, field)
        escaped_value = value.gsub(/'/, "\\\\'")
        "keywords('#{escaped_value}', :fields => [:#{field}])"
      else
        ""
      end
    end
    

    3. Join all the scopes

    The generated scopes are joined join("\n") and that is evaled.

    This approach allows the user to selected the models they want to search, and optionally to do field specific searching. The system will then only search the models with any specified fields (or common fields), ignoring the rest.

    The method to check if the field is indexed is:

    # based on http://blog.locomotivellc.com/post/6321969631/sunspot-introspection
    def field_is_indexed?(model_clazz, field)
      # first part returns an array of all indexed fields - text and other types - plus ':class'
      Sunspot::Setup.for(model_clazz).all_field_factories.map(&:name).include?(field.to_sym)
    end
    

    And if anyone needs it, a check for sortability:

    def field_is_sortable?(classes_to_check, field)
      if field.present?
        classes_to_check.each do |table_clazz|
          return false if ! Sunspot::Setup.for(table_clazz).field_factories.map(&:name).include?(field.to_sym)
        end
        return true
      end
      false
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Converting another app from Rails 2 to Rails 3 - in one of my
I'm working on converting and old Rails app from Rails 2.3.14 to Rails 3.0.
I am converting a script from PHP to ASP.net C#. In PHP, i could
I'm working on a dashboard app using Rails 3.1.1/Highcharts with several graphs, but am
I'm in the process of converting my Rails app to use mongodb through mongoid.
So I'm connecting to SQL Server 2005 from my Rails app via the activerecord-sqlserver-adapter.
I'm currently converting a fairly large Rails 2 app to Rails 3. The app
I'm having trouble connecting to the database for my Rails app using pgAdmin. I
I'm converting a PHP app written in CodeIgniter to Rails. Due to the size
I had to create a sqlite3 database in my rails 3.1.1 app manually, converting

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.