Class: Solr::Query

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

Constant Summary

SOLR_CHARS =
'+-&|!(){}[]^"~*?:\\/'

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Query) initialize(query_string)

Returns a new instance of Query



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

def initialize(query_string)
  @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
end

Class Method Details

+ (Object) construct_advanced_query_string(advanced_query)



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

def self.construct_advanced_query_string(advanced_query)
  if advanced_query.has_key?('subqueries')
    subqueries = advanced_query['subqueries'].map {|subq|
      construct_advanced_query_string(subq)
    }.join(" #{advanced_query['op']} ")

    "(#{subqueries})"
  else
    prefix = advanced_query['negated'] ? "-" : ""
    field = AdvancedSearch.solr_field_for(advanced_query['field'])

    if advanced_query["jsonmodel_type"] == "date_field_query"
      if advanced_query["comparator"] == "lesser_than"
        value = "[* TO #{advanced_query["value"]}T00:00:00Z-1MILLISECOND]"
      elsif advanced_query["comparator"] == "greater_than"
        value = "[#{advanced_query["value"]}T00:00:00Z+1DAY TO *]"
      else # advanced_query["comparator"] == "equal"
        value = "[#{advanced_query["value"]}T00:00:00Z TO #{advanced_query["value"]}T00:00:00Z+1DAY-1MILLISECOND]"
      end
    elsif advanced_query["jsonmodel_type"] == "field_query" && advanced_query["literal"]
      value = "(\"#{solr_escape(advanced_query['value'])}\")"
    else
      value = "(#{advanced_query['value']})"
    end

    "#{prefix}#{field}:#{value}"
  end
end

+ (Object) create_advanced_search(advanced_query_json)



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

+ (Object) create_keyword_search(query)



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

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

+ (Object) create_match_all_query



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

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

+ (Object) create_term_query(field, term)



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

+ (Object) solr_escape(s)



74
75
76
77
# File 'backend/app/model/solr.rb', line 74

def self.solr_escape(s)
  pattern = Regexp.quote(SOLR_CHARS)
  s.gsub(/([#{pattern}])/, '\\\\\1')
end

Instance Method Details

- (Object) add_solr_param(param, value)



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

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

- (Object) highlighting(yes_please = true)



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

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

- (Object) page_size



120
121
122
# File 'backend/app/model/solr.rb', line 120

def page_size
  @pagination[:page_size]
end

- (Object) pagination(page, page_size)



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

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

- (Object) set_excluded_ids(ids)



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

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

  self
end

- (Object) set_facets(fields)



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

def set_facets(fields)
  if fields
    @facet_fields = fields
  end

  self
end

- (Object) set_filter_terms(filter_terms)



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

def set_filter_terms(filter_terms)
  unless Array(filter_terms).empty?
    filter_terms.map{|str| ASUtils.json_parse(str)}.each{|json|
      json.each {|facet, term|
        add_solr_param(:fq, self.class.term_query(facet.strip, term.to_s.strip))
      }
    }
  end

  self
end

- (Object) set_record_types(record_types)



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

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

- (Object) set_repo_id(repo_id)



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

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

  self
end

- (Object) set_root_record(root_record)



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

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

  self
end

- (Object) set_simple_filters(filter_terms)



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

def set_simple_filters(filter_terms)
  unless Array(filter_terms).empty?
    filter_terms.map{|str| 
      add_solr_param(:fq, str.strip )
    }
  end

  self
end

- (Object) set_solr_url(solr_url)



96
97
98
99
# File 'backend/app/model/solr.rb', line 96

def set_solr_url(solr_url)
  @solr_url = solr_url
  self
end

- (Object) set_sort(sort)



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

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

- (Object) set_writer_type(type)



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

def set_writer_type(type)
  @writer_type = type
end

- (Object) show_excluded_docs(value)



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

def show_excluded_docs(value)
  @show_excluded_docs = value
  self
end

- (Object) show_published_only(value)



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

def show_published_only(value)
  @show_published_only = value
  self
end

- (Object) show_suppressed(value)



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

def show_suppressed(value)
  @show_suppressed = value
  self
end

- (Object) to_solr_url



229
230
231
232
233
234
235
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
# File 'backend/app/model/solr.rb', line 229

def to_solr_url
  raise "Missing pagination settings" unless @pagination

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

  if @show_published_only
    add_solr_param(:fq, "publish:true")
  end


  if @highlighting
    add_solr_param(:hl, "true")
  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])
  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

  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],
                                   [:start, (@pagination[:page] - 1) * @pagination[:page_size]],
                                   [:rows, @pagination[:page_size]]] +
                                  @solr_params)

  url
end

- (Object) use_standard_query_type



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

def use_standard_query_type
  @query_type = :standard
  self
end