Class: Solr::Query

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/solr.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_string, opts = {}) ⇒ Query

The query_string parameter needs to be created before initialization (see above). Possible refactor to have that conversion happen when the instance is rendered into a Solr url.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'backend/app/model/solr.rb', line 138

def initialize(query_string, opts = {})
  @solr_url = URI.parse(AppConfig[:solr_url])

  @query_string = query_string
  @writer_type = "json"
  @query_type = :edismax
  @pagination = nil
  @solr_params = []
  @facet_fields = []
  @highlighting = false

  @show_suppressed = false
  @show_published_only = false
  @csv_header = true
  @protect_unpublished = opts[:protect_unpublished] || false
end

Class Method Details

.construct_advanced_query_string(advanced_query, use_literal: false, protect_unpublished: false) ⇒ Object

AdvancedQueryString maps application’s search options to Solr fields.



123
124
125
126
127
128
129
130
131
132
133
# File 'backend/app/model/solr.rb', line 123

def self.construct_advanced_query_string(advanced_query, use_literal: false, protect_unpublished: false)
  if advanced_query.has_key?('subqueries')
    clauses = advanced_query['subqueries'].map {|subq|
      construct_advanced_query_string(subq, use_literal: use_literal, protect_unpublished: protect_unpublished)
    }
    subqueries = clauses.join(" #{advanced_query['op']} ")
    "(#{subqueries})"
  else
    AdvancedQueryString.new(advanced_query, use_literal: use_literal, protect_unpublished: protect_unpublished).to_solr_s
  end
end

.create_advanced_search(advanced_query_json, protect_unpublished: false) ⇒ Object



115
116
117
118
119
# File 'backend/app/model/solr.rb', line 115

def self.create_advanced_search(advanced_query_json, protect_unpublished: false)
  query = new(construct_advanced_query_string(advanced_query_json['query'], protect_unpublished: protect_unpublished))
  query.protect_unpublished! if protect_unpublished
  query
end

.create_keyword_search(query) ⇒ Object



105
106
107
# File 'backend/app/model/solr.rb', line 105

def self.create_keyword_search(query)
  new(query)
end

.create_match_all_queryObject



100
101
102
# File 'backend/app/model/solr.rb', line 100

def self.create_match_all_query
  new("*:*")
end

.create_term_query(field, term) ⇒ Object



110
111
112
# File 'backend/app/model/solr.rb', line 110

def self.create_term_query(field, term)
  new(term_query(field, term))
end

Instance Method Details

#add_solr_param(param, value) ⇒ Object



291
292
293
294
# File 'backend/app/model/solr.rb', line 291

def add_solr_param(param, value)
  @solr_params << [param, value]
  self
end

#add_solr_params_from_configObject



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'backend/app/model/solr.rb', line 155

def add_solr_params_from_config
  if AppConfig[:solr_params].any?
    AppConfig[:solr_params].each do |param, value|
      if value.is_a? Array
        value.each do |v|
          add_solr_param(param, v.respond_to?(:call) ? self.instance_eval(&v) : v)
        end
      else
        add_solr_param(param, value.respond_to?(:call) ? self.instance_eval(&value) : value)
      end
    end
  end
end

#get_writer_typeObject



301
302
303
# File 'backend/app/model/solr.rb', line 301

def get_writer_type
  @writer_type
end

#highlighting(yes_please = true) ⇒ Object



179
180
181
182
# File 'backend/app/model/solr.rb', line 179

def highlighting(yes_please = true)
  @highlighting = yes_please
  self
end

#limit_fields_to(fields) ⇒ Object



286
287
288
289
# File 'backend/app/model/solr.rb', line 286

def limit_fields_to(fields)
  @fields_to_show = fields
  self
end

#page_sizeObject



191
192
193
# File 'backend/app/model/solr.rb', line 191

def page_size
  @pagination[:page_size]
end

#pagination(page, page_size) ⇒ Object



185
186
187
188
# File 'backend/app/model/solr.rb', line 185

def pagination(page, page_size)
  @pagination = {:page => page, :page_size => page_size}
  self
end

#protect_unpublished!Object



377
378
379
# File 'backend/app/model/solr.rb', line 377

def protect_unpublished!
  @protect_unpublished = true
end

#remove_csv_headerObject



169
170
171
# File 'backend/app/model/solr.rb', line 169

def remove_csv_header
  @csv_header = false
end

#set_excluded_ids(ids) ⇒ Object



224
225
226
227
228
229
230
231
# File 'backend/app/model/solr.rb', line 224

def set_excluded_ids(ids)
  if ids
    query = ids.map { |id| "\"#{id}\"" }.join(' OR ')
    add_solr_param(:fq, "-id:(#{query})")
  end

  self
end

#set_facets(fields, mincount = 0) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'backend/app/model/solr.rb', line 259

def set_facets(fields, mincount = 0)
  if fields
    @facet_fields = fields
  end

  @facet_mincount = mincount

  self
end

#set_filter(advanced_query) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'backend/app/model/solr.rb', line 234

def set_filter(advanced_query)
  if advanced_query
    query_string = self.class.construct_advanced_query_string(advanced_query['query'],
                                                              use_literal: true)
    add_solr_param(:fq, query_string)
  end

  self
end

#set_filter_queries(queries) ⇒ Object



244
245
246
247
248
249
250
# File 'backend/app/model/solr.rb', line 244

def set_filter_queries(queries)
  ASUtils.wrap(queries).each do |q|
    add_solr_param(:fq, "{!type=edismax}#{q}")
  end

  self
end

#set_record_types(record_types) ⇒ Object



214
215
216
217
218
219
220
221
# File 'backend/app/model/solr.rb', line 214

def set_record_types(record_types)
  if record_types
    query = Array(record_types).map { |type| "\"#{type}\"" }.join(' OR ')
    add_solr_param(:fq, "types:(#{query})")
  end

  self
end

#set_repo_id(repo_id) ⇒ Object



196
197
198
199
200
201
202
# File 'backend/app/model/solr.rb', line 196

def set_repo_id(repo_id)
  if repo_id
    add_solr_param(:fq, "repository:\"/repositories/#{repo_id}\" OR repository:global")
  end

  self
end

#set_root_record(root_record) ⇒ Object



205
206
207
208
209
210
211
# File 'backend/app/model/solr.rb', line 205

def set_root_record(root_record)
  if root_record
    add_solr_param(:fq, "(resource:\"#{root_record}\" OR digital_object:\"#{root_record}\")")
  end

  self
end

#set_solr_url(solr_url) ⇒ Object



173
174
175
176
# File 'backend/app/model/solr.rb', line 173

def set_solr_url(solr_url)
  @solr_url = solr_url
  self
end

#set_sort(sort) ⇒ Object



270
271
272
# File 'backend/app/model/solr.rb', line 270

def set_sort(sort)
  add_solr_param(:sort, sort)
end

#set_writer_type(type) ⇒ Object



297
298
299
# File 'backend/app/model/solr.rb', line 297

def set_writer_type(type)
  @writer_type = type
end

#show_excluded_docs(value) ⇒ Object



275
276
277
278
# File 'backend/app/model/solr.rb', line 275

def show_excluded_docs(value)
  @show_excluded_docs = value
  self
end

#show_published_only(value) ⇒ Object



281
282
283
284
# File 'backend/app/model/solr.rb', line 281

def show_published_only(value)
  @show_published_only = value
  self
end

#show_suppressed(value) ⇒ Object



253
254
255
256
# File 'backend/app/model/solr.rb', line 253

def show_suppressed(value)
  @show_suppressed = value
  self
end

#to_solr_urlObject



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'backend/app/model/solr.rb', line 305

def to_solr_url
  raise "Missing pagination settings" unless @pagination

  if @fields_to_show
    add_solr_param(:fl, @fields_to_show.join(', '))
  end

  unless @show_excluded_docs
    add_solr_param(:fq, "-exclude_by_default:true")
  end

  if @show_published_only
    # public ui
    add_solr_param(:fq, "publish:true")
    add_solr_param(:fq, "types:pui")
  else
    # staff ui
    add_solr_param(:fq, "-types:pui_only")
  end


  if @highlighting
    add_solr_param(:hl, "true")
    add_solr_param(:"hl.fl", "*")
    add_solr_param(:"hl.simple.pre", '<span class="searchterm">')
    add_solr_param(:"hl.simple.post", "</span>")
  end

  unless @show_suppressed
    add_solr_param(:fq, "suppressed:false")
  end

  add_solr_param(:facet, "true")
  unless @facet_fields.empty?
    add_solr_param(:"facet.field", @facet_fields)
    add_solr_param(:"facet.limit", AppConfig[:solr_facet_limit])
    add_solr_param(:"facet.mincount", @facet_mincount)
  end

  if @query_type == :edismax
    add_solr_param(:defType, "edismax")
    add_solr_param(:pf, "four_part_id^4")
    if @protect_unpublished
      add_solr_param(:qf, "identifier_ws^3 title_ws^2 finding_aid_filing_title^2 fullrecord_published")
    else
      add_solr_param(:qf, "identifier_ws^3 title_ws^2 finding_aid_filing_title^2 fullrecord")
    end
  end

  # do it here so instance variables can be resolved
  add_solr_params_from_config

  Solr.search_hooks.each do |hook|
    hook.call(self)
  end

  url = @solr_url
  # retain path if present i.e. "solr/aspace/select" when using an external Solr with path required
  url.path += "/select"
  url.query = URI.encode_www_form([[:q, @query_string],
                                   [:wt, @writer_type],
                                   ["csv.escape", '\\'],
                                   ["csv.encapsulator", '"'],
                                   ["csv.header", @csv_header ],
                                   [:start, (@pagination[:page] - 1) * @pagination[:page_size]],
                                   [:rows, @pagination[:page_size]]] +
                                  @solr_params)

  url
end