Class: Solr::Facets

Inherits:
Object show all
Defined in:
lib/solr/facets.rb

Overview

The list of all facets returned by Solr

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Facets) initialize(facets, facet_queries)

Initialize from the two facet parameters from RSolr::Ext



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/solr/facets.rb', line 48

def initialize(facets, facet_queries)
  @all = []

  # Step through the facets
  if facets
    facets.each do |f|
      f.items.each do |it|
        next if Integer(it.hits) == 0
        @all << Facet.new(name: f.name, value: it.value, hits: it.hits)
      end
    end
  end

  # Step through the facet queries
  if facet_queries
    facet_queries.each do |k, v|
      next if Integer(v) == 0
      @all << Facet.new(query: k, hits: v)
    end
  end
end

Instance Attribute Details

- (Array<Solr::Facet>) all (readonly)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/solr/facets.rb', line 9

class Facets
  attr_reader :all

  # Get all facets for a given field
  #
  # @param [Symbol] field the field to retrieve facets for
  # @return [Array<Solr::Facet>] all facets for this field
  def for_field(field)
    @all.select { |f| f.field == field.to_sym }
  end

  # Get all facets for a given field, sorted
  #
  # @param [Symbol] field the field to retrieve sorted facets for
  # @return [Array<Solr::Facet>] sorted facets for this field
  def sorted_for_field(field)
    for_field(field).sort
  end

  # Find a facet by its query parameter
  #
  # @param [String] query the query to search for
  # @return [Solr::Facet] the facet for this query
  def for_query(query)
    all.find { |f| f.query == query }
  end

  # Return true if there are no facets
  #
  # @return [Boolean] true if +all.empty?+
  def empty?
    return true unless @all
    @all.empty?
  end

  # Initialize from the two facet parameters from RSolr::Ext
  #
  # @param [Array<RSolr::Ext::Facet>] facets the facet parameters
  # @param [Hash] facet_queries the facet queries
  def initialize(facets, facet_queries)
    @all = []

    # Step through the facets
    if facets
      facets.each do |f|
        f.items.each do |it|
          next if Integer(it.hits) == 0
          @all << Facet.new(name: f.name, value: it.value, hits: it.hits)
        end
      end
    end

    # Step through the facet queries
    if facet_queries
      facet_queries.each do |k, v|
        next if Integer(v) == 0
        @all << Facet.new(query: k, hits: v)
      end
    end
  end

end

Instance Method Details

- (Boolean) empty?

Return true if there are no facets



39
40
41
42
# File 'lib/solr/facets.rb', line 39

def empty?
  return true unless @all
  @all.empty?
end

- (Array<Solr::Facet>) for_field(field)

Get all facets for a given field



16
17
18
# File 'lib/solr/facets.rb', line 16

def for_field(field)
  @all.select { |f| f.field == field.to_sym }
end

- (Solr::Facet) for_query(query)

Find a facet by its query parameter



32
33
34
# File 'lib/solr/facets.rb', line 32

def for_query(query)
  all.find { |f| f.query == query }
end

- (Array<Solr::Facet>) sorted_for_field(field)

Get all facets for a given field, sorted



24
25
26
# File 'lib/solr/facets.rb', line 24

def sorted_for_field(field)
  for_field(field).sort
end