Sha256: 05cdcd6368f89aaac7f4f6b4239ebbae76587565f969ebd707165b7fb23fef2b

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Sunspot
  module Search
    class StatFacet

      def initialize(field, search, options)
        @field, @search, @options = field, search, options
      end

      def field_name
        @field.name
      end

      def rows
        #sort options :count or :stat_field 
        @options[:sort] ||= :count
        @options[:type] ||= "sum"
        @options[:limit] ||= -1
        @sort = false
        @rows ||=
        begin
          if !@search.stat_response['stats_fields'].nil?
            if @options[:facet].present?
              stat = @search.stat_response['stats_fields'][@field.indexed_name]
              data = stat.nil? ? [] : stat['facets'][@options[:facet].indexed_name]
              @sort = true
            else
              data = @search.stat_response['stats_fields'].to_a
            end
          end

          rows = []

          data.collect do |stat, value|
            rows << StatRow.new(stat, value[@options[:type]], value, self)
          end

          if @options[:sort] == :count
            rows.sort! { |lrow, rrow| rrow.value <=> lrow.value }
          else
            rows.sort! { |lrow, rrow| lrow.stat_field <=> rrow.stat_field }
          end if @sort
          return rows.empty? ? [] : rows[0..@options[:limit]]
        rescue Exception => e
          puts "Error: #{e}"
          return []
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sunspot_stats-0.0.6 lib/search/stat_facet.rb