Class: DNZ::Facet
Overview
A facet retrieved from DigitalNZ search results. The facet has a name and an array of FacetValue objects.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The facet name.
-
#search ⇒ Object
readonly
The search that orginally ran to produce this facet.
Class Method Summary collapse
-
.find(facet, search_text = '*:*', num_results = -1)) ⇒ Object
Find a facet and return it.
-
.find_related(facet, parent_facet, value, search_text = '*:*', num_results = -1)) ⇒ Object
Find related facet values.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Retrieve a FacetValue by name.
-
#each ⇒ Object
Enumerate the FacetValue objects.
-
#initialize(client, search, doc) ⇒ Facet
constructor
A new instance of Facet.
- #inspect ⇒ Object
- #to_s ⇒ Object
-
#values ⇒ Object
An array of FacetValue objects.
Constructor Details
#initialize(client, search, doc) ⇒ Facet
Returns a new instance of Facet.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dnz/facet.rb', line 42 def initialize(client, search, doc) @name = doc.xpath('facet-field').text @values = [] @search = search doc.xpath('values').first.children.each do |value_doc| value = DNZ::FacetValue.new(client, self, value_doc) @values << value if value.valid? end end |
Instance Attribute Details
#name ⇒ Object (readonly)
The facet name
8 9 10 |
# File 'lib/dnz/facet.rb', line 8 def name @name end |
#search ⇒ Object (readonly)
The search that orginally ran to produce this facet
11 12 13 |
# File 'lib/dnz/facet.rb', line 11 def search @search end |
Class Method Details
.find(facet, search_text = '*:*', num_results = -1)) ⇒ Object
Find a facet and return it. This is a convienence method for running Client.search(‘search’, :facets => facet).facets.
-
facet
- The name of the facet. -
search_text
- Optional search text. Defaults to everything (:). -
num_results
- Optional maximum number of facet values to return in the
facet. This defaults to -1 which means no limit.
22 23 24 25 |
# File 'lib/dnz/facet.rb', line 22 def self.find(facet, search_text = '*:*', num_results = -1) = {:facets => facet, :facet_num_results => num_results, :num_results => 0} Client.search(search_text, ).facets[facet] end |
.find_related(facet, parent_facet, value, search_text = '*:*', num_results = -1)) ⇒ Object
Find related facet values. This is a convienence method for running Client.search(‘parent_facet:value’, :facets => facet).facets. It will return the facet and facet values for results scoped to another facet.
-
facet
- The name of the facet. -
parent_facet
- The name of the facet to scope results to. -
value
- The facet value to scope the results to. -
search_text
- Optional search text. Defaults to everything (:). -
num_results
- Optional maximum number of facet values to return in the
facet. This defaults to -1 which means no limit.
37 38 39 40 |
# File 'lib/dnz/facet.rb', line 37 def self.(facet, parent_facet, value, search_text = '*:*', num_results = -1) search_text = [search_text, '%s:"%s"' % [parent_facet, value]].join(' AND ') self.find(facet, search_text, num_results) end |
Instance Method Details
#[](index) ⇒ Object
Retrieve a FacetValue by name
59 60 61 |
# File 'lib/dnz/facet.rb', line 59 def [](index) @values.detect{|value| value.name == index } end |
#each ⇒ Object
Enumerate the FacetValue objects
64 65 66 67 68 |
# File 'lib/dnz/facet.rb', line 64 def each @values.each do |value| yield value end end |
#inspect ⇒ Object
74 75 76 |
# File 'lib/dnz/facet.rb', line 74 def inspect '[ %s ]' % values.collect(&:inspect).join(', ') end |
#to_s ⇒ Object
70 71 72 |
# File 'lib/dnz/facet.rb', line 70 def to_s values.join(', ') end |
#values ⇒ Object
An array of FacetValue objects
54 55 56 |
# File 'lib/dnz/facet.rb', line 54 def values @values end |