Class: DNZ::Facet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dnz/facet.rb

Overview

A facet retrieved from DigitalNZ search results. The facet has a name and an array of FacetValue objects.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

The facet name



8
9
10
# File 'lib/dnz/facet.rb', line 8

def name
  @name
end

#searchObject (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)      
  options = {:facets => facet, :facet_num_results => num_results, :num_results => 0}
  Client.search(search_text, options).facets[facet]
end

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.find_related(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

#eachObject

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

#inspectObject



74
75
76
# File 'lib/dnz/facet.rb', line 74

def inspect
  '[ %s ]' % values.collect(&:inspect).join(', ')
end

#to_sObject



70
71
72
# File 'lib/dnz/facet.rb', line 70

def to_s
  values.join(', ')
end

#valuesObject

An array of FacetValue objects



54
55
56
# File 'lib/dnz/facet.rb', line 54

def values
  @values
end