Class: SolrLite::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(solr_response, params) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/response.rb', line 9

def initialize(solr_response, params)
  @solr_response = solr_response
  @params = params
  @explainer = nil
  @highlights = nil

  set_facet_values()

  # This value can be set by the client if we want to use a custom
  # representation of solr_docs while preserving the entire Response
  # object.
  @items = []
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



7
8
9
# File 'lib/response.rb', line 7

def items
  @items
end

#solr_responseObject

Returns the value of attribute solr_response.



7
8
9
# File 'lib/response.rb', line 7

def solr_response
  @solr_response
end

Instance Method Details

#endObject



114
115
116
# File 'lib/response.rb', line 114

def end
  [start + page_size, num_found].min
end

#error_msgObject



34
35
36
37
38
# File 'lib/response.rb', line 34

def error_msg
  return "" if @solr_response["error"] == nil
  return "" if @solr_response["error"]["msg"] == nil
  @solr_response["error"]["msg"]
end

#explainerObject



195
196
197
# File 'lib/response.rb', line 195

def explainer()
  @explainer ||= SolrLite::Explainer.new(@solr_response)
end

#facetsObject



143
144
145
# File 'lib/response.rb', line 143

def facets
  @params.facets
end

#groups_foundObject

Total number of groups found in Solr for a grouped request.



65
66
67
68
69
70
71
72
# File 'lib/response.rb', line 65

def groups_found
  if @solr_response["grouped"] != nil && @params.group_count != nil
    return @solr_response["facets"][@params.group_count] || 0
  end
  return 0
rescue
  0
end

#highlightsObject



203
204
205
# File 'lib/response.rb', line 203

def highlights()
  @highlights ||= SolrLite::Highlights.new(@solr_response)
end

#num_foundObject

Total number documents found in solr usually larger than solr_docs.count



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/response.rb', line 42

def num_found

  if @solr_response["response"] != nil
    # Normal Solr query
    return @solr_response["response"]["numFound"]
  end

  if @solr_response["grouped"] != nil
    # Grouped Solr query.
    total = 0
    @solr_response["grouped"].keys.each do |key|
      total += @solr_response["grouped"][key]["matches"]
    end
    return total
  end

  return 0
rescue
  0
end

#num_found_for_group(group_field, group_value) ⇒ Object

Total number documents found in Solr for a given group field/value/



76
77
78
79
80
81
82
83
# File 'lib/response.rb', line 76

def num_found_for_group(group_field, group_value)
  group = @solr_response["grouped"][group_field]["groups"]
  docs_for_value = group.find {|x| x["groupValue"] == group_value }
  return 0 if docs_for_value == nil
  docs_for_value["doclist"]["numFound"]
rescue
  0
end

#num_pagesObject



85
86
87
88
89
90
# File 'lib/response.rb', line 85

def num_pages
  return 0 if page_size == 0
  pages = (num_found / page_size).to_i
  pages += 1 if (num_found % page_size) != 0
  pages
end

#ok?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/response.rb', line 23

def ok?
  return true if status == 0
  return true if status >= 200 && status <= 299
  false
end

#pageObject



118
119
120
121
# File 'lib/response.rb', line 118

def page
  return 1 if page_size == 0 # fail safe
  (start / page_size) + 1
end

#page_sizeObject



92
93
94
95
96
# File 'lib/response.rb', line 92

def page_size
  @solr_response["responseHeader"]["params"]["rows"].to_i
rescue
  0
end

#set_facet_valuesObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/response.rb', line 147

def set_facet_values()
  return if @params == nil
  return if @solr_response["facet_counts"] == nil
  solr_ranges = @solr_response["facet_counts"]["facet_ranges"] || {}
  solr_facets = @solr_response["facet_counts"]["facet_fields"]
  solr_facets.each do |solr_facet|
    # solr_facet is an array with two elements, e.g.
    # ["record_type", ["PEOPLE", 32, "ORGANIZATION", 4]]
    #
    # the first element has the field for the facet (record_type)
    # the second element is an array with of value/count pairs (PEOPLE/32, ORG/4)
    field_name = solr_facet[0]
    facet_field = @params.facet_for_field(field_name)

    if facet_field == nil
      # Solr returned facets for a field that we did not ask for. Ignore it.
      next
    end

    if facet_field.range
      # Use the range values as the facet values.
      #
      # Notice that we are overloading the "values" field and therefore
      # we lose (i.e. don't store) the actual facet values and their counts.
      # We might want to rethink this and keep them both.
      values = solr_ranges[facet_field.name]["counts"] || []
      pairs_count = values.count/2
      for pair in (1..pairs_count)
        index = (pair-1) * 2
        start_range = values[index].to_i
        end_range = start_range + facet_field.range_gap - 1
        count = values[index+1]
        facet_field.add_range(start_range, end_range, count)
      end
    else
      # Regular facet values
      values = solr_facet[1]
      pairs = values.count/2
      for pair in (1..pairs)
        index = (pair-1) * 2
        text = values[index]
        count = values[index+1]
        facet_field.add_value(text, count)
      end
    end
  end
end

#solr_docsObject

Raw solr_docs



124
125
126
# File 'lib/response.rb', line 124

def solr_docs
  @solr_response["response"]["docs"]
end

#solr_docs_for_group(group_field, group_value) ⇒ Object

Documents for a given group field and value (see Solr.search_group())



135
136
137
138
139
140
141
# File 'lib/response.rb', line 135

def solr_docs_for_group(group_field, group_value)
  group = @solr_response["grouped"][group_field]["groups"]
  docs_for_value = group.find {|x| x["groupValue"] == group_value }
  return [] if docs_for_value == nil
  docs = docs_for_value["doclist"]["docs"]
  docs
end

#solr_groups(group_field) ⇒ Object

Groups in the solr_docs (see Solr.search_group())



129
130
131
132
# File 'lib/response.rb', line 129

def solr_groups(group_field)
  return [] if @solr_response["grouped"] == nil
  @solr_response["grouped"][group_field]["groups"].map {|x| x["groupValue"]}
end

#spellcheckObject



199
200
201
# File 'lib/response.rb', line 199

def spellcheck()
  @spellcheck ||= SolrLite::Spellcheck.new(@solr_response)
end

#startObject

Start position for retrieval (used for pagination)



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/response.rb', line 99

def start
  if @solr_response["response"] != nil
    @solr_response["response"]["start"].to_i
  else
    # For grouped responses
    # (I believe we could use this value for grouped and not-grouped
    # responses, but for backwards compatibility and since I have not
    # tested it for non-grouped responses, for now we only use it for
    # grouped responses.)
    @solr_response["responseHeader"]["params"]["start"].to_i
  end
rescue
  0
end

#statusObject



29
30
31
32
# File 'lib/response.rb', line 29

def status
  return -1 if @solr_response["responseHeader"] == nil
  @solr_response["responseHeader"]["status"]
end