Sha256: 3af8893573eb9a23ac0749635e753a45915dece63c5b38c3125b50643d3d0183

Contents?: true

Size: 1.88 KB

Versions: 56

Compression:

Stored size: 1.88 KB

Contents

module Sunspot
  module Search
    class QueryFacet
      RequestedFacet = Struct.new(:label, :boolean_phrase) #:nodoc:

      attr_reader :name

      def initialize(name, search, options) #:nodoc:
        @name, @search, @options = name, search, options
        @requested_facets = []
      end

      def rows(options = {})
        @rows ||=
          begin
            data = @search.facet_response['facet_queries']
            rows = []
            minimum_count =
              case
              when @options[:minimum_count] then @options[:minimum_count]
              when @options[:zeros] then 0
              else 1
              end
            @requested_facets.each do |requested_facet|
              count = data[requested_facet.boolean_phrase] || 0
              if count >= minimum_count
                rows << FacetRow.new(requested_facet.label, count, self)
              end
            end
            sort_rows!(rows)
          end
      end

      def add_row(label, boolean_phrase) #:nodoc:
        @requested_facets << RequestedFacet.new(label, boolean_phrase)
      end

      private

      def sort_rows!(rows)
        case @options[:sort] || (:count if limit)
        when :count
          rows.sort! { |lrow, rrow| rrow.count <=> lrow.count }
        when :index
          rows.sort! do |lrow, rrow|
            if lrow.respond_to?(:<=>)
              lrow.value <=> rrow.value
            elsif lrow.respond_to?(:first) && rrow.respond_to?(:first) && lrow.first.respond_to?(:<=>)
              lrow.first.value <=> rrow.first.value
            else
              lrow.value.to_s <=> rrow.value.to_s
            end
          end
        end
        if limit
          rows.replace(rows.first(limit))
        end
        rows
      end

      def limit
        return @limit if defined?(@limit)
        @limit = (@options[:limit].to_i if @options[:limit].to_i > 0)
      end
    end
  end
end

Version data entries

56 entries across 56 versions & 10 rubygems

Version Path
sunspot-2.7.1 lib/sunspot/search/query_facet.rb
sunspot-2.7.0 lib/sunspot/search/query_facet.rb
sunspot-2.6.0 lib/sunspot/search/query_facet.rb
sunspot-2.5.0 lib/sunspot/search/query_facet.rb
sunspot-2.4.0 lib/sunspot/search/query_facet.rb
sunspot-2.3.0 lib/sunspot/search/query_facet.rb
sunspot-2.2.8 lib/sunspot/search/query_facet.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/sunspot-2.2.7/lib/sunspot/search/query_facet.rb
sunspot-2.2.7 lib/sunspot/search/query_facet.rb
sunspot-2.2.6 lib/sunspot/search/query_facet.rb
sunspot-2.2.5 lib/sunspot/search/query_facet.rb
sunspot-2.2.4 lib/sunspot/search/query_facet.rb
sunspot-2.2.3 lib/sunspot/search/query_facet.rb
sunspot-2.2.2 lib/sunspot/search/query_facet.rb
sunspot-2.2.1 lib/sunspot/search/query_facet.rb
sunspot-2.2.0 lib/sunspot/search/query_facet.rb
sunspot-2.1.1 lib/sunspot/search/query_facet.rb
sunspot-2.1.0 lib/sunspot/search/query_facet.rb
sunspot-2.0.0 lib/sunspot/search/query_facet.rb
sunspot-2.0.0.pre.130115 lib/sunspot/search/query_facet.rb