sunspotsunspot-rails

How do I dynamically build a search block in sunspot?


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?


Solution

  • 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