blacklight 7.14.1 → 7.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -0
  3. data/VERSION +1 -1
  4. data/app/assets/stylesheets/blacklight/_icons.scss +5 -1
  5. data/app/assets/stylesheets/blacklight/blacklight_defaults.scss +5 -0
  6. data/app/components/blacklight/advanced_search_form_component.html.erb +52 -0
  7. data/app/components/blacklight/advanced_search_form_component.rb +88 -0
  8. data/app/components/blacklight/constraint_component.html.erb +1 -1
  9. data/app/components/blacklight/constraint_layout_component.html.erb +1 -1
  10. data/app/components/blacklight/constraints_component.html.erb +19 -3
  11. data/app/components/blacklight/constraints_component.rb +41 -18
  12. data/app/components/blacklight/content_areas_shim.rb +12 -0
  13. data/app/components/blacklight/document/action_component.html.erb +1 -1
  14. data/app/components/blacklight/document/action_component.rb +6 -1
  15. data/app/components/blacklight/document/actions_component.html.erb +3 -5
  16. data/app/components/blacklight/document/actions_component.rb +16 -2
  17. data/app/components/blacklight/document/thumbnail_component.html.erb +1 -1
  18. data/app/components/blacklight/document/thumbnail_component.rb +4 -1
  19. data/app/components/blacklight/document_component.html.erb +4 -7
  20. data/app/components/blacklight/document_component.rb +73 -68
  21. data/app/components/blacklight/document_metadata_component.html.erb +2 -2
  22. data/app/components/blacklight/document_metadata_component.rb +13 -2
  23. data/app/components/blacklight/document_title_component.html.erb +17 -0
  24. data/app/components/blacklight/document_title_component.rb +59 -0
  25. data/app/components/blacklight/facet_field_checkboxes_component.html.erb +23 -0
  26. data/app/components/blacklight/facet_field_checkboxes_component.rb +24 -0
  27. data/app/components/blacklight/facet_field_component.rb +4 -1
  28. data/app/components/blacklight/facet_field_inclusive_constraint_component.html.erb +6 -0
  29. data/app/components/blacklight/facet_field_inclusive_constraint_component.rb +29 -0
  30. data/app/components/blacklight/facet_field_list_component.html.erb +3 -2
  31. data/app/components/blacklight/facet_field_no_layout_component.rb +4 -1
  32. data/app/components/blacklight/facet_field_pagination_component.rb +1 -1
  33. data/app/components/blacklight/facet_item_component.rb +4 -2
  34. data/app/components/blacklight/metadata_field_component.html.erb +2 -2
  35. data/app/components/blacklight/metadata_field_layout_component.html.erb +3 -1
  36. data/app/components/blacklight/metadata_field_layout_component.rb +26 -1
  37. data/app/components/blacklight/response/view_type_button_component.html.erb +4 -0
  38. data/app/components/blacklight/response/view_type_button_component.rb +36 -0
  39. data/app/components/blacklight/response/view_type_component.html.erb +2 -5
  40. data/app/components/blacklight/response/view_type_component.rb +9 -13
  41. data/app/components/blacklight/search_bar_component.html.erb +4 -0
  42. data/app/components/blacklight/search_bar_component.rb +13 -4
  43. data/app/components/blacklight/system/dropdown_component.html.erb +4 -7
  44. data/app/components/blacklight/system/dropdown_component.rb +24 -0
  45. data/app/components/blacklight/system/flash_message_component.html.erb +1 -1
  46. data/app/components/blacklight/system/flash_message_component.rb +7 -1
  47. data/app/components/blacklight/system/modal_component.rb +7 -1
  48. data/app/controllers/concerns/blacklight/catalog.rb +7 -1
  49. data/app/helpers/blacklight/blacklight_helper_behavior.rb +3 -4
  50. data/app/helpers/blacklight/component_helper_behavior.rb +2 -2
  51. data/app/helpers/blacklight/configuration_helper_behavior.rb +2 -2
  52. data/app/helpers/blacklight/render_constraints_helper_behavior.rb +2 -2
  53. data/app/presenters/blacklight/clause_presenter.rb +37 -0
  54. data/app/presenters/blacklight/document_presenter.rb +13 -5
  55. data/app/presenters/blacklight/facet_field_presenter.rb +4 -0
  56. data/app/presenters/blacklight/facet_grouped_item_presenter.rb +45 -0
  57. data/app/presenters/blacklight/facet_item_presenter.rb +32 -20
  58. data/app/presenters/blacklight/inclusive_facet_item_presenter.rb +16 -0
  59. data/app/presenters/blacklight/rendering/helper_method.rb +4 -4
  60. data/app/presenters/blacklight/search_bar_presenter.rb +4 -0
  61. data/app/services/blacklight/search_service.rb +1 -1
  62. data/app/views/bookmarks/_tools.html.erb +1 -1
  63. data/app/views/catalog/_advanced_search_form.html.erb +7 -0
  64. data/app/views/catalog/_advanced_search_help.html.erb +24 -0
  65. data/app/views/catalog/_citation.html.erb +1 -1
  66. data/app/views/catalog/_document.html.erb +2 -2
  67. data/app/views/catalog/_facet_layout.html.erb +2 -2
  68. data/app/views/catalog/_search_form.html.erb +1 -0
  69. data/app/views/catalog/_show_main_content.html.erb +3 -3
  70. data/app/views/catalog/advanced_search.html.erb +17 -0
  71. data/app/views/catalog/email.html.erb +2 -2
  72. data/app/views/catalog/email_success.html.erb +1 -1
  73. data/app/views/catalog/facet.html.erb +3 -3
  74. data/app/views/catalog/sms.html.erb +2 -2
  75. data/app/views/catalog/sms_success.html.erb +1 -1
  76. data/blacklight.gemspec +2 -2
  77. data/config/i18n-tasks.yml +1 -0
  78. data/config/locales/blacklight.de.yml +2 -2
  79. data/config/locales/blacklight.en.yml +17 -0
  80. data/lib/blacklight/configuration.rb +52 -6
  81. data/lib/blacklight/configuration/view_config.rb +16 -5
  82. data/lib/blacklight/engine.rb +3 -1
  83. data/lib/blacklight/open_struct_with_hash_access.rb +22 -1
  84. data/lib/blacklight/routes/searchable.rb +1 -0
  85. data/lib/blacklight/search_builder.rb +2 -0
  86. data/lib/blacklight/search_state.rb +7 -3
  87. data/lib/blacklight/search_state/filter_field.rb +17 -7
  88. data/lib/blacklight/solr/facet_paginator.rb +2 -0
  89. data/lib/blacklight/solr/repository.rb +11 -2
  90. data/lib/blacklight/solr/request.rb +31 -0
  91. data/lib/blacklight/solr/response.rb +2 -16
  92. data/lib/blacklight/solr/response/facets.rb +76 -22
  93. data/lib/blacklight/solr/response/params.rb +104 -0
  94. data/lib/blacklight/solr/search_builder_behavior.rb +126 -32
  95. data/lib/generators/blacklight/assets_generator.rb +6 -2
  96. data/lib/generators/blacklight/user_generator.rb +1 -1
  97. data/spec/components/blacklight/advanced_search_form_component_spec.rb +51 -0
  98. data/spec/components/blacklight/document_component_spec.rb +18 -3
  99. data/spec/components/blacklight/facet_field_checkboxes_component_spec.rb +55 -0
  100. data/spec/components/blacklight/facet_field_list_component_spec.rb +39 -4
  101. data/spec/controllers/catalog_controller_spec.rb +9 -0
  102. data/spec/features/advanced_search_spec.rb +67 -0
  103. data/spec/lib/blacklight/configuration/view_config_spec.rb +1 -1
  104. data/spec/lib/blacklight/open_struct_with_hash_access_spec.rb +20 -0
  105. data/spec/lib/blacklight/search_state/filter_field_spec.rb +65 -0
  106. data/spec/models/blacklight/configuration_spec.rb +64 -0
  107. data/spec/models/blacklight/solr/facet_paginator_spec.rb +4 -0
  108. data/spec/models/blacklight/solr/repository_spec.rb +12 -0
  109. data/spec/models/blacklight/solr/request_spec.rb +62 -29
  110. data/spec/models/blacklight/solr/response/facets_spec.rb +109 -0
  111. data/spec/models/blacklight/solr/response_spec.rb +10 -0
  112. data/spec/models/blacklight/solr/search_builder_spec.rb +77 -0
  113. data/spec/presenters/blacklight/clause_presenter_spec.rb +34 -0
  114. data/spec/presenters/blacklight/document_presenter_spec.rb +13 -0
  115. data/spec/presenters/blacklight/facet_grouped_item_presenter_spec.rb +41 -0
  116. data/spec/views/catalog/index.atom.builder_spec.rb +1 -1
  117. metadata +37 -9
@@ -29,6 +29,7 @@ module Blacklight::Solr::Response::Facets
29
29
  # represents a facet; which is a field and its values
30
30
  class FacetField
31
31
  attr_reader :name, :items
32
+ attr_accessor :missing
32
33
 
33
34
  def initialize name, items, options = {}
34
35
  @name = name
@@ -52,6 +53,14 @@ module Blacklight::Solr::Response::Facets
52
53
  @options[:prefix] || solr_default_prefix
53
54
  end
54
55
 
56
+ def type
57
+ @options[:type] || 'terms'
58
+ end
59
+
60
+ def data
61
+ @options[:data] || {}
62
+ end
63
+
55
64
  def index?
56
65
  sort == 'index'
57
66
  end
@@ -90,7 +99,7 @@ module Blacklight::Solr::Response::Facets
90
99
  # Get all the Solr facet data (fields, queries, pivots) as a hash keyed by
91
100
  # both the Solr field name and/or by the blacklight field name
92
101
  def aggregations
93
- @aggregations ||= {}.merge(facet_field_aggregations).merge(facet_query_aggregations).merge(facet_pivot_aggregations)
102
+ @aggregations ||= {}.merge(facet_field_aggregations).merge(facet_query_aggregations).merge(facet_pivot_aggregations).merge(json_facet_aggregations)
94
103
  end
95
104
 
96
105
  def facet_counts
@@ -159,19 +168,24 @@ module Blacklight::Solr::Response::Facets
159
168
  items = values.map do |value, hits|
160
169
  i = FacetItem.new(value: value, hits: hits)
161
170
 
162
- # solr facet.missing serialization
171
+ # legacy solr facet.missing serialization
163
172
  if value.nil?
164
173
  i.label = I18n.t(:"blacklight.search.fields.facet.missing.#{facet_field_name}", default: [:"blacklight.search.facets.missing"])
165
174
  i.fq = "-#{facet_field_name}:[* TO *]"
175
+ i.missing = true
166
176
  end
167
177
 
168
178
  i
169
179
  end
170
180
 
171
181
  options = facet_field_aggregation_options(facet_field_name)
172
- hash[facet_field_name] = FacetField.new(facet_field_name,
173
- items,
174
- options)
182
+ facet_field = FacetField.new(facet_field_name, items, options)
183
+
184
+ if values[nil]
185
+ facet_field.missing = items.find(&:missing)
186
+ end
187
+
188
+ hash[facet_field_name] = facet_field
175
189
 
176
190
  # alias all the possible blacklight config names..
177
191
  blacklight_config.facet_fields.select { |_k, v| v.field == facet_field_name }.each_key do |key|
@@ -180,23 +194,6 @@ module Blacklight::Solr::Response::Facets
180
194
  end
181
195
  end
182
196
 
183
- def facet_field_aggregation_options(facet_field_name)
184
- options = {}
185
- options[:sort] = (params[:"f.#{facet_field_name}.facet.sort"] || params[:'facet.sort'])
186
- if params[:"f.#{facet_field_name}.facet.limit"] || params[:"facet.limit"]
187
- options[:limit] = (params[:"f.#{facet_field_name}.facet.limit"] || params[:"facet.limit"]).to_i
188
- end
189
-
190
- if params[:"f.#{facet_field_name}.facet.offset"] || params[:'facet.offset']
191
- options[:offset] = (params[:"f.#{facet_field_name}.facet.offset"] || params[:'facet.offset']).to_i
192
- end
193
-
194
- if params[:"f.#{facet_field_name}.facet.prefix"] || params[:'facet.prefix']
195
- options[:prefix] = (params[:"f.#{facet_field_name}.facet.prefix"] || params[:'facet.prefix'])
196
- end
197
- options
198
- end
199
-
200
197
  ##
201
198
  # Aggregate Solr's facet_query response into the virtual facet fields defined
202
199
  # in the blacklight configuration
@@ -211,12 +208,28 @@ module Blacklight::Solr::Response::Facets
211
208
  Blacklight::Solr::Response::Facets::FacetItem.new(value: key, hits: hits, label: facet_field.query[key][:label])
212
209
  end
213
210
 
211
+ items += facet_query_aggregations_from_json(facet_field)
212
+
214
213
  items = items.sort_by(&:hits).reverse if facet_field.sort && facet_field.sort.to_sym == :count
215
214
 
216
215
  hash[field_name] = Blacklight::Solr::Response::Facets::FacetField.new field_name, items
217
216
  end
218
217
  end
219
218
 
219
+ def facet_query_aggregations_from_json(facet_field)
220
+ return [] unless self['facets']
221
+
222
+ salient_facet_queries = facet_field.query.map { |_k, x| x[:fq] }
223
+
224
+ relevant_facet_data = self['facets'].select { |k, _v| salient_facet_queries.include?(k) }.reject { |_key, data| data['count'].zero? }
225
+
226
+ relevant_facet_data.map do |key, data|
227
+ salient_fields = facet_field.query.select { |_key, val| val[:fq] == key }
228
+ facet_key = ((salient_fields.keys if salient_fields.respond_to? :keys) || salient_fields.first).first
229
+ Blacklight::Solr::Response::Facets::FacetItem.new(value: facet_key, hits: data[:count], label: facet_field.query[facet_key][:label])
230
+ end
231
+ end
232
+
220
233
  ##
221
234
  # Convert Solr's facet_pivot response into
222
235
  # a hash of Blacklight::Solr::Response::Facet::FacetField objects
@@ -244,4 +257,45 @@ module Blacklight::Solr::Response::Facets
244
257
 
245
258
  Blacklight::Solr::Response::Facets::FacetItem.new(value: lst[:value], hits: lst[:count], field: lst[:field], items: items, fq: parent_fq)
246
259
  end
260
+
261
+ def construct_json_nested_facet_fields(bucket, parent_fq = {})
262
+ bucket.select { |_, nested| nested.is_a?(Hash) && nested.key?('buckets') }.map do |facet_field_name, nested|
263
+ nested['buckets'].map do |subbucket|
264
+ i = Blacklight::Solr::Response::Facets::FacetItem.new(field: facet_field_name, value: subbucket['val'], hits: subbucket['count'], fq: parent_fq, data: subbucket)
265
+
266
+ i.items = construct_json_nested_facet_fields(subbucket, parent_fq.merge(key => subbucket['val'])) if has_json_nested_facets?(subbucket)
267
+ i
268
+ end
269
+ end.flatten
270
+ end
271
+
272
+ def has_json_nested_facets?(bucket)
273
+ bucket.any? { |_, nested| nested.is_a?(Hash) && nested.key?('buckets') }
274
+ end
275
+
276
+ def json_facet_aggregations
277
+ return {} unless self['facets']
278
+
279
+ self['facets'].each_with_object({}) do |(facet_field_name, data), hash|
280
+ next if facet_field_name == 'count'
281
+
282
+ items = (data['buckets'] || []).map do |bucket|
283
+ i = Blacklight::Solr::Response::Facets::FacetItem.new(value: bucket['val'], hits: bucket['count'], data: bucket)
284
+
285
+ i.items = construct_json_nested_facet_fields(bucket, facet_field_name => bucket['val']) if has_json_nested_facets?(bucket)
286
+
287
+ i
288
+ end
289
+
290
+ options = facet_field_aggregation_options(facet_field_name).merge(data: data)
291
+ facet_field = FacetField.new(facet_field_name, items, options)
292
+
293
+ facet_field.missing = Blacklight::Solr::Response::Facets::FacetItem.new(
294
+ hits: data.dig('missing', 'count'),
295
+ data: data['missing']
296
+ ) if data['missing']
297
+
298
+ hash[facet_field_name] = facet_field
299
+ end
300
+ end
247
301
  end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+ module Blacklight::Solr::Response::Params
3
+ # From https://solr.apache.org/guide/8_8/json-request-api.html#supported-properties-and-syntax
4
+ QUERY_PARAMETER_TO_JSON_PARAMETER_MAPPING = {
5
+ q: :query,
6
+ fq: :filter,
7
+ start: :offset,
8
+ rows: :limit,
9
+ fl: :fields,
10
+ sort: :sort
11
+ }.freeze
12
+
13
+ def params
14
+ header['params'] || request_params
15
+ end
16
+
17
+ def start
18
+ search_builder&.start || single_valued_param(:start).to_i
19
+ end
20
+
21
+ def rows
22
+ search_builder&.rows || single_valued_param(:rows).to_i
23
+ end
24
+
25
+ def sort
26
+ search_builder&.sort || single_valued_param(:sort)
27
+ end
28
+
29
+ def facet_field_aggregation_options(facet_field_name)
30
+ defaults = {
31
+ sort: single_valued_param(:'facet.sort'),
32
+ limit: single_valued_param(:"facet.limit")&.to_i || 100,
33
+ offset: single_valued_param(:"facet.offset")&.to_i || 0,
34
+ prefix: single_valued_param(:"facet.prefix")
35
+ }
36
+
37
+ json_facet = json_params.dig('facet', facet_field_name)&.slice(:limit, :offset, :prefix, :sort)&.symbolize_keys || {}
38
+
39
+ param_facet = {
40
+ sort: single_valued_param(:"f.#{facet_field_name}.facet.sort"),
41
+ limit: single_valued_param(:"f.#{facet_field_name}.facet.limit")&.to_i,
42
+ offset: single_valued_param(:"f.#{facet_field_name}.facet.offset")&.to_i,
43
+ prefix: single_valued_param(:"f.#{facet_field_name}.facet.prefix")
44
+ }.reject { |_k, v| v.nil? }
45
+
46
+ options = defaults.merge(json_facet).merge(param_facet)
47
+ options[:sort] ||= options[:limit].positive? ? 'count' : 'index'
48
+
49
+ options
50
+ end
51
+
52
+ private
53
+
54
+ def search_builder
55
+ request_params if request_params.is_a?(Blacklight::SearchBuilder)
56
+ end
57
+
58
+ # Extract JSON Request API parameters from the response header or the request itself
59
+ def json_params
60
+ encoded_json_params = header&.dig('params', 'json')
61
+
62
+ return request_params['json'] || {} if encoded_json_params.blank?
63
+
64
+ @json_params ||= JSON.parse(encoded_json_params).with_indifferent_access
65
+ end
66
+
67
+ # Handle merging solr parameters from the myriad of ways they may be expressed by applying the single-value
68
+ # precedence logic:
69
+ #
70
+ # From https://solr.apache.org/guide/8_8/json-request-api.html#json-parameter-merging :
71
+ # When multiple parameter values conflict with one another a single value is chosen based on the following precedence rules:
72
+ # - Traditional query parameters (q, rows, etc.) take first precedence and are used over any other specified values.
73
+ # - json-prefixed query parameters are considered next.
74
+ # - Values specified in the JSON request body have the lowest precedence and are only used if specified nowhere else.
75
+ #
76
+ # @param [String] key the solr parameter to use
77
+ def single_valued_param(key)
78
+ json_key = QUERY_PARAMETER_TO_JSON_PARAMETER_MAPPING[key]
79
+
80
+ params[key] ||
81
+ params["json.#{key}"] ||
82
+ json_params[json_key || key] ||
83
+ json_params.dig(:params, key) ||
84
+ json_params.dig(:params, "json.#{key}")
85
+ end
86
+
87
+ # Merge together multi-valued solr parameters from the myriad of ways they may be expressed.
88
+ # Unlike single-valued parameters, this merges all the values across the params.
89
+ #
90
+ # @param [String] key the solr parameter to use
91
+ def multivalued_param(key)
92
+ json_key = QUERY_PARAMETER_TO_JSON_PARAMETER_MAPPING[key]
93
+
94
+ [
95
+ params[key],
96
+ params["json.#{key}"],
97
+ json_params[json_key || key],
98
+ json_params.dig(:params, key),
99
+ json_params.dig(:params, "json.#{key}")
100
+ ].select(&:present?).inject([]) do |memo, arr|
101
+ memo.concat(Array.wrap(arr))
102
+ end
103
+ end
104
+ end
@@ -8,7 +8,8 @@ module Blacklight::Solr
8
8
  :default_solr_parameters, :add_query_to_solr, :add_facet_fq_to_solr,
9
9
  :add_facetting_to_solr, :add_solr_fields_to_query, :add_paging_to_solr,
10
10
  :add_sorting_to_solr, :add_group_config_to_solr,
11
- :add_facet_paging_to_solr
11
+ :add_facet_paging_to_solr, :add_adv_search_clauses,
12
+ :add_additional_filters
12
13
  ]
13
14
  end
14
15
 
@@ -58,15 +59,69 @@ module Blacklight::Solr
58
59
  ##
59
60
  if search_field&.query_builder.present?
60
61
  add_search_field_query_builder_params(solr_parameters)
62
+ elsif search_field&.clause_params.present?
63
+ add_search_field_with_json_query_parameters(solr_parameters)
61
64
  elsif search_field&.solr_local_parameters.present?
62
65
  add_search_field_with_local_parameters(solr_parameters)
63
66
  elsif search_state.query_param.is_a? Hash
64
- add_multifield_search_query(solr_parameters)
65
- elsif blacklight_params[:q]
66
- solr_parameters[:q] = search_state.query_param
67
+ add_additional_filters(solr_parameters, search_state.query_param)
68
+ elsif search_state.query_param
69
+ solr_parameters.append_query search_state.query_param
67
70
  end
68
71
  end
69
72
 
73
+ def add_additional_filters(solr_parameters, additional_filters = nil)
74
+ q = additional_filters || @additional_filters
75
+
76
+ return if q.blank?
77
+
78
+ if q.values.any?(&:blank?)
79
+ # if any field parameters are empty, exclude _all_ results
80
+ solr_parameters.append_query "{!lucene}NOT *:*"
81
+ else
82
+ composed_query = q.map do |field, values|
83
+ "#{field}:(#{Array(values).map { |x| solr_param_quote(x) }.join(' OR ')})"
84
+ end.join(" AND ")
85
+
86
+ solr_parameters.append_query "{!lucene}#{composed_query}"
87
+ end
88
+
89
+ solr_parameters[:defType] = 'lucene'
90
+ solr_parameters[:spellcheck] = 'false'
91
+ end
92
+
93
+ def add_search_field_with_json_query_parameters(solr_parameters)
94
+ bool_query = search_field.clause_params.transform_values { |v| v.merge(query: search_state.query_param) }
95
+
96
+ solr_parameters.append_boolean_query(:must, bool_query)
97
+ end
98
+
99
+ # Transform "clause" parameters into the Solr JSON Query DSL
100
+ def add_adv_search_clauses(solr_parameters)
101
+ return if search_state.clause_params.blank?
102
+
103
+ defaults = { must: [], must_not: [], should: [] }
104
+ default_op = blacklight_params[:op]&.to_sym || :must
105
+ solr_parameters[:mm] = 1 if default_op == :should && search_state.clause_params.values.any? { |clause| }
106
+
107
+ search_state.clause_params.each_value do |clause|
108
+ op, query = adv_search_clause(clause, default_op)
109
+ next unless defaults.key?(op)
110
+
111
+ solr_parameters.append_boolean_query(op, query)
112
+ end
113
+ end
114
+
115
+ # @return [Array] the first element is the query operator and the second is the value to add
116
+ def adv_search_clause(clause, default_op)
117
+ op = clause[:op]&.to_sym || default_op
118
+ field = (blacklight_config.search_fields || {})[clause[:field]] if clause[:field]
119
+
120
+ return unless field&.clause_params && clause[:query].present?
121
+
122
+ [op, field.clause_params.transform_values { |v| v.merge(query: clause[:query]) }]
123
+ end
124
+
70
125
  ##
71
126
  # Add any existing facet limits, stored in app-level HTTP query
72
127
  # as :f, to solr as appropriate :fq query.
@@ -80,18 +135,38 @@ module Blacklight::Solr
80
135
  if filter.config.filter_query_builder
81
136
  filter_query, subqueries = filter.config.filter_query_builder.call(self, filter, solr_parameters)
82
137
 
83
- solr_parameters.append_filter_query(filter_query)
138
+ solr_parameters.append_filter_query(filter_query) if filter_query
84
139
  solr_parameters.merge!(subqueries) if subqueries
85
140
  else
86
141
  filter.values.reject(&:blank?).each do |value|
87
- filter_query, subqueries = facet_value_to_fq_string(filter.config.key, value)
88
- solr_parameters.append_filter_query(filter_query)
142
+ filter_query, subqueries = if value.is_a?(Array)
143
+ facet_inclusive_value_to_fq_string(filter.key, value.reject(&:blank?))
144
+ else
145
+ facet_value_to_fq_string(filter.config.key, value)
146
+ end
147
+
148
+ solr_parameters.append_filter_query filter_query
89
149
  solr_parameters.merge!(subqueries) if subqueries
90
150
  end
91
151
  end
92
152
  end
93
153
  end
94
154
 
155
+ def add_solr_facet_json_params(solr_parameters, field_name, facet, **additional_parameters)
156
+ solr_parameters[:json] ||= { facet: {} }
157
+ solr_parameters[:json][:facet] ||= {}
158
+
159
+ field_config = facet.json.respond_to?(:reverse_merge) ? facet.json : {}
160
+
161
+ field_config = field_config.reverse_merge(
162
+ type: 'terms',
163
+ field: facet.field,
164
+ limit: facet_limit_with_pagination(field_name)
165
+ ).merge(additional_parameters)
166
+
167
+ solr_parameters[:json][:facet][field_name] = field_config.select { |_k, v| v.present? }
168
+ end
169
+
95
170
  ##
96
171
  # Add appropriate Solr facetting directives in, including
97
172
  # taking account of our facet paging/'more'. This is not
@@ -100,6 +175,11 @@ module Blacklight::Solr
100
175
  facet_fields_to_include_in_request.each do |field_name, facet|
101
176
  solr_parameters[:facet] ||= true
102
177
 
178
+ if facet.json
179
+ add_solr_facet_json_params(solr_parameters, field_name, facet)
180
+ next
181
+ end
182
+
103
183
  if facet.pivot
104
184
  solr_parameters.append_facet_pivot with_ex_local_param(facet.ex, facet.pivot.join(","))
105
185
  elsif facet.query
@@ -169,9 +249,7 @@ module Blacklight::Solr
169
249
 
170
250
  facet_config = blacklight_config.facet_fields[facet]
171
251
 
172
- # Now override with our specific things for fetching facet values
173
- facet_ex = facet_config.respond_to?(:ex) ? facet_config.ex : nil
174
- solr_params[:"facet.field"] = with_ex_local_param(facet_ex, facet_config.field)
252
+ solr_params[:rows] = 0
175
253
 
176
254
  limit = if solr_params["facet.limit"]
177
255
  solr_params["facet.limit"].to_i
@@ -184,13 +262,21 @@ module Blacklight::Solr
184
262
  prefix = search_state.facet_prefix
185
263
  offset = (page - 1) * limit
186
264
 
265
+ if facet_config.json
266
+ add_solr_facet_json_params(solr_parameters, facet, facet_config, limit: limit + 1, offset: offset, sort: sort, prefix: prefix)
267
+ return
268
+ end
269
+
270
+ # Now override with our specific things for fetching facet values
271
+ facet_ex = facet_config.respond_to?(:ex) ? facet_config.ex : nil
272
+ solr_params[:"facet.field"] = with_ex_local_param(facet_ex, facet_config.field)
273
+
187
274
  # Need to set as f.facet_field.facet.* to make sure we
188
275
  # override any field-specific default in the solr request handler.
189
276
  solr_params[:"f.#{facet_config.field}.facet.limit"] = limit + 1
190
277
  solr_params[:"f.#{facet_config.field}.facet.offset"] = offset
191
278
  solr_params[:"f.#{facet_config.field}.facet.sort"] = sort if sort
192
279
  solr_params[:"f.#{facet_config.field}.facet.prefix"] = prefix if prefix
193
- solr_params[:rows] = 0
194
280
  end
195
281
 
196
282
  def with_ex_local_param(ex, value)
@@ -250,16 +336,17 @@ module Blacklight::Solr
250
336
 
251
337
  ##
252
338
  # Convert a facet/value pair into a solr fq parameter
253
- def facet_value_to_fq_string(facet_field, value)
339
+ def facet_value_to_fq_string(facet_field, value, use_local_params: true)
254
340
  facet_config = blacklight_config.facet_fields[facet_field]
255
341
 
256
342
  solr_field = facet_config.field if facet_config && !facet_config.query
257
343
  solr_field ||= facet_field
258
344
 
259
345
  local_params = []
260
- local_params << "tag=#{facet_config.tag}" if facet_config && facet_config.tag
261
346
 
262
- prefix = "{!#{local_params.join(' ')}}" unless local_params.empty?
347
+ if use_local_params
348
+ local_params << "tag=#{facet_config.tag}" if facet_config && facet_config.tag
349
+ end
263
350
 
264
351
  if facet_config && facet_config.query
265
352
  if facet_config.query[value]
@@ -269,12 +356,34 @@ module Blacklight::Solr
269
356
  '-*:*'
270
357
  end
271
358
  elsif value.is_a?(Range)
359
+ prefix = "{!#{local_params.join(' ')}}" unless local_params.empty?
272
360
  "#{prefix}#{solr_field}:[#{value.first} TO #{value.last}]"
273
361
  else
274
362
  "{!term f=#{solr_field}#{(' ' + local_params.join(' ')) unless local_params.empty?}}#{convert_to_term_value(value)}"
275
363
  end
276
364
  end
277
365
 
366
+ def facet_inclusive_value_to_fq_string(facet_field, values)
367
+ return if values.blank?
368
+
369
+ return facet_value_to_fq_string(facet_field, values.first) if values.length == 1
370
+
371
+ facet_config = blacklight_config.facet_fields[facet_field]
372
+
373
+ local_params = []
374
+ local_params << "tag=#{facet_config.tag}" if facet_config && facet_config.tag
375
+
376
+ solr_filters = values.each_with_object({}).with_index do |(v, h), index|
377
+ h["f_inclusive.#{facet_field}.#{index}"] = facet_value_to_fq_string(facet_field, v, use_local_params: false)
378
+ end
379
+
380
+ filter_query = solr_filters.keys.map do |k|
381
+ "{!query v=$#{k}}"
382
+ end.join(' OR ')
383
+
384
+ ["{!lucene#{(' ' + local_params.join(' ')) unless local_params.empty?}}#{filter_query}", solr_filters]
385
+ end
386
+
278
387
  def convert_to_term_value(value)
279
388
  if value.is_a?(DateTime) || value.is_a?(Time)
280
389
  value.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
@@ -288,7 +397,7 @@ module Blacklight::Solr
288
397
  ##
289
398
  # The key to use to retrieve the grouped field to display
290
399
  def grouped_key_for_results
291
- blacklight_config.index.group
400
+ blacklight_config.view_config(action_name: :index).group
292
401
  end
293
402
 
294
403
  def facet_fields_to_include_in_request
@@ -306,7 +415,7 @@ module Blacklight::Solr
306
415
  def add_search_field_query_builder_params(solr_parameters)
307
416
  q, additional_parameters = search_field.query_builder.call(self, search_field, solr_parameters)
308
417
 
309
- solr_parameters[:q] = q
418
+ solr_parameters.append_query q
310
419
  solr_parameters.merge!(additional_parameters) if additional_parameters
311
420
  end
312
421
 
@@ -314,7 +423,7 @@ module Blacklight::Solr
314
423
  local_params = search_field.solr_local_parameters.map do |key, val|
315
424
  key.to_s + "=" + solr_param_quote(val, quote: "'")
316
425
  end.join(" ")
317
- solr_parameters[:q] = "{!#{local_params}}#{search_state.query_param}"
426
+ solr_parameters.append_query "{!#{local_params}}#{search_state.query_param}"
318
427
 
319
428
  ##
320
429
  # Set Solr spellcheck.q to be original user-entered query, without
@@ -322,20 +431,5 @@ module Blacklight::Solr
322
431
  # params!
323
432
  solr_parameters["spellcheck.q"] ||= search_state.query_param
324
433
  end
325
-
326
- def add_multifield_search_query(solr_parameters)
327
- q = search_state.query_param
328
- solr_parameters[:q] = if q.values.any?(&:blank?)
329
- # if any field parameters are empty, exclude _all_ results
330
- "{!lucene}NOT *:*"
331
- else
332
- "{!lucene}" + q.map do |field, values|
333
- "#{field}:(#{Array(values).map { |x| solr_param_quote(x) }.join(' OR ')})"
334
- end.join(" AND ")
335
- end
336
-
337
- solr_parameters[:defType] = 'lucene'
338
- solr_parameters[:spellcheck] = 'false'
339
- end
340
434
  end
341
435
  end