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) ⇒ SearchResultData

Returns a new instance of SearchResultData.



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

def initialize(search_data)
  @search_data = search_data
  @facet_data = {}

  self.class.run_result_hooks(search_data)
  init_facets
  init_sorts
end

Class Method Details

.ACCESSION_FACETSObject



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

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

.add_result_hook(&block) ⇒ Object



383
384
385
386
# File 'frontend/app/models/search_result_data.rb', line 383

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

.AGENT_FACETSObject



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

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



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

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

.ASSESSMENT_FACETSObject



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

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



306
307
308
# File 'frontend/app/models/search_result_data.rb', line 306

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

.BASE_SORT_FIELDSObject



302
303
304
# File 'frontend/app/models/search_result_data.rb', line 302

def self.BASE_SORT_FIELDS
  %w(create_time user_mtime)
end

.CLASSIFICATION_FACETSObject



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

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

.CONTAINER_PROFILE_FACETSObject



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

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



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

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

.EVENT_FACETSObject



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

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

.facets_for(record_type) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'frontend/app/models/search_result_data.rb', line 368

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



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

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

.LOCATION_FACETSObject



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

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



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

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

.run_result_hooks(results) ⇒ Object



389
390
391
392
393
394
# File 'frontend/app/models/search_result_data.rb', line 389

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

.SUBJECT_FACETSObject



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

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

.TOP_CONTAINER_FACETSObject



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

def self.TOP_CONTAINER_FACETS
  []
end

.UNTITLED_TYPESObject



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

def self.UNTITLED_TYPES
  ["event"]
end

Instance Method Details

#[](key) ⇒ Object



61
62
63
# File 'frontend/app/models/search_result_data.rb', line 61

def [](key)
  @search_data[key]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  @search_data[key] = value
end

#add_sort_field(field, label = nil) ⇒ Object



284
285
286
287
# File 'frontend/app/models/search_result_data.rb', line 284

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



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

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

  @sort_data[index][:direction]
end

#facet_display_string(facet_group, facet) ⇒ Object



110
111
112
# File 'frontend/app/models/search_result_data.rb', line 110

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



73
74
75
76
77
78
79
80
81
82
83
# File 'frontend/app/models/search_result_data.rb', line 73

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



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

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



114
115
116
117
118
119
120
121
122
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
# File 'frontend/app/models/search_result_data.rb', line 114

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



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

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

#facets_for_filterObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'frontend/app/models/search_result_data.rb', line 85

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)


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

def filtered_terms?
  @search_data[:criteria].has_key?("filter_term[]") and @search_data[:criteria]["filter_term[]"].reject {|f| f.empty?}.length > 0
end

#get_typeObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'frontend/app/models/search_result_data.rb', line 206

def get_type
  type = 'multi'
  if @search_data[:type]
    type = @search_data[:type]
  elsif (@search_data[:criteria]["type[]"] || []).length == 1
    type = @search_data[:criteria]["type[]"][0]
  elsif (types = @search_data[: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 = @search_data[: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)


198
199
200
201
202
203
204
# File 'frontend/app/models/search_result_data.rb', line 198

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

#init_facetsObject



13
14
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
# File 'frontend/app/models/search_result_data.rb', line 13

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 (@search_data[:criteria].has_key?('q'))
          query = @search_data[: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



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

def init_sorts
  if sorted?
    @sort_data = @search_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)


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

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

#results?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'frontend/app/models/search_result_data.rb', line 194

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

#sort_facets(facet_group, facets) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'frontend/app/models/search_result_data.rb', line 99

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



280
281
282
# File 'frontend/app/models/search_result_data.rb', line 280

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



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

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)


231
232
233
# File 'frontend/app/models/search_result_data.rb', line 231

def sorted?
  @search_data[:criteria]["sort"]
end

#sorted_by(index = 0) ⇒ Object



235
236
237
238
239
240
241
# File 'frontend/app/models/search_result_data.rb', line 235

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

#sorted_by?(field) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  false
end

#sorted_by_label(index = 0) ⇒ Object



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

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



227
228
229
# File 'frontend/app/models/search_result_data.rb', line 227

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