Class: Pho::Facet::Results

Inherits:
Object
  • Object
show all
Defined in:
lib/pho/facet.rb

Overview

Captures the data returned in a facetted search

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, fields, facets = Hash.new) ⇒ Results

Returns a new instance of Results.



32
33
34
35
36
# File 'lib/pho/facet.rb', line 32

def initialize(query, fields, facets=Hash.new)
  @query = query
  @fields = fields
  @facets = facets
end

Instance Attribute Details

#facetsObject (readonly)

An array of facets



30
31
32
# File 'lib/pho/facet.rb', line 30

def facets
  @facets
end

#fieldsObject (readonly)

The fields used to generate the results



27
28
29
# File 'lib/pho/facet.rb', line 27

def fields
  @fields
end

#queryObject (readonly)

The query used to generate the facet results, as echoed in the response



24
25
26
# File 'lib/pho/facet.rb', line 24

def query
  @query
end

Class Method Details

.parse(data) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pho/facet.rb', line 58

def Results.parse(data)
  doc = REXML::Document.new(data)
  root = doc.root
  head = root.elements[1]

  query = ""
  fields = ""
  queryEl = head.get_elements("query")[0]
  if queryEl != nil
    query = queryEl.text
  end
  fieldsEl = head.get_elements("fields")[0]
  if fieldsEl != nil
    fields = fieldsEl.text
  end

  results = Results.new(query, fields)
  
  fields = root.get_elements("fields")[0]
  if fields == nil
    raise "No fields in document!"
  end
  
  fields.get_elements("field").each do |field|
    field_name = field.attribute("name").value
    results.facets[field_name] = Array.new
    
    field.get_elements("term").each do |term|
      term = Term.new(term.attribute("number").value.to_i, 
        term.attribute("search-uri").value, 
        term.text() )
        
      results.facets[field_name] << term
    end
    
  end
  
  return results          
end

.read_from_store(store, query, facets, params = Hash.new) ⇒ Object

Convenience function to perform a facetted search against a store, returning a Results object parsed from the XML response

store

the store against which the query will be performed

query

the search query

facets

an ordered list of facets to be used

params

additional params. See Store.facet for details. XML output is requested automatically



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pho/facet.rb', line 45

def Results.read_from_store(store, query, facets, params=Hash.new)
  
  params["output"] = "xml"
  resp = store.facet(query, facets, params)    
      
  if resp.status != 200
    raise "Unable to do facetted search. Response code was #{resp.status}"
  end
  
  return parse(resp.content)
  
end