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

Returns a new instance of Query.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'backend/app/model/solr.rb', line 144

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
end

Class Method Details

.construct_advanced_query_string(advanced_query, use_literal = false) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'backend/app/model/solr.rb', line 122

def self.construct_advanced_query_string(advanced_query, use_literal = false)
  if advanced_query.has_key?('subqueries')
    clauses = advanced_query['subqueries'].map {|subq|
      construct_advanced_query_string(subq, use_literal)
    }

# This causes incorrect results for X NOT Y queries via the PUI, see https://github.com/archivesspace/archivesspace/issues/1699
#        # Solr doesn't allow purely negative expression groups, so we add a
#        # match all query to compensate when we hit one of these.
#        if advanced_query['subqueries'].all? {|subquery| subquery['negated']}
#          clauses << '*:*'
#        end

    subqueries = clauses.join(" #{advanced_query['op']} ")

    "(#{subqueries})"
  else
    AdvancedQueryString.new(advanced_query, use_literal).to_solr_s
  end
end

.create_advanced_search(advanced_query_json) ⇒ Object



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

def self.create_advanced_search(advanced_query_json)
  new(construct_advanced_query_string(advanced_query_json['query'])).
    use_standard_query_type
end

.create_keyword_search(query) ⇒ Object



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

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

.create_match_all_queryObject



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

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

.create_term_query(field, term) ⇒ Object



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

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

Instance Method Details

#add_solr_param(param, value) ⇒ Object



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

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

#add_solr_params_from_configObject



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'backend/app/model/solr.rb', line 160

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



312
313
314
# File 'backend/app/model/solr.rb', line 312

def get_writer_type
  @writer_type
end

#highlighting(yes_please = true) ⇒ Object



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

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

#limit_fields_to(fields) ⇒ Object



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

def limit_fields_to(fields)
  @fields_to_show = fields
  self
end

#page_sizeObject



202
203
204
# File 'backend/app/model/solr.rb', line 202

def page_size
  @pagination[:page_size]
end

#pagination(page, page_size) ⇒ Object



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

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

#remove_csv_headerObject



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

def remove_csv_header
  @csv_header = false
end

#set_excluded_ids(ids) ⇒ Object



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

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



270
271
272
273
274
275
276
277
278
# File 'backend/app/model/solr.rb', line 270

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

  @facet_mincount = mincount

  self
end

#set_filter(advanced_query) ⇒ Object



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

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



255
256
257
258
259
260
261
# File 'backend/app/model/solr.rb', line 255

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



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

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



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

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



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

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



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

def set_solr_url(solr_url)
  @solr_url = solr_url
  self
end

#set_sort(sort) ⇒ Object



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

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

#set_writer_type(type) ⇒ Object



308
309
310
# File 'backend/app/model/solr.rb', line 308

def set_writer_type(type)
  @writer_type = type
end

#show_excluded_docs(value) ⇒ Object



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

def show_excluded_docs(value)
  @show_excluded_docs = value
  self
end

#show_published_only(value) ⇒ Object



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

def show_published_only(value)
  @show_published_only = value
  self
end

#show_suppressed(value) ⇒ Object



264
265
266
267
# File 'backend/app/model/solr.rb', line 264

def show_suppressed(value)
  @show_suppressed = value
  self
end

#to_solr_urlObject



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
375
376
377
378
379
380
381
# File 'backend/app/model/solr.rb', line 316

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")
    if @query_type == :standard
      add_solr_param(:"hl.fl", "*")
    end
  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")
    add_solr_param(:qf, "four_part_id^3 title^2 finding_aid_filing_title^2 fullrecord")
  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

#use_standard_query_typeObject



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

def use_standard_query_type
  @query_type = :standard
  self
end