Module ActsAsSolr::ClassMethods
In: lib/class_methods.rb

Methods

Included Modules

CommonMethods

Public Instance methods

returns the total number of documents found in the query specified:

 Book.count_by_solr 'rails' => 3

[Source]

# File lib/class_methods.rb, line 128
    def count_by_solr(query)        
      data = process_query(query)
      data ? data['response']['numFound'] : 0
    end

Finds instances of a model. Terms are ANDed by default, can be overwritten by using OR between terms

Here‘s a sample (untested) code for your controller:

 def search
   results = Book.find_by_solr params[:query]
 end

You can also search for specific fields by searching for ‘field:value‘

options:

start:- The first document to be retrieved (offset)
rows:- The number of rows per page
field_types:Use this option if you had set a field type on your acts_as_solr call:
  class Electronic < ActiveRecord::Base
    acts_as_solr :fields => [{:price => :range_float}]
    def current_time
      Time.now
    end
  end

Then on your search you would do:

  Electronic.find_by_solr "ipod AND price:[* TO 59.99]",
                          :field_types => [{:price => :range_float}]

[Source]

# File lib/class_methods.rb, line 37
    def find_by_solr(query, options={})
      data = process_query(query, options)
      if data
        docs = data['response']['docs']
        return [] if docs.size == 0
        ids = docs.collect {|doc| doc["#{solr_configuration[:primary_key_field]}"]}
        conditions = [ "#{self.table_name}.#{primary_key} in (?)", ids ]
        result = self.find(:all, :conditions => conditions)
        reorder(result, ids)
      end            
    end

Finds instances of a model and returns an array with the ids:

 Book.find_id_by_solr "rails" => [1,4,7]

The options accepted are the same as find_by_solr

[Source]

# File lib/class_methods.rb, line 84
    def find_id_by_solr(query, options={})
      data = process_query(query, options)
      if data
        docs = data['response']['docs']
        ids = docs.collect {|doc| doc["#{solr_configuration[:primary_key_field]}"]}
        return docs.size == 0 ? [] : ids
      end            
    end

Finds instances of a model and returns a hash with the documents and the faceted search results:

 def search
   records = Electronic.find_with_facet "memory", :facets => {:fields =>[:category]}
 end

options:

Accepts the same options as find_by_solr plus:

facets:This option argument accepts two other arguments:
fields:The fields to be included in the faceted search (Solr‘s facet.field)
query:The queries to be included in the faceted search (Solr‘s facet.query)

Example:

  Electronic.find_with_facet "memory", :facets => {:query => ["price:[* TO 200]",
                                                              "price:[200 TO 500]",
                                                              "price:[500 TO *]"],
                                                   :fields => [:category, :manufacturer]}

[Source]

# File lib/class_methods.rb, line 68
    def find_with_facet(query, options={})
      data = process_query(query, options)
      if data
        docs = data['response']['docs']
        return { :docs => [], :facets => { 'facet_fields' => []}} if docs.size == 0
        ids = docs.collect {|doc| doc["#{solr_configuration[:primary_key_field]}"]}
        conditions = [ "#{self.table_name}.#{primary_key} in (?)", ids ]
        result = self.find(:all, :conditions => conditions)
        {:docs => reorder(result, ids), :facets => data['facet_counts'], :total => data['response']['numFound'].to_i}
      end            
    end

This method can be used to execute a search across multiple models:

  Book.multi_solr_search "Napoleon OR Tom", :models => [Movie]

options:

Accepts the same options as find_by_solr plus:

models:The additional models you‘d like to include in the search
results_format:Specify the format of the results found
:objects :Will return an array with the results being objects (default). Example:
 Book.multi_solr_search "Napoleon OR Tom", :models => [Movie], :results_format => :objects
:ids :Will return an array with the ids of each entry found. Example:
 Book.multi_solr_search "Napoleon OR Tom", :models => [Movie], :results_format => :ids
 => [{"id" => "Movie:1"},{"id" => Book:1}]

Where the value of each array is as Model:instance_id

[Source]

# File lib/class_methods.rb, line 107
    def multi_solr_search(query, options = {})
      models = "AND (#{solr_configuration[:type_field]}:#{self.name}"
      options[:models].each{|m| models << " OR type_t:"+m.to_s} if options[:models].is_a?(Array)
      options.update(:results_format => :objects) unless options[:results_format]
      data = process_query(query, options, models<<")")
      result = []
      if data
        docs = data['response']['docs']
        return [] if docs.size == 0
        if options[:results_format] == :objects
          docs.each{|doc| k = doc.fetch('id').split(':'); result << k[0].constantize.find_by_id(k[1])}
          result
        elsif options[:results_format] == :ids
          docs
        end
      end
    end

It‘s used to rebuild the Solr index for a specific model.

 Book.rebuild_solr_index

[Source]

# File lib/class_methods.rb, line 135
    def rebuild_solr_index
      self.find(:all).each {|content| content.solr_save}
      logger.debug self.count>0 ? "Index for #{self.name} has been rebuilt" : "Nothing to index for #{self.name}"
    end

[Source]

# File lib/class_methods.rb, line 140
    def solr_commit
      ActsAsSolr::Post.execute('<commit/>', :update)
    end

[Validate]