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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'backend/app/model/solr.rb', line 64

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



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

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



36
37
38
39
# File 'backend/app/model/solr.rb', line 36

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



26
27
28
# File 'backend/app/model/solr.rb', line 26

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

.create_match_all_queryObject



21
22
23
# File 'backend/app/model/solr.rb', line 21

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

.create_term_query(field, term) ⇒ Object



31
32
33
# File 'backend/app/model/solr.rb', line 31

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



222
223
224
225
# File 'backend/app/model/solr.rb', line 222

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

#add_solr_params_from_configObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'backend/app/model/solr.rb', line 80

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



232
233
234
# File 'backend/app/model/solr.rb', line 232

def get_writer_type
  @writer_type
end

#highlighting(yes_please = true) ⇒ Object



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

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

#limit_fields_to(fields) ⇒ Object



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

def limit_fields_to(fields)
  @fields_to_show = fields
  self
end

#page_sizeObject



122
123
124
# File 'backend/app/model/solr.rb', line 122

def page_size
  @pagination[:page_size]
end

#pagination(page, page_size) ⇒ Object



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

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

#remove_csv_headerObject



94
95
96
# File 'backend/app/model/solr.rb', line 94

def remove_csv_header
  @csv_header = false
end

#set_excluded_ids(ids) ⇒ Object



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

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



190
191
192
193
194
195
196
197
198
# File 'backend/app/model/solr.rb', line 190

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

  @facet_mincount = mincount

  self
end

#set_filter(advanced_query) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'backend/app/model/solr.rb', line 165

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



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

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



145
146
147
148
149
150
151
152
# File 'backend/app/model/solr.rb', line 145

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



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

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



136
137
138
139
140
141
142
# File 'backend/app/model/solr.rb', line 136

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



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

def set_solr_url(solr_url)
  @solr_url = solr_url
  self
end

#set_sort(sort) ⇒ Object



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

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

#set_writer_type(type) ⇒ Object



228
229
230
# File 'backend/app/model/solr.rb', line 228

def set_writer_type(type)
  @writer_type = type
end

#show_excluded_docs(value) ⇒ Object



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

def show_excluded_docs(value)
  @show_excluded_docs = value
  self
end

#show_published_only(value) ⇒ Object



212
213
214
215
# File 'backend/app/model/solr.rb', line 212

def show_published_only(value)
  @show_published_only = value
  self
end

#show_suppressed(value) ⇒ Object



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

def show_suppressed(value)
  @show_suppressed = value
  self
end

#to_solr_urlObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'backend/app/model/solr.rb', line 236

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



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

def use_standard_query_type
  @query_type = :standard
  self
end