Sha256: 4ae0bd3dbe72c328b3853c7a0be8681a95f44901550b5c31488e497424ed5cc8

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 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
        @options[:type] ||= "sum"
        @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]], self)
          end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sunspot_stats-0.0.5 lib/search/stat_facet.rb
sunspot_stats-0.0.4 lib/search/stat_facet.rb
sunspot_stats-0.0.3 lib/search/stat_facet.rb
sunspot_stats-0.0.2 lib/search/stat_facet.rb