solr_lite 0.0.13 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 04c666a78bdf04147350b03ee3f430fdafe36cabfc5d5869538dbbf1e7a963d9
4
- data.tar.gz: cee4e1f082d4a9cbae8ee9e6fb136efd4a20562648a324d62cc7d040d38b672b
2
+ SHA1:
3
+ metadata.gz: 9d3e98c34609bc57613afad765b11e2133809da5
4
+ data.tar.gz: dff5de790a389e362c32d2256e79ef324c70db4b
5
5
  SHA512:
6
- metadata.gz: 23b83c4f7e1f495631f9f01f4f939828a0dbfe78f06397fd2873e99fa2e7b722bf960c7412da0a89be914976b9eaed4a51cf6c9133e66dc674414a6e29d2b7c2
7
- data.tar.gz: 62cbe1329b68a5529c1e0b6c916261d1da9a56fcc9a44981a748a0a1dc34fc44db1713b2e2a50f5601631418fc64698844c508c85d5e91f7e3d3f0f1a5510602
6
+ metadata.gz: cd782b09b42a885272d359d370fd3fb6e2b4d4f8e322e0d134f4dfb77ffb516594c4994d049c5c16678b19ee9143ef7e1a892fac0d87f4a3ddb92aa5af49cdc3
7
+ data.tar.gz: 8462ddab0645f0c005e3d444d55ca64421b78cf3b85e00f3e8e689fb25febdfc316a68246b28a6a9906a6d471fd4a0cd4c9c88a3971852822628b96baa7b3b2c
@@ -60,6 +60,17 @@ module SolrLite
60
60
  0
61
61
  end
62
62
 
63
+ # Total number of groups found in Solr
64
+ # for a grouped request.
65
+ def groups_found
66
+ if @solr_response["grouped"] != nil && @params.group_count != nil
67
+ return @solr_response["facets"][@params.group_count] || 0
68
+ end
69
+ return 0
70
+ rescue
71
+ 0
72
+ end
73
+
63
74
  # Total number documents found in Solr
64
75
  # for a given group field/value/
65
76
  def num_found_for_group(group_field, group_value)
@@ -134,6 +145,7 @@ module SolrLite
134
145
  end
135
146
 
136
147
  def set_facet_values()
148
+ return if @params == nil
137
149
  return if @solr_response["facet_counts"] == nil
138
150
  solr_ranges = @solr_response["facet_counts"]["facet_ranges"] || {}
139
151
  solr_facets = @solr_response["facet_counts"]["facet_fields"]
@@ -145,6 +157,12 @@ module SolrLite
145
157
  # the second element is an array with of value/count pairs (PEOPLE/32, ORG/4)
146
158
  field_name = solr_facet[0]
147
159
  facet_field = @params.facet_for_field(field_name)
160
+
161
+ if facet_field == nil
162
+ # Solr returned facets for a field that we did not ask for. Ignore it.
163
+ next
164
+ end
165
+
148
166
  if facet_field.range
149
167
  # Use the range values as the facet values.
150
168
  #
@@ -33,15 +33,25 @@ module SolrLite
33
33
  # [Bool] True to request Solr to use spellchecking (defaults to false).
34
34
  attr_accessor :spellcheck
35
35
 
36
- # [Bool] Set to true to request hit highlighting information from Solr.
36
+ # [Bool] Set to true to request hit highlighting information from Solr.
37
37
  attr_accessor :hl
38
38
 
39
- # [String] Sets the highlight fields (hl.fl) to request from Solr.
39
+ # [String] Sets the highlight fields (hl.fl) to request from Solr.
40
40
  attr_accessor :hl_fl
41
41
 
42
- # [Integer] Sets the number of hit highlights to request from Solr.
42
+ # [Integer] Sets the number of hit highlights to request from Solr.
43
43
  attr_accessor :hl_snippets
44
44
 
45
+ # [String] The name of the value in the response to hold the number of groups found
46
+ # in a grouped request. Defaults to "group_count", set to nil to omit.
47
+ attr_accessor :group_count
48
+
49
+ # [String] Sets the qf value to pass to Solr.
50
+ attr_accessor :qf
51
+
52
+ # [String] Sets the pf value to pass to Solr.
53
+ attr_accessor :pf
54
+
45
55
  DEFAULT_PAGE_SIZE = 20
46
56
 
47
57
  # Creates an instance of the SearchParams class.
@@ -64,6 +74,9 @@ module SolrLite
64
74
  @hl = false
65
75
  @hl_fl = nil
66
76
  @hl_snippets = 1
77
+ @group_count = "group_count"
78
+ @qf = nil
79
+ @pf = nil
67
80
  end
68
81
 
69
82
  # Returns facet information about a given field.
@@ -175,6 +188,14 @@ module SolrLite
175
188
  qs += "&sort=#{CGI.escape(@sort)}"
176
189
  end
177
190
 
191
+ if @qf != nil
192
+ qs += "&qf=#{@qf}"
193
+ end
194
+
195
+ if @pf != nil
196
+ qs += "&pf=#{@pf}"
197
+ end
198
+
178
199
  if @spellcheck
179
200
  qs += "&spellcheck=on"
180
201
  end
@@ -58,6 +58,25 @@ module SolrLite
58
58
  solr_response.solr_docs.first
59
59
  end
60
60
 
61
+ def get_many(ids, q_field = "q", fl = "*", batch_size = 20)
62
+ data = []
63
+ batches = to_batches(ids, batch_size)
64
+ batches.each do |batch|
65
+ ids_string = batch.join(" OR ")
66
+ query_string = "#{q_field}=id%3A(#{ids_string})" # %3A => :
67
+ query_string += "&fl=#{fl}"
68
+ query_string += "&rows=#{batch_size}"
69
+ query_string += "&wt=json&indent=on"
70
+ if @def_type != nil
71
+ query_string += "&defType=#{@def_type}"
72
+ end
73
+ url = "#{@solr_url}/select?#{query_string}"
74
+ solr_response = Response.new(http_get(url), nil)
75
+ data += solr_response.solr_docs
76
+ end
77
+ data
78
+ end
79
+
61
80
  # Issues a search request to Solr.
62
81
  #
63
82
  # @param params [SolrLite::SearchParams] Search parameters.
@@ -76,8 +95,8 @@ module SolrLite
76
95
  response
77
96
  end
78
97
 
79
- def search_group(params, extra_fqs = [], qf = nil, mm = nil, debug = false, group_field, group_limit)
80
- http_response = search_core(params, extra_fqs, qf, mm, debug, group_field, group_limit)
98
+ def search_group(params, extra_fqs = [], qf = nil, mm = nil, debug = false, group_field = nil, group_limit = nil, group_extra = nil)
99
+ http_response = search_core(params, extra_fqs, qf, mm, debug, group_field, group_limit, group_extra)
81
100
  response = Response.new(http_response, params)
82
101
  response
83
102
  end
@@ -155,7 +174,7 @@ module SolrLite
155
174
  end
156
175
 
157
176
  private
158
- def search_core(params, extra_fqs, qf, mm, debug, group_field, group_limit)
177
+ def search_core(params, extra_fqs, qf, mm, debug, group_field, group_limit, group_extra = nil)
159
178
  if params.fl != nil
160
179
  query_string = "fl=#{params.fl.join(",")}"
161
180
  else
@@ -182,6 +201,20 @@ module SolrLite
182
201
  # See https://lucene.apache.org/solr/guide/7_0/result-grouping.html
183
202
  # and https://wiki.apache.org/solr/FieldCollapsing
184
203
  query_string += "&group=true&group.field=#{group_field}&group.limit=#{group_limit}"
204
+
205
+ if params.group_count != nil
206
+ # Adds an extra calculated facet to get the total number of groups. This is required
207
+ # because Solr does not return this value in the response, instead Solr
208
+ # returns the total number of documents found across all groups, but not
209
+ # the total number of groups found.
210
+ # See https://lucene.apache.org/solr/guide/7_7/json-facet-api.html#metrics-example
211
+ # and https://stackoverflow.com/a/56138991/446681
212
+ query_string += '&json.facet={"' + params.group_count + '":"unique(' + group_field + ')"}'
213
+ end
214
+
215
+ if group_extra != nil
216
+ query_string += "&#{group_extra}"
217
+ end
185
218
  end
186
219
 
187
220
  if @def_type != nil
@@ -244,5 +277,22 @@ module SolrLite
244
277
  @logger.info(msg)
245
278
  end
246
279
  end
280
+
281
+ def to_batches(arr, batch_size)
282
+ batch_count = (arr.count / batch_size)
283
+ if (arr.count % batch_size) > 0
284
+ batch_count += 1
285
+ end
286
+
287
+ batches = []
288
+ (1..batch_count).each do |i|
289
+ start = (i-1) * batch_size
290
+ stop = start + batch_size - 1
291
+ batch = arr[start..stop]
292
+ batches << batch
293
+ end
294
+
295
+ batches
296
+ end
247
297
  end
248
298
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solr_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hector Correa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-02 00:00:00.000000000 Z
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A lightweight gem to connect to Solr and run queries. Requires no extra
14
14
  dependencies.
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 2.7.7
50
+ rubygems_version: 2.6.14.1
51
51
  signing_key:
52
52
  specification_version: 4
53
53
  summary: A lightweight gem to connect to Solr and run queries