Sha256: 78d095fb6206f68b2791fccc6cf70a033e8ebfdbeda91c70c986c1c9bdffdd37

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require "rsolr"

class Supernova::SolrCriteria < Supernova::Criteria
  def to_params
    solr_options = { :fq => [], :q => "*:*" }
    solr_options[:fq] += self.filters[:with].map { |key, value| "#{key}:#{value}" } if self.filters[:with]
    if self.filters[:without]
     self.filters[:without].each do |key, values| 
       solr_options[:fq] += values.map { |value| "!#{key}:#{value}" }
     end
    end
    solr_options[:sort] = self.search_options[:order] if self.search_options[:order]
    solr_options[:q] = self.filters[:search] if self.filters[:search]
    
    if self.search_options[:geo_center] && self.search_options[:geo_distance]
      solr_options[:pt] = "#{self.search_options[:geo_center][:lat]},#{self.search_options[:geo_center][:lng]}"
      solr_options[:d] = self.search_options[:geo_distance].to_f / Supernova::KM_TO_METER
      solr_options[:sfield] = :location
      solr_options[:fq] << "{!geofilt}"
    end
    solr_options[:fq] << "type:#{self.clazz}" if self.clazz
    
    if self.search_options[:pagination]
      solr_options[:rows] = per_page
      solr_options[:start] = (current_page - 1) * solr_options[:rows]
    end
    solr_options
  end
  
  def build_docs(docs)
    docs.map do |hash|
      build_doc(hash)
    end
  end
  
  def build_doc(hash)
    doc = hash["type"].constantize.new
    hash.each do |key, value|
      if key == "id"
        doc.id = value.to_s.split("/").last if doc.respond_to?(:id=)
      else
        doc.send(:"#{key}=", value) if doc.respond_to?(:"#{key}=")
      end
    end
    doc.instance_variable_set("@readonly", true)
    doc.instance_variable_set("@new_record", false)
    doc
  end
  
  def to_a
    response = Supernova::Solr.connection.get("select", :params => to_params)
    collection = Supernova::Collection.new(current_page, per_page, response["response"]["numFound"])
    collection.replace(build_docs(response["response"]["docs"]))
    collection
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
supernova-0.2.1 lib/supernova/solr_criteria.rb