Class: SearchResultData

Inherits:
Object
  • Object
show all
Defined in:
frontend/app/models/search_result_data.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_data, criteria, context_criteria = {}) ⇒ SearchResultData

Returns a new instance of SearchResultData.



3
4
5
6
7
8
9
10
11
12
# File 'frontend/app/models/search_result_data.rb', line 3

def initialize(search_data, criteria, context_criteria = {})
  @search_data = search_data
  @criteria = criteria
  @context_criteria = context_criteria
  @facet_data = {}

  self.class.run_result_hooks(search_data)
  init_facets
  init_sorts
end

Class Method Details

.ACCESSION_FACETSObject



325
326
327
# File 'frontend/app/models/search_result_data.rb', line 325

def self.ACCESSION_FACETS
  ["subjects", "accession_date_year", "creators"] + Plugins.search_facets_for_type(:accession)
end

.add_result_hook(&block) ⇒ Object



392
393
394
395
# File 'frontend/app/models/search_result_data.rb', line 392

def self.add_result_hook(&block)
  @result_hooks ||= []
  @result_hooks << block
end

.AGENT_FACETSObject



319
320
321
322
323
# File 'frontend/app/models/search_result_data.rb', line 319

def self.AGENT_FACETS
  extras = [:agent_person, :agent_family, :agent_corporate_entity, :agent_software]
             .flat_map {|agent_type| Plugins.search_facets_for_type(agent_type)}
  ["primary_type", "source", "rules"] + extras
end

.ARCHIVAL_OBJECT_FACETSObject



333
334
335
# File 'frontend/app/models/search_result_data.rb', line 333

def self.ARCHIVAL_OBJECT_FACETS
  ["subjects", "publish", "level", "classification_path", "primary_type"] + Plugins.search_facets_for_type(:archival_object)
end

.ASSESSMENT_FACETSObject



369
370
371
# File 'frontend/app/models/search_result_data.rb', line 369

def self.ASSESSMENT_FACETS
  ['assessment_record_types', 'assessment_surveyors', 'assessment_review_required', 'assessment_reviewers', 'assessment_completed', 'assessment_inactive', 'assessment_survey_year', 'assessment_sensitive_material'] + Plugins.search_facets_for_type(:assessment)
end

.BASE_FACETSObject



315
316
317
# File 'frontend/app/models/search_result_data.rb', line 315

def self.BASE_FACETS
  ["primary_type", "creators", "subjects", "langcode"] + Plugins.search_facets_for_base
end

.BASE_SORT_FIELDSObject



311
312
313
# File 'frontend/app/models/search_result_data.rb', line 311

def self.BASE_SORT_FIELDS
  %w(create_time user_mtime)
end

.CLASSIFICATION_FACETSObject



361
362
363
# File 'frontend/app/models/search_result_data.rb', line 361

def self.CLASSIFICATION_FACETS
  [] + Plugins.search_facets_for_type(:classification)
end

.CONTAINER_PROFILE_FACETSObject



341
342
343
# File 'frontend/app/models/search_result_data.rb', line 341

def self.CONTAINER_PROFILE_FACETS
  ["container_profile_width_u_sstr", "container_profile_height_u_sstr", "container_profile_depth_u_sstr", "container_profile_dimension_units_u_sstr"] + Plugins.search_facets_for_type(:container_profile)
end

.DIGITAL_OBJECT_FACETSObject



337
338
339
# File 'frontend/app/models/search_result_data.rb', line 337

def self.DIGITAL_OBJECT_FACETS
  ["subjects", "publish", "digital_object_type", "level", "primary_type", "langcode"] + Plugins.search_facets_for_type(:digital_object)
end

.EVENT_FACETSObject



353
354
355
# File 'frontend/app/models/search_result_data.rb', line 353

def self.EVENT_FACETS
  ["event_type", "outcome"] + Plugins.search_facets_for_type(:event)
end

.facets_for(record_type) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'frontend/app/models/search_result_data.rb', line 377

def self.facets_for(record_type)
  if record_type.include? 'agent'
    record_type = 'agent'
  elsif record_type == 'archival_object'
    record_type = 'resource'
  elsif record_type == 'digital_object_component'
    record_type = 'digital_object'
  end
  begin
    self.send("#{record_type.upcase}_FACETS")
  rescue
    self.BASE_FACETS
  end
end

.JOB_FACETSObject



373
374
375
# File 'frontend/app/models/search_result_data.rb', line 373

def self.JOB_FACETS
  ["status", "job_type", "report_type", "owner"]
end

.LOCATION_FACETSObject



345
346
347
# File 'frontend/app/models/search_result_data.rb', line 345

def self.LOCATION_FACETS
  ["temporary", "owner_repo_display_string_u_ssort", "building", "floor", "room", "area", "location_profile_display_string_u_ssort"] + Plugins.search_facets_for_type(:location)
end

.RESOURCE_FACETSObject



329
330
331
# File 'frontend/app/models/search_result_data.rb', line 329

def self.RESOURCE_FACETS
  ["subjects", "publish", "level", "classification_path", "primary_type", "langcode"] + Plugins.search_facets_for_type(:resource)
end

.run_result_hooks(results) ⇒ Object



398
399
400
401
402
403
# File 'frontend/app/models/search_result_data.rb', line 398

def self.run_result_hooks(results)
  @result_hooks ||= []
  Array(@result_hooks).each do |hook|
    hook.call(results)
  end
end

.SUBJECT_FACETSObject



349
350
351
# File 'frontend/app/models/search_result_data.rb', line 349

def self.SUBJECT_FACETS
  ["source", "first_term_type"] + Plugins.search_facets_for_type(:subject)
end

.TOP_CONTAINER_FACETSObject



365
366
367
# File 'frontend/app/models/search_result_data.rb', line 365

def self.TOP_CONTAINER_FACETS
  []
end

.UNTITLED_TYPESObject



357
358
359
# File 'frontend/app/models/search_result_data.rb', line 357

def self.UNTITLED_TYPES
  ["event"]
end

Instance Method Details

#[](key) ⇒ Object



63
64
65
66
# File 'frontend/app/models/search_result_data.rb', line 63

def [](key)
  return @criteria if key.to_s == 'criteria'
  @search_data[key]
end

#[]=(key, value) ⇒ Object



68
69
70
71
# File 'frontend/app/models/search_result_data.rb', line 68

def []=(key, value)
  raise "Can't modify criteria" if key.to_s == 'criteria'
  @search_data[key] = value
end

#add_sort_field(field, label = nil) ⇒ Object



293
294
295
296
# File 'frontend/app/models/search_result_data.rb', line 293

def add_sort_field(field, label =nil)
  @sort_fields ||= self.class.BASE_SORT_FIELDS.collect {|f| [f, I18n.t("search.multi.#{f}")]}.to_h
  @sort_fields[field] = label || I18n.t("search.multi.#{field}")
end

#current_sort_direction(index = 0) ⇒ Object



262
263
264
265
266
# File 'frontend/app/models/search_result_data.rb', line 262

def current_sort_direction(index = 0)
  return "desc" unless sorted?

  @sort_data[index][:direction]
end

#facet_display_string(facet_group, facet) ⇒ Object



119
120
121
# File 'frontend/app/models/search_result_data.rb', line 119

def facet_display_string(facet_group, facet)
  "#{I18n.t("search.#{get_type}.#{facet_group}", :default => I18n.t("search.multi.#{facet_group}", :default => facet_group))}: #{facet_label_string(facet_group, facet)}"
end

#facet_label_for_filter(filter) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'frontend/app/models/search_result_data.rb', line 82

def facet_label_for_filter(filter)
  filter_json = ASUtils.json_parse(filter)
  facet = filter_json.keys[0]
  term = filter_json[facet]

  if @facet_data.has_key?(facet) and @facet_data[facet].has_key?(term)
    @facet_data[facet][term][:display_string]
  else
    facet_display_string(facet, term)
  end
end

#facet_label_for_query(query) ⇒ Object



302
303
304
305
306
307
308
309
# File 'frontend/app/models/search_result_data.rb', line 302

def facet_label_for_query(query)
  if (query.match(/\-.+\:\*/))
    value = query.tr("-:*", "")
    "#{I18n.t("search.blank_facet_query_fields."+value)+": None"}"
  else
    "#{I18n.t("search.multi.query")}: #{query}"
  end
end

#facet_label_string(facet_group, facet) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'frontend/app/models/search_result_data.rb', line 123

def facet_label_string(facet_group, facet)
  # Plugins can opt to tell us how facet values should be translated.
  if plugin_key = Plugins.facet_i18n_key(facet_group, facet)
    return I18n.t(plugin_key, :default => facet)
  end

  return I18n.t("search.location.none") if facet == "none"
  return facet.upcase if facet_group == "owner_repo_display_string_u_ssort"
  return I18n.t("#{facet}._singular", :default => I18n.t("plugins.#{facet}._singular", :default => facet)) if facet_group === "primary_type"
  return I18n.t("enumerations.name_source.#{facet}", :default => I18n.t("enumerations.subject_source.#{facet}", :default => facet)) if facet_group === "source"
  return I18n.t("enumerations.name_rule.#{facet}", :default => facet) if facet_group === "rules"
  return I18n.t("boolean.#{facet.to_s}", :default => facet) if facet_group === "publish"
  return I18n.t("enumerations.digital_object_digital_object_type.#{facet.to_s}", :default => facet) if facet_group === "digital_object_type"
  return I18n.t("enumerations.location_temporary.#{facet.to_s}", :default => facet) if facet_group === "temporary"
  return I18n.t("enumerations.event_event_type.#{facet.to_s}", :default => facet) if facet_group === "event_type"
  return I18n.t("enumerations.event_outcome.#{facet.to_s}", :default => facet) if facet_group === "outcome"
  return I18n.t("enumerations.subject_term_type.#{facet.to_s}", :default => facet) if facet_group === "first_term_type"

  return I18n.t("enumerations.language_iso639_2.#{facet}", :default => facet) if facet_group === "langcode"

  if facet_group === "source"
    if get_type.include? "subject"
      return I18n.t("enumerations.subject_source.#{facet}", :default => facet)
    else
      return I18n.t("enumerations.name_source.#{facet}", :default => facet)
    end
  end

  if facet_group === "level"
    if get_type.include? "digital_object"
      return I18n.t("enumerations.digital_object_level.#{facet.to_s}", :default => facet)
    else
      return I18n.t("enumerations.archival_record_level.#{facet.to_s}", :default => facet)
    end
  end

  # labels for collection management groups
  return I18n.t("#{facet}._singular", :default => facet) if facet_group === "parent_type"
  return I18n.t("enumerations.collection_management_processing_priority.#{facet}", :default => facet) if facet_group === "processing_priority"
  return I18n.t("enumerations.collection_management_processing_status.#{facet}", :default => facet) if facet_group === "processing_status"

  if facet_group === "classification_path"
    return ClassificationHelper.format_classification(ASUtils.json_parse(facet))
  end

  if facet_group === "assessment_review_required"
    return I18n.t("assessment._frontend.assessment_review_required.#{facet}_value")
  end

  if facet_group === "assessment_sensitive_material"
    return I18n.t("assessment._frontend.assessment_sensitive_material.#{facet}_value")
  end

  if facet_group === "assessment_inactive"
    return I18n.t("assessment._frontend.assessment_inactive.#{facet}_value")
  end

  if facet_group === "assessment_record_types"
    return I18n.t("#{facet}._singular", :default => facet)
  end

  if facet_group === "assessment_completed"
    return I18n.t("assessment._frontend.assessment_completed.#{facet}_value")
  end

  if facet_group === "status"
    return I18n.t("job.status_#{facet}", :default => facet)
  end

  if facet_group === "job_type"
    return I18n.t("job.types.#{facet}", :default => facet)
  end

  if facet_group === "report_type"
    return I18n.t("reports.#{facet}.title", :default => facet)
  end

  facet
end

#facet_query_string(facet_group, facet) ⇒ Object



56
57
58
59
60
61
# File 'frontend/app/models/search_result_data.rb', line 56

def facet_query_string(facet_group, facet)
  if (facet == "none")
    return "-" + facet_group + ":*"
  end
  {facet_group => facet}.to_json
end

#facets_for_filterObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'frontend/app/models/search_result_data.rb', line 94

def facets_for_filter
  facet_data_for_filter = @facet_data.clone
  facet_data_for_filter.each {|facet_group, facets|
    facets.delete_if {|facet, facet_map|
      facet_map[:count] === @search_data['total_hits']
    }
  }
  facet_data_for_filter.delete_if {|facet_group, facets| facets.empty?}
  facet_data_for_filter.each do |facet_group, facets|
    facet_data_for_filter[facet_group] = sort_facets(facet_group, facets)
  end
  facet_data_for_filter
end

#filtered_terms?Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'frontend/app/models/search_result_data.rb', line 73

def filtered_terms?
  return false unless @criteria["filter_term[]"]
  user_filter_terms.reject {|f| f.empty?}.length > 0
end

#get_typeObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'frontend/app/models/search_result_data.rb', line 215

def get_type
  type = 'multi'
  if @search_data[:type]
    type = @search_data[:type]
  elsif (@criteria["type[]"] || []).length == 1
    type = @criteria["type[]"][0]
  elsif (types = @criteria["type[]"] || []).length == 2
    if types.include?('resource') && types.include?('archival_object')
      type = 'resource'
    elsif types.include?('digital_object') && types.include?('digital_object_component')
      type = 'digital_object'
    end
  elsif terms = @criteria['filter_term[]']
    types = terms.collect { |term| ASUtils.json_parse(term)['primary_type'] }.compact
    type = types[0] if types.length == 1
  end
  type = 'agent' if type.include? 'agent'
  type = 'repositories' if type == 'repository'
  type
end

#has_titles?Boolean

Returns:

  • (Boolean)


207
208
209
210
211
212
213
# File 'frontend/app/models/search_result_data.rb', line 207

def has_titles?
  if @criteria.has_key?("type[]") and (types - self.class.UNTITLED_TYPES).empty?
    false
  else
    true
  end
end

#init_facetsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'frontend/app/models/search_result_data.rb', line 15

def init_facets
  @search_data['facets']['facet_fields'].each {|facet_group, facets|
    @facet_data[facet_group] = {}
    facets.each_slice(2).each {|facet_and_count|
      next if facet_and_count[1] === 0


      if (facet_and_count[0] == "none")
        query = facet_query_string(facet_group, facet_and_count[0])
        if (@criteria.has_key?('q'))
          query = @criteria['q'] + ' AND ' + query
        end
        @facet_data[facet_group][facet_and_count[0]] = {
            :label => facet_label_string(facet_group, facet_and_count[0]),
            :count => facet_and_count[1],
            :q => query,
            :display_string => facet_display_string(facet_group, facet_and_count[0])
          }
      else
        @facet_data[facet_group][facet_and_count[0]] = {
            :label => facet_label_string(facet_group, facet_and_count[0]),
            :count => facet_and_count[1],
            :filter_term => facet_query_string(facet_group, facet_and_count[0]),
            :display_string => facet_display_string(facet_group, facet_and_count[0])
          }
      end
    }
  }
end

#init_sortsObject



46
47
48
49
50
51
52
53
# File 'frontend/app/models/search_result_data.rb', line 46

def init_sorts
  if sorted?
    @sort_data = @criteria["sort"].split(", ").map {|s|
      matches = s.match(/(\S+)\s(asc|desc)/)
      {:field => matches[1], :direction => matches[2]}
    }
  end
end

#query?Boolean

Returns:

  • (Boolean)


298
299
300
# File 'frontend/app/models/search_result_data.rb', line 298

def query?
  not @criteria["q"].blank?
end

#results?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'frontend/app/models/search_result_data.rb', line 203

def results?
  @search_data.has_key?('results') and not @search_data['results'].empty?
end

#sort_facets(facet_group, facets) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'frontend/app/models/search_result_data.rb', line 108

def sort_facets(facet_group, facets)
  case facet_group
  when 'accession_date_year'
    f = facets.sort { |a, b| (b[0].to_i <=> a[0].to_i) * (AppConfig[:sort_accession_date_filter_asc] ? -1 : 1) }.to_h
    f['9999'][:label] = I18n.t("accession.accession_date_unknown") if f['9999']
    f
  else
    facets
  end
end

#sort_fieldsObject



289
290
291
# File 'frontend/app/models/search_result_data.rb', line 289

def sort_fields
  @sort_fields ||= self.class.BASE_SORT_FIELDS.collect {|f| [f, I18n.t("search.multi.#{f}")]}.to_h
end

#sort_filter_for(field, default = "asc") ⇒ Object



269
270
271
272
273
274
275
# File 'frontend/app/models/search_result_data.rb', line 269

def sort_filter_for(field, default = "asc")
  return "#{field} #{default}" if field != sorted_by

  return "" if current_sort_direction != default

  return "#{field} #{default === "asc" ? "desc" : "asc"}"
end

#sorted?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'frontend/app/models/search_result_data.rb', line 240

def sorted?
  @criteria["sort"]
end

#sorted_by(index = 0) ⇒ Object



244
245
246
247
248
249
250
# File 'frontend/app/models/search_result_data.rb', line 244

def sorted_by(index = 0)
  if sorted? && @sort_data[index]
    @sort_data[index][:field]
  else
    nil
  end
end

#sorted_by?(field) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
256
257
258
259
# File 'frontend/app/models/search_result_data.rb', line 253

def sorted_by?(field)
  @sort_data.each do |entry|
    return true if entry[:field] == field
  end

  false
end

#sorted_by_label(index = 0) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
# File 'frontend/app/models/search_result_data.rb', line 277

def sorted_by_label(index = 0)
  _sorted_by = sorted_by(index)

  return I18n.t("search_sorting.select") if _sorted_by.nil?

  _sorted_by = 'title_sort' if sorted_by == 'title'

  label = sort_fields[_sorted_by] || I18n.t("search.multi.#{_sorted_by}")
  direction = sorted_by == 'score' ? '' : I18n.t("search_sorting.#{current_sort_direction(index)}")
  "#{label} #{direction}".strip
end

#typesObject



236
237
238
# File 'frontend/app/models/search_result_data.rb', line 236

def types
  @criteria["type[]"]
end

#user_filter_termsObject



78
79
80
# File 'frontend/app/models/search_result_data.rb', line 78

def user_filter_terms
  @criteria["filter_term[]"] - (@context_criteria["filter_term[]"] || [])
end