Class: Sunspot::Search::AbstractSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/search/stat_search.rb

Overview

This class encapsulates the results of a Solr search. It provides access to search results, total result count, facets, and pagination information. Instances of Search are returned by the Sunspot.search and Sunspot.new_search methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, setup, query, configuration) ⇒ AbstractSearch

:nodoc:



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/search/stat_search.rb', line 20

def initialize(connection, setup, query, configuration) #:nodoc:
  @connection, @setup, @query = connection, setup, query
  @query.paginate(1, configuration.pagination.default_per_page)

  @stats = []
  @stats_by_name = {}

  @facets = []
  @facets_by_name = {}

  @groups_by_name = {}
  @groups = []
end

Instance Attribute Details

#statsObject (readonly)

Retrieve all facet objects defined for this search, in order they were defined. To retrieve an individual facet by name, use #facet()



18
19
20
# File 'lib/search/stat_search.rb', line 18

def stats
  @stats
end

Instance Method Details

#add_field_stat(field, options = {}) ⇒ Object

:nodoc:



56
57
58
59
# File 'lib/search/stat_search.rb', line 56

def add_field_stat(field, options = {}) #:nodoc:
  name = (options[:name] || field.name)
  add_stat(name, StatFacet.new(field, self, options))
end

#add_stat(name, stat) ⇒ Object



61
62
63
64
# File 'lib/search/stat_search.rb', line 61

def add_stat(name, stat)
  @stats << stat
  @stats_by_name[name.to_sym] = stat
end

#stat(name) ⇒ Object



35
36
37
38
39
# File 'lib/search/stat_search.rb', line 35

def stat(name)
  if name
    @stats_by_name[name.to_sym]
  end
end

#stat_responseObject

:nodoc:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/search/stat_search.rb', line 41

def stat_response #:nodoc:
  @solr_result['stats']["stats_fields"].each_pair do |k, value|
    if value && value.key?("facets")
      value["facets"].each_pair do |k1, value1|
        value1.each_pair do |k2, value2|
          if @solr_result['stats']['stats_fields'][k]['facets'][k1][k2]['mean'].to_s == 'NaN'
            @solr_result['stats']['stats_fields'][k]['facets'][k1][k2]['mean'] = 0.0
          end
        end
      end
    end
  end
  @solr_result['stats']||[]
end