Class: IndexerCommon

Inherits:
Object
  • Object
show all
Defined in:
indexer/app/lib/indexer_common.rb

Direct Known Subclasses

PeriodicIndexer, RealtimeIndexer

Constant Summary collapse

@@record_types =
IndexerCommonConfig.record_types
@@global_types =
IndexerCommonConfig.global_types
@@records_with_children =
[]
@@init_hooks =
[]
@@resolved_attributes =
IndexerCommonConfig.resolved_attributes
@@paused_until =
Time.now

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend_url) ⇒ IndexerCommon

Returns a new instance of IndexerCommon.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'indexer/app/lib/indexer_common.rb', line 61

def initialize(backend_url)
  @backend_url = backend_url
  @document_prepare_hooks = []
  @extra_documents_hooks = []
  @delete_hooks = []
  @batch_hooks = []
  @current_session = nil

  while true
    begin
      JSONModel::init(:client_mode => true, :url => @backend_url)
      break
    rescue
      Log.error("Connection to backend failed (#{$!}).  Retrying...")
      sleep(5)
    end
  end

  # Force load up front
  self.enum_fields

  configure_doc_rules

  @@init_hooks.each do |hook|
    hook.call(self)
  end
end

Class Method Details

.add_attribute_to_resolve(attr) ⇒ Object



38
39
40
# File 'indexer/app/lib/indexer_common.rb', line 38

def self.add_attribute_to_resolve(attr)
  @@resolved_attributes.push(attr) unless @@resolved_attributes.include?(attr)
end

.add_indexer_initialize_hook(&block) ⇒ Object



34
35
36
# File 'indexer/app/lib/indexer_common.rb', line 34

def self.add_indexer_initialize_hook(&block)
  @@init_hooks << block
end

.extract_string_values(doc, *opts) ⇒ Object



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
# File 'indexer/app/lib/indexer_common.rb', line 124

def self.extract_string_values(doc, *opts)
  return doc, doc if doc.is_a?(String)

  if doc.is_a?(Array)
    published_queue = doc.flatten
  else
    published_queue = [doc]
  end

  extract_unpublished = !opts.include?(:published_only)

  unpublished_queue = []
  published_strings = []
  unpublished_strings = []
  published_done = false

  [
    [published_queue, published_strings],
    [unpublished_queue, unpublished_strings]
  ].each do |queue, strings|

    while !queue.empty?
      doc = queue.pop

      if (!published_done && doc.has_key?("publish") && !doc["publish"])
        if extract_unpublished
          unpublished_queue.push(doc)
        end
        next
      end

      doc.each do |key, val|
        if IndexerCommonConfig.fullrecord_excludes.include?(key) || key =~ /_enum_s$/
          next # ignored
        elsif val.is_a?(String)
          strings.push(val)
        elsif val.is_a?(Hash)
          queue.push(val)
        elsif val.is_a?(Array)
          val.flatten.each do |v|
            if v.is_a?(String)
              strings.push(v)
            elsif v.is_a?(Hash)
              queue.push(v)
            end
          end
        end
      end
    end

    if extract_unpublished
      published_done = true
    else
      break
    end

  end

  if extract_unpublished
    return published_strings, unpublished_strings
  else
    return published_strings
  end
end

.generate_permutations_for_identifier(identifer) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'indexer/app/lib/indexer_common.rb', line 105

def self.generate_permutations_for_identifier(identifer)
  return [] if identifer.nil?

  [
    identifer,
    identifer.gsub(/[[:punct:]]+/, " "),
    identifer.gsub(/[[:punct:] ]+/, ""),
    identifer.scan(/([0-9]+|[^0-9]+)/).flatten(1).join(" ")
  ].uniq
end

.generate_sort_string_for_identifier(identifier, size = 255) ⇒ Object

Isolate leading alpha and numeric values to create a sortable string



118
119
120
121
# File 'indexer/app/lib/indexer_common.rb', line 118

def self.generate_sort_string_for_identifier(identifier, size = 255)
  letters, numbers, rest = identifier.scan(/([^0-9]*)([0-9]*)(.*)/)[0]
  letters.strip.ljust(size).gsub(' ', '#') + numbers.strip.rjust(size).gsub(' ', '0') + rest.strip.ljust(size)
end

.generate_years_for_date_range(begin_date, end_date) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'indexer/app/lib/indexer_common.rb', line 89

def self.generate_years_for_date_range(begin_date, end_date)
  return [] unless begin_date

  end_date ||= begin_date

  b = begin_date.scan(/\A[0-9]{1,4}/).first
  e = end_date.scan(/\A[0-9]{1,4}/).first

  if b && e
    (b .. e).to_a
  else
    []
  end
end

.pause(duration = 900) ⇒ Object

This is to pause the indexer. Duration is given in seconds.



52
53
54
# File 'indexer/app/lib/indexer_common.rb', line 52

def self.pause(duration = 900 )
  @@paused_until = Time.now + duration
end

.paused?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'indexer/app/lib/indexer_common.rb', line 56

def self.paused?
  @@paused_until > Time.now
end

Instance Method Details

#add_agents(doc, record) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'indexer/app/lib/indexer_common.rb', line 196

def add_agents(doc, record)
  if record['record']['linked_agents']
    # index all linked agents first
    doc['agents'] = record['record']['linked_agents'].collect{|link| link['_resolved']['display_name']['sort_name']}
    doc['agent_uris'] = record['record']['linked_agents'].collect{|link| link['ref']}

    # only published agents
    doc['published_agents'] = []
    doc['published_agent_uris'] = []
    record['record']['linked_agents'].each do |link|
      if link['_resolved']['publish']
        doc['published_agents'] << link['_resolved']['display_name']['sort_name']
        doc['published_agent_uris'] << link['ref']
      end
    end

    # index the creators only
    creators = record['record']['linked_agents'].select{|link| link['role'] === 'creator'}
    doc['creators'] = creators.collect{|link| link['_resolved']['display_name']['sort_name']} if not creators.empty?

    # make a special sort field for each agent
    # creator > subject > source
    seen = {}
    record['record']['linked_agents'].each do |link|
      if seen[link['ref']] == 'creator'
        # do nothing
      elsif seen[link['ref']] == 'subject' && link['role'] != 'creator'
        # do nothing
      else
        relator_label = link['relator'] ? I18n.t("enumerations.linked_agent_archival_record_relators.#{link['relator']}") : ''

        doc["#{link['ref'].gsub(/\//, '_')}_relator_sort"] = "#{link['role']} #{relator_label}"
        seen[link['ref']] = link['role']
      end
    end
  end
end

#add_arks(doc, record) ⇒ Object



365
366
367
368
369
370
371
# File 'indexer/app/lib/indexer_common.rb', line 365

def add_arks(doc, record)
  return unless AppConfig[:arks_enabled]

  if arks = record['record']['ark_name']
    doc['ark_name'] = ([arks.fetch('current', nil)] + arks.fetch('previous')).compact.map {|s| trim_ark_value(s)}
  end
end

#add_audit_info(doc, record) ⇒ Object



257
258
259
260
261
# File 'indexer/app/lib/indexer_common.rb', line 257

def add_audit_info(doc, record)
  ['created_by', 'last_modified_by', 'user_mtime', 'system_mtime', 'create_time'].each do |f|
    doc[f] = record['record'][f] if record['record'].has_key?(f)
  end
end

#add_batch_hook(&block) ⇒ Object



1032
1033
1034
# File 'indexer/app/lib/indexer_common.rb', line 1032

def add_batch_hook(&block)
  @batch_hooks << block
end

#add_delete_hook(&block) ⇒ Object



1037
1038
1039
# File 'indexer/app/lib/indexer_common.rb', line 1037

def add_delete_hook(&block)
  @delete_hooks << block
end

#add_document_prepare_hook(&block) ⇒ Object



1012
1013
1014
# File 'indexer/app/lib/indexer_common.rb', line 1012

def add_document_prepare_hook(&block)
  @document_prepare_hooks << block
end

#add_extents(doc, record) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
# File 'indexer/app/lib/indexer_common.rb', line 323

def add_extents(doc, record)
  if record['record']['extents']
    extents = record['record']['extents']
    display_extents = extents.select {|extent| extent['portion'] == 'whole'}
    display_extents = extents if display_extents.empty?
    doc['extents'] = []
    display_extents.each do |extent|
      doc['extents'] << "#{extent['number']} --- #{extent['extent_type']}"
    end
  end
end

#add_extra_documents_hook(&block) ⇒ Object



1027
1028
1029
# File 'indexer/app/lib/indexer_common.rb', line 1027

def add_extra_documents_hook(&block)
  @extra_documents_hooks << block
end

#add_level(doc, record) ⇒ Object



300
301
302
303
304
# File 'indexer/app/lib/indexer_common.rb', line 300

def add_level(doc, record)
  if record['record'].has_key? 'level'
    doc['level'] = (record['record']['level'] === 'otherlevel') ? record['record']['other_level'] : record['record']['level']
  end
end

#add_notes(doc, record) ⇒ Object



264
265
266
267
268
269
270
# File 'indexer/app/lib/indexer_common.rb', line 264

def add_notes(doc, record)
  if record['record']['notes']
    # 'notes' only contains unpublished notes at this stage, but 'notes_published'
    # will be merged into it by Solr using copyField
    doc['notes_published'], doc['notes'] = IndexerCommon.extract_string_values(record['record']['notes'])
  end
end

#add_subjects(doc, record) ⇒ Object



234
235
236
237
238
239
# File 'indexer/app/lib/indexer_common.rb', line 234

def add_subjects(doc, record)
  if record['record']['subjects']
    doc['subjects'] = record['record']['subjects'].map {|s| s['_resolved']['title']}.compact
    doc['subject_uris'] = record['record']['subjects'].collect{|link| link['ref']}
  end
end

#add_subjects_subrecord(doc, record, subrecord, type = 'subjects') ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'indexer/app/lib/indexer_common.rb', line 242

def add_subjects_subrecord(doc, record, subrecord, type = 'subjects')
  doc['subjects'] ||= []
  doc['subject_uris'] ||= []

  if record['record'][subrecord]
    record['record'][subrecord].each do |sr|
      next unless sr[type]

      doc['subjects'].concat(sr[type].map {|s| s['_resolved']['title']}.compact)
      doc['subject_uris'].concat(sr[type].collect{|link| link['ref']})
    end
  end
end

#add_summary(doc, record) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'indexer/app/lib/indexer_common.rb', line 307

def add_summary(doc, record)
  if record['record'].has_key?('notes') && record['record']['notes'].is_a?(Array)
    notes = record['record']['notes']
    abstract = notes.find {|note| note['type'] == 'abstract'}
    if abstract
      doc['summary'] = abstract['content'].join("\n")
    else
      scopecontent = notes.find {|note| note['type'] == 'scopecontent'}
      if scopecontent && scopecontent.has_key?('subnotes')
        doc['summary'] = scopecontent['subnotes'].map {|sn| sn['content']}.join("\n")
      end
    end
  end
end

#add_years(doc, record) ⇒ Object



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
# File 'indexer/app/lib/indexer_common.rb', line 273

def add_years(doc, record)
  if record['record']['dates']
    doc['years'] = []
    record['record']['dates'].each do |date|
      doc['years'] += IndexerCommon.generate_years_for_date_range(date['begin'], date['end'])
    end
    unless doc['years'].empty?
      doc['years'] = doc['years'].sort.uniq
      doc['year_sort'] = doc['years'].first.rjust(4, '0') + doc['years'].last.rjust(4, '0')
    end
    dates = record['record']['dates']
    display_dates = dates.select {|date| date['date_type'] == 'inclusive'}
    display_dates = dates if display_dates.empty?
    doc['dates'] = []
    display_dates.each do |date|
      if date['expression']
        doc['dates'] << date['expression']
      elsif date['date_type'] === "single"
        doc['dates'] << date['begin']
      elsif date['date_type']
        doc['dates'] << "#{date['begin']} - #{date['end']}"
      end
    end
  end
end

#apply_pui_fields(doc, record) ⇒ Object



1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
# File 'indexer/app/lib/indexer_common.rb', line 1346

def apply_pui_fields(doc, record)
  # only add pui types if the record is published
  if doc['publish']
    object_record_types = ['accession', 'digital_object', 'digital_object_component']

    if object_record_types.include?(doc['primary_type'])
      doc['types'] << 'pui_record'
    end

    if ['agent_person', 'agent_corporate_entity'].include?(doc['primary_type'])
      doc['types'] << 'pui_agent'
    end

    unless RecordInheritance.has_type?(doc['primary_type'])
      # All record types are available to PUI except archival objects, since
      # our pui_indexer indexes a specially formatted version of those.
      if ['resource'].include?(doc['primary_type'])
        doc['types'] << 'pui_collection'
      elsif ['classification'].include?(doc['primary_type'])
        doc['types'] << 'pui_record_group'
      elsif ['agent_person'].include?(doc['primary_type'])
        doc['types'] << 'pui_person'
      else
        doc['types'] << 'pui_' + doc['primary_type']
      end

      doc['types'] << 'pui'
    end
  end

  # index all top containers for pui
  if doc['primary_type'] == 'top_container'
    doc['publish'] = record['record']['is_linked_to_published_record']
    if doc['publish']
      doc['types'] << 'pui_container'
      doc['types'] << 'pui'
    end
  end
end

#build_fullrecord(doc, record) ⇒ Object



190
191
192
193
194
# File 'indexer/app/lib/indexer_common.rb', line 190

def build_fullrecord(doc, record)
  # 'fullrecord' only contains unpublished text at this stage, but 'fullrecord_published'
  # will be merged into it by Solr using copyField
  doc['fullrecord_published'], doc['fullrecord'] = IndexerCommon.extract_string_values(record['record'])
end

#clean_for_sort(value) ⇒ Object



1168
1169
1170
1171
1172
1173
1174
1175
# File 'indexer/app/lib/indexer_common.rb', line 1168

def clean_for_sort(value)
  return nil if value.nil?
  out = value.gsub(/<[^>]+>/, '')
  out.gsub!(/-/, ' ')
  out.gsub!(/[^\p{L}\d\s_]/, '')
  out.gsub!(/\s+/, ' ')
  out.strip
end

#clean_whitespace(doc) ⇒ Object



1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
# File 'indexer/app/lib/indexer_common.rb', line 1155

def clean_whitespace(doc)
  if doc.is_a?(String) && !doc.frozen?
    doc.strip!
  elsif doc.is_a?(Hash)
    doc.values.each {|v| clean_whitespace(v)}
  elsif doc.is_a?(Array)
    doc.each {|v| clean_whitespace(v)}
  end

  doc
end

#configure_doc_rulesObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
# File 'indexer/app/lib/indexer_common.rb', line 374

def configure_doc_rules

  add_document_prepare_hook {|doc, record|
    found_keys = Set.new

    ASUtils.search_nested(record["record"], enum_fields, ['_resolved']) do |field, field_value|
      key = "#{field}_enum_s"

      doc[key] ||= Set.new
      doc[key] << field_value

      found_keys << key
    end

    ASUtils.search_nested(record["record"], ['items'], ['_resolved']) do |field, field_value|
      if field_value.is_a?(Hash) && field_value.key?('type')
        doc['type_enum_s'] ||= Set.new
        doc['type_enum_s'] << field_value.fetch('type')
        found_keys << 'type_enum_s'
      end
    end

    # Turn our sets back into regular arrays so they serialize out to JSON correctly
    found_keys.each do |key|
      doc[key] = doc[key].to_a.flatten
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'archival_object'
      doc['resource'] = record['record']['resource']['ref'] if record['record']['resource']
      doc['title'] = record['record']['display_string']
      doc['identifier'] = record['record']['component_id']
      doc['component_id'] = record['record']['component_id']
      doc['ref_id'] = record['record']['ref_id']
      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']
    end
  }

  add_document_prepare_hook {|doc, record|
    add_subjects(doc, record)
    add_agents(doc, record)
    add_audit_info(doc, record)
    add_notes(doc, record)
    add_years(doc, record)
    add_level(doc, record)
    add_summary(doc, record)
    add_extents(doc, record)
    add_arks(doc, record)
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'accession'
      date = record['record']['accession_date']
      if date == '9999-12-31'
        unknown = I18n.t('accession.accession_date_unknown')
        doc['accession_date'] = unknown
        doc['fullrecord'] ||= ''
        doc['fullrecord'] << unknown + ' '
      else
        doc['accession_date'] = date
      end
      doc['accession_date_year'] = Date.parse(date).year
      doc['identifier'] = (0...4).map {|i| record['record']["id_#{i}"]}.compact.join("-")
      doc['title'] = record['record']['display_string']

      doc['acquisition_type'] = record['record']['acquisition_type']
      doc['resource_type'] = record['record']['resource_type']
      doc['restrictions_apply'] = record['record']['restrictions_apply']
      doc['access_restrictions'] = record['record']['access_restrictions']
      doc['use_restrictions'] = record['record']['use_restrictions']
      doc['related_resource_uris'] = record['record']['related_resources'].
                                        collect { |resource| resource["ref"] }.
                                        compact.uniq

      doc['related_accession_uris'] = record['record']['related_accessions'].
                                         collect { |accession| accession["ref"] }.
                                         compact.uniq

      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']
      if cm = record['record']['collection_management']
        doc['processing_priority'] = cm['processing_priority']
        doc['processors'] = cm['processors']
      end
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'subject'
      doc['source'] = record['record']['source']
      doc['first_term_type'] = record['record']['terms'][0]['term_type']
      doc['publish'] = record['record']['publish'] && record['record']['is_linked_to_published_record']
      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']
    end
  }

  add_document_prepare_hook {|doc, record|
    if record['record'].has_key?('used_within_repositories')
      doc['used_within_repository'] = record['record']['used_within_repositories']
      doc['used_within_published_repository'] = record['record']['used_within_published_repositories']
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'repository'
      doc['repository'] = doc["id"]
      doc['title'] = record['record']['repo_code']
      doc['repo_sort'] = record['record']['display_string']
      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']
      doc['position_int_sort'] = record['record']['position']
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'location'
      if record['record'].has_key? 'temporary'
        doc['temporary'] = record['record']['temporary']
      end
      doc['building'] = record['record']['building']
      doc['floor'] = record['record']['floor']
      doc['room'] = record['record']['room']
      doc['area'] = record['record']['area']
     if record['record']['owner_repo']
       repo = JSONModel::HTTP.get_json(record['record']['owner_repo']['ref'])
        doc['owner_repo_uri_u_sstr'] = record['record']['owner_repo']['ref']
        doc['owner_repo_display_string_u_ssort'] = repo["repo_code"]
     end
     end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'digital_object_component'
      doc['digital_object'] = record['record']['digital_object']['ref']
      doc['digital_object_id'] = record['record']['component_id']
      doc['identifier'] = record['record']['component_id']
      doc['title'] = record['record']['display_string']
      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'resource'
      doc['finding_aid_title'] = record['record']['finding_aid_title']
      doc['finding_aid_filing_title'] = record['record']['finding_aid_filing_title']
      doc['identifier'] = (0...4).map {|i| record['record']["id_#{i}"]}.compact.join("-")
      doc['resource_type'] = record['record']['resource_type']
      doc['level'] = record['record']['level']
      doc['restrictions'] = record['record']['restrictions']
      doc['ead_id'] = record['record']['ead_id']
      doc['finding_aid_status'] = record['record']['finding_aid_status']
      doc['related_accession_uris'] = record['record']['related_accessions'].
                                         collect{|accession| accession["ref"]}.
                                         compact.uniq
      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']
      if cm = record['record']['collection_management']
        doc['processing_priority'] = cm['processing_priority']
        doc['processors'] = cm['processors']
      end
    end

    if doc['primary_type'] == 'digital_object'
      doc['digital_object_type'] = record['record']['digital_object_type']

      doc['digital_object_id'] = record['record']['digital_object_id']
      doc['identifier'] = record['record']['digital_object_id']
      doc['level'] = record['record']['level']
      doc['restrictions'] = record['record']['restrictions']
      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']

      doc['collection_uri_u_sstr'] = record['record']['collection'].map {|collection| collection['ref']}
      doc['linked_instance_uris'] = record['record']['linked_instances'].
                                       collect{|instance| instance["ref"]}.
                                       compact.uniq
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'repository'
      doc['repository'] = doc["id"]
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'event'
      doc['event_type'] = record['record']['event_type']
      doc['title'] = record['record']['event_type'] # adding this for emedded searches
      doc['outcome'] = record['record']['outcome']
      doc['linked_record_uris'] = record['record']['linked_records'].map { |c| c['ref'] }

      # ANW-1635: index linked record titles/display names so they are available in CSV output
      doc['linked_record_titles'] = record['record']['linked_records'].map do |rec|
        if    rec['_resolved']['jsonmodel_type'] == "agent_person"
          rec['_resolved']['display_name']['sort_name']
        elsif rec['_resolved']['jsonmodel_type'] == "agent_family"
          rec['_resolved']['display_name']['sort_name']
        elsif rec['_resolved']['jsonmodel_type'] == "agent_corporate_entity"
          rec['_resolved']['display_name']['sort_name']
        elsif rec['_resolved']['jsonmodel_type'] == "agent_software"
          rec['_resolved']['display_name']['sort_name']
        elsif rec['_resolved']['jsonmodel_type'] == "accession"
          rec['_resolved']['title']
        elsif rec['_resolved']['jsonmodel_type'] == "resource"
          rec['_resolved']['title']
        elsif rec['_resolved']['jsonmodel_type'] == "digital_object"
          rec['_resolved']['title']
        elsif rec['_resolved']['jsonmodel_type'] == "digital_object_component"
          rec['_resolved']['title']
        elsif rec['_resolved']['jsonmodel_type'] == "archival_object"
          rec['_resolved']['display_string']
        else
          "not_found"
        end
      end
    end
  }

  add_document_prepare_hook {|doc, record|
    if ['agent_person', 'agent_family', 'agent_software', 'agent_corporate_entity'].include?(doc['primary_type'])
      record['record'].reject! { |rec| rec === 'agent_contacts' }
      doc['title'] = record['record']['display_name']['sort_name']

      authorized_name = record['record']['names'].find {|name| name['authorized']}

      has_conventions_dec = !record['record']['agent_conventions_declarations'].empty?
      has_agent_record_id = !record['record']['agent_record_identifiers'].empty?

      if has_agent_record_id
        primary_record_id = record['record']['agent_record_identifiers'].select do |ari|
          ari['primary_identifier'] == true
        end

        primary_record_id = primary_record_id.first
      else
        primary_record_id = nil
      end

      if has_conventions_dec
        conventions_dec = record['record']['agent_conventions_declarations'].first
      else
        conventions_dec = nil
      end

      if primary_record_id
        doc['authority_id'] = primary_record_id['record_identifier']
        doc['source'] = primary_record_id['source']
      elsif authorized_name
        doc['authority_id'] = authorized_name['authority_id']
        doc['source'] = authorized_name['source']
      end

      if conventions_dec && conventions_dec['name_rule']
        doc['rules'] = conventions_dec['name_rule']
      elsif authorized_name
        doc['rules'] = authorized_name['rules']
      end

      doc['linked_agent_roles'] = record['record']['linked_agent_roles']

      doc['related_agent_uris'] = ASUtils.wrap(record['record']['related_agents']).collect{|ra| ra['ref']}
      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']

      if record['record']['is_user']
        doc['is_user'] = true
        doc['types'] << 'agent_with_user'
      else
        doc['is_user'] = false
      end

      add_subjects_subrecord(doc, record, 'agent_functions')
      add_subjects_subrecord(doc, record, 'agent_occupations')
      add_subjects_subrecord(doc, record, 'agent_places')
      add_subjects_subrecord(doc, record, 'agent_topics')

      add_subjects_subrecord(doc, record, 'agent_functions', 'places')
      add_subjects_subrecord(doc, record, 'agent_occupations', 'places')
      add_subjects_subrecord(doc, record, 'agent_resources', 'places')
      add_subjects_subrecord(doc, record, 'agent_topics', 'places')

      # Assign the additional type of 'agent'
      doc['types'] << 'agent'
    end
  }

  add_document_prepare_hook {|doc, record|
    doc['external_id'] = Array(record['record']['external_ids']).map do |eid|
      eid['external_id']
    end
  }

  add_document_prepare_hook {|doc, record|
    if ['classification', 'classification_term'].include?(doc['primary_type'])
      doc['classification_path'] = ASUtils.to_json(record['record']['path_from_root'])
      doc['agent_uris'] = ASUtils.wrap(record['record']['creator']).collect{|agent| agent['ref']}
      doc['published_agent_uris'] = []
      if !record.dig(:record, :creator, :_resolved).nil?
         if record['record']['creator']['_resolved']['publish'] && !record['record']['creator']['ref'].nil?
           doc['published_agent_uris'] << record['record']['creator']['ref']
         end
      end
      doc['agents'] = ASUtils.wrap(record['record']['creator']).collect{|link| link['_resolved']['display_name']['sort_name']}
      doc['identifier_sort'] = IndexerCommon.generate_sort_string_for_identifier(record['record']['identifier'])
      doc['repo_sort'] = record['record']['repository']['_resolved']['display_string']
      doc['has_classification_terms'] = record['record']['has_classification_terms']
      doc['slug'] = record['record']['slug']
      doc['is_slug_auto'] = record['record']['is_slug_auto']
      doc['identifier'] = record['record']['identifier']
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'classification_term'
      doc['classification'] = record['record']['classification']['ref']
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'job'
      report_type = record['record']['job']['report_type']
      doc['title'] = (report_type ? I18n.t("reports.#{report_type}.title", :default => report_type) :
        I18n.t("job.types.#{record['record']['job_type']}"))
      doc['types'] << record['record']['job_type']
      doc['types'] << report_type
      doc['job_type'] = record['record']['job_type']
      doc['report_type'] = report_type
      doc['job_report_type'] = report_type || doc['job_type']
      doc['status'] = record['record']['status']
      doc['owner'] = record['record']['owner']
      doc['time_submitted'] = Time.parse(record['record']['time_submitted']).getlocal if record['record']['time_submitted']
      doc['time_started'] = Time.parse(record['record']['time_started']).getlocal if record['record']['time_started']
      doc['time_finished'] = Time.parse(record['record']['time_finished']).getlocal if record['record']['time_finished']

      filenames = record['record']['job']['filenames'] || [record['record']['job']['filename']].compact
      doc['files'] = []
      doc['job_data'] = []
      files = JSONModel::HTTP::get_json("#{record['record']['uri']}/output_files")
      files.each do |file|
        job_id = record['record']['uri'].split('/').last
        link = "/jobs/#{job_id}/file/#{file}"
        doc['files'] << link
        filename = filenames.shift
        doc['job_data'] << (filename ? "input_file --- #{filename}" : "output_file --- #{link}")
      end
      unless record['record']['job'].is_a? String
        record['record']['job'].reject { |k, _v| ['jsonmodel_type', 'filenames', 'report_type'].include? k }.each do |k, v|
          doc['job_data'] << "#{k} --- #{v}"
        end
      end
      doc['queue_position'] = record['record']['queue_position']
    end
  }


  add_document_prepare_hook {|doc, record|
    records_with_classifications = ['resource', 'accession', 'digital_object']

    if records_with_classifications.include?(doc['primary_type']) && record['record']['classifications'].length > 0
      doc['classification_paths'] = record['record']['classifications'].map { |c| ASUtils.to_json(c['_resolved']['path_from_root']) }
      doc['classification_uris'] = record['record']['classifications'].map { |c| c['ref'] }
    end
  }

  add_document_prepare_hook {|doc, record|
    if ['resource', 'archival_object', 'accession'].include?(doc['primary_type']) && record['record']['instances'] && record['record']['instances'].length > 0
      doc['location_uris'] = record['record']['instances'].
                                collect{|instance| instance["sub_container"]}.compact.
                                collect{|sub_container| sub_container["top_container"]["_resolved"]}.compact.
                                collect{|top_container| top_container["container_locations"]}.flatten.
                                collect{|container_location| container_location["ref"]}.uniq
      doc['digital_object_uris'] = record['record']['instances'].
                                      collect{|instance| instance["digital_object"]}.compact.
                                      collect{|digital_object_instance| digital_object_instance["ref"]}.
                                      flatten.uniq
    end
  }


  # Index four-part IDs separately
  add_document_prepare_hook {|doc, record|
    four_part_id = (0..3).map {|n| record['record']["id_#{n}"]}.compact.join(" ")

    unless four_part_id.empty?
      doc['four_part_id'] = four_part_id
    end
  }


  add_document_prepare_hook {|doc, record|
    if record['record']['jsonmodel_type'] == 'top_container'
      doc['title'] = record['record']['long_display_string']
      doc['display_string'] = record['record']['display_string']
      doc['type_u_ssort'] = record['record']['type']
      doc['notes'] = record['record']['internal_note']

      if record['record']['series']
        doc['series_uri_u_sstr'] = record['record']['series'].map {|series| series['ref']}
        doc['series_title_u_sstr'] = record['record']['series'].map {|series| series['display_string']}
        doc['series_level_u_sstr'] = record['record']['series'].map {|series| series['level_display_string']}
        doc['series_identifier_stored_u_sstr'] = record['record']['series'].map {|series| series['identifier']}
        doc['series_identifier_u_stext'] = record['record']['series'].map {|series|
          IndexerCommon.generate_permutations_for_identifier(series['identifier'])
        }.flatten

        record['record']['series'].select{|series| series['publish']}.each do |series|
          doc['published_series_uri_u_sstr'] ||= []
          doc['published_series_uri_u_sstr'] << series['ref']
          doc['published_series_title_u_sstr'] ||= []
          doc['published_series_title_u_sstr'] << series['display_string']
        end
      end

      if record['record']['collection']
        doc['collection_uri_u_sstr'] = record['record']['collection'].map {|collection| collection['ref']}
        doc['collection_display_string_u_sstr'] = record['record']['collection'].map {|collection| collection['display_string']}
        doc['collection_identifier_stored_u_sstr'] = record['record']['collection'].map {|collection| collection['identifier']}
        doc['collection_identifier_u_stext'] = record['record']['collection'].map {|collection|
          IndexerCommon.generate_permutations_for_identifier(collection['identifier'])
        }.flatten
      end

      if record['record']['container_profile']
        doc['container_profile_uri_u_sstr'] = record['record']['container_profile']['ref']
        doc['container_profile_display_string_u_sstr'] = record['record']['container_profile']['_resolved']['display_string']
      end

      if record['record']['container_locations'].length > 0
        doc['has_location_u_sbool'] = true
        doc['location_uri_u_sstr'] = []
        doc['location_uris'] = []
        doc['location_display_string_u_sstr'] = []
        record['record']['container_locations'].each do |container_location|
          if container_location['status'] == 'current'
            doc['location_uri_u_sstr'] << container_location['ref']
            doc['location_uris'] << container_location['ref']
            doc['location_display_string_u_sstr'] << container_location['_resolved']['title']
          end
        end
      else
        doc['has_location_u_sbool'] = false
      end
      doc['exported_u_sbool'] = record['record'].has_key?('exported_to_ils')
      doc['empty_u_sbool'] = record['record']['collection'].empty?

      if record['record']['indicator']
        doc['indicator_u_icusort'] = record['record']['indicator']
      end

      doc['top_container_u_typeahead_utext'] = record['record']['display_string'].gsub(/[^0-9A-Za-z]/, '').downcase
      doc['top_container_u_icusort'] = record['record']['display_string']
      doc['barcode_u_sstr'] = record['record']['barcode']
      doc['barcode_u_icusort'] = record['record']['barcode']

      doc['subcontainer_barcodes_u_sstr'] = record["record"]["subcontainer_barcodes"]
      doc['created_for_collection_u_sstr'] = record['record']['created_for_collection']
    end
  }


  add_document_prepare_hook {|doc, record|
    if ['resource', 'archival_object', 'accession'].include?(doc['primary_type'])
      # we no longer want the contents of containers to be indexed at the container's location
      doc.delete('location_uris')

      # index the top_container's linked via a sub_container
      ASUtils.wrap(record['record']['instances']).each{|instance|
        if instance['sub_container'] && instance['sub_container']['top_container']
          doc['top_container_uri_u_sstr'] ||= []
          doc['top_container_uri_u_sstr'] << instance['sub_container']['top_container']['ref']
          if instance['sub_container']['type_2']
            doc['child_container_u_sstr'] ||= []
            doc['child_container_u_sstr'] << "#{instance['sub_container']['type_2']} #{instance['sub_container']['indicator_2']} #{instance['sub_container']['barcode_2']}"
          end
          if instance['sub_container']['type_3']
            doc['grand_child_container_u_sstr'] ||= []
            doc['grand_child_container_u_sstr'] << "#{instance['sub_container']['type_3']} #{instance['sub_container']['indicator_3']}"
          end
        end
      }
    end
  }


  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'container_profile'
      doc['title'] = record['record']['display_string']
      doc['display_string'] = record['record']['display_string']
      doc['note'] = record['record']['note']

      ['width', 'height', 'depth'].each do |property|
        doc["container_profile_#{property}_u_sstr"] = record['record'][property]
      end

      doc["container_profile_dimension_units_u_sstr"] = record['record']['dimension_units']

      doc['typeahead_sort_key_u_sort'] = record['record']['display_string']
    end
  }


  add_document_prepare_hook { |doc, record|
    if !self.instance_of?(PUIIndexer)
      # The PUI indexer makes its own call to build_fullrecord, so only call it here for realtime and periodic
      build_fullrecord(doc, record)
    end
  }

  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'location_profile'
      doc['title'] = record['record']['display_string']
      doc['display_string'] = record['record']['display_string']

      ['width', 'height', 'depth'].each do |property|
        doc["location_profile_#{property}_u_sstr"] = record['record'][property]
      end

      doc["location_profile_dimension_units_u_sstr"] = record['record']['dimension_units']

      doc['typeahead_sort_key_u_sort'] = record['record']['display_string']
    end

    if record['record']['location_profile']
      doc['location_profile_uri_u_sstr'] = record['record']['location_profile']['ref']
      doc['location_profile_display_string_u_ssort'] = record['record']['location_profile']['_resolved']['display_string']
    end
  }

  add_document_prepare_hook {|doc, record|
    doc['ancestors'] = ASUtils.wrap(record['record']['ancestors']).map {|ancestor|
      ancestor.fetch('ref')
    }
  }

  add_document_prepare_hook {|doc, record|
    ASUtils.wrap(record['record']['rights_statements']).each do |rights_statement|
      ASUtils.wrap(rights_statement['linked_agents']).each do |agent_link|
        doc['rights_statement_agent_uris'] ||= []
        doc['rights_statement_agent_uris'] << agent_link['ref']
      end
    end
  }

  record_has_children('collection_management')
  add_extra_documents_hook {|record|
    docs = []

    cm = record['record']['collection_management']
    if cm
      parent_type = JSONModel.parse_reference(record['uri'])[:type]
      title = record['record']['title'] || record['record']['display_string']
      docs << {
        'id' => cm['uri'],
        'uri' => cm['uri'],
        'parent_id' => record['uri'],
        'parent_title' => title,
        'parent_type' => parent_type,
        'title' => title,
        'title_sort' => clean_for_sort(title),
        'types' => ['collection_management'],
        'primary_type' => 'collection_management',
        'json' => cm.to_json(:max_nesting => false),
        'processing_priority' => cm['processing_priority'],
        'processing_status' => cm['processing_status'],
        'processing_hours_total' => cm['processing_hours_total'],
        'processing_funding_source' => cm['processing_funding_source'],
        'processors' => cm['processors'],
        'suppressed' => record['record']['suppressed'],
        'repository' => get_record_scope(record['uri']),
        'created_by' => cm['created_by'],
        'last_modified_by' => cm['last_modified_by'],
        'system_mtime' => cm['system_mtime'],
        'user_mtime' => cm['user_mtime'],
        'create_time' => cm['create_time'],
      }
    end

    docs
  }


  add_document_prepare_hook {|doc, record|
    if doc['primary_type'] == 'assessment'
      doc['assessment_id'] = JSONModel.parse_reference(record['record']['uri']).fetch(:id)
      doc['title'] = record['record']['display_string']
      doc['display_string'] = record['record']['display_string']

      doc['assessment_record_uris'] = ASUtils.wrap(record['record']['records']).map{|r| r['ref']}
      doc['assessment_records'] = ASUtils.wrap(record['record']['records']).map{|r| r['_resolved']['display_string'] || r['_resolved']['title']}
      doc['assessment_record_types'] = ASUtils.wrap(record['record']['records']).map{|r| r['_resolved']['jsonmodel_type']}.uniq.sort
      doc['assessment_surveyor_uris'] = ASUtils.wrap(record['record']['surveyed_by']).map{|r| r['ref']}
      doc['assessment_surveyors'] = ASUtils.wrap(record['record']['surveyed_by']).map{|r| r['_resolved']['title']}
      doc['assessment_survey_begin'] = "#{record['record']['survey_begin']}T00:00:00Z"
      doc['assessment_survey_end'] = "#{record['record']['survey_end']}T00:00:00Z" if record['record']['survey_end']
      doc['assessment_review_required'] = record['record']['review_required']
      doc['assessment_sensitive_material'] = record['record']['sensitive_material']
      if (ASUtils.wrap(record['record']['reviewer']).length > 0)
        doc['assessment_reviewer_uris'] = ASUtils.wrap(record['record']['reviewer']).map{|r| r['ref']}
        doc['assessment_reviewers'] = ASUtils.wrap(record['record']['reviewer']).map{|r| r['_resolved']['title']}
      end
      doc['assessment_inactive'] = record['record']['inactive']

      doc['assessment_survey_year'] = IndexerCommon.generate_years_for_date_range(record['record']['survey_begin'], record['record']['survey_end'])

      doc['assessment_collection_uris'] = ASUtils.wrap(record['record']['collections']).map{|r| r['ref']}
      doc['assessment_collections'] = ASUtils.wrap(record['record']['collections']).map{|r| r['_resolved']['display_string'] || r['_resolved']['title']}

      doc['assessment_completed'] = !record['record']['survey_end'].nil?

      doc['assessment_formats'] = record['record']['formats'].select{|r| r.has_key?('value')}.map{|r| r['label']}
      doc['assessment_ratings'] = record['record']['ratings'].select{|r| r.has_key?('value') || r.has_key?('note')}.map{|r| r['label']}
      doc['assessment_conservation_issues'] = record['record']['conservation_issues'].select{|r| r.has_key?('value')}.map{|r| r['label']}

      doc['title_sort'] = doc['assessment_id'].to_s.rjust(10, '0')
    end
  }


  add_document_prepare_hook {|doc, record|
    doc['langcode'] ||= []
    if record['record'].has_key?('lang_materials') and record['record']['lang_materials'].is_a?(Array)
      record['record']['lang_materials'].each { |langmaterial|
        if langmaterial.has_key?('language_and_script')
          doc['langcode'].push(langmaterial['language_and_script']['language'])
        end
      }
      doc['langcode'].uniq!
    end
  }

end

#dedupe_by_uri(records) ⇒ Object

When applying a batch of updates, keep only the most recent version of each record



1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'indexer/app/lib/indexer_common.rb', line 1140

def dedupe_by_uri(records)
  result = []
  seen = {}

  records.reverse.each do |record|
    if !seen[record['uri']]
      result << record
      seen[record['uri']] = true
    end
  end

  result.reverse
end

#delete_records(records, opts = {}) ⇒ Object



1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'indexer/app/lib/indexer_common.rb', line 1109

def delete_records(records, opts = {})

  return if records.empty?

  req = Net::HTTP::Post.new("#{solr_url.path}/update")
  req['Content-Type'] = 'application/json'

  # Delete the ID plus any documents that were the child of that ID
  delete_request = {:delete => records.map {|id|
      [{"id" => id},
       {'query' => opts.fetch(:parent_id_field, 'parent_id') + ":\"#{id}\""}]}.flatten(1)
  }

  @delete_hooks.each do |hook|
    hook.call(records, delete_request)
  end

  req.body = delete_request.to_json

  response = do_http_request(solr_url, req)


  if response.code == '200'
    Log.info "Deleted #{records.length} documents: #{response}"
  else
    Log.error "SolrIndexerError when deleting records: #{response.body}"
  end
end

#do_http_request(url, req) ⇒ Object



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'indexer/app/lib/indexer_common.rb', line 1047

def do_http_request(url, req)
  req['X-ArchivesSpace-Session'] = @current_session

  opts = {
    :read_timeout => AppConfig[:indexer_solr_timeout_seconds].to_i
  }

  ASHTTP.start_uri(url, opts) do |http|
    http.request(req)
  end
rescue Timeout::Error
  FakeSolrTimeoutResponse.new(req)
end

#enum_fieldsObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'indexer/app/lib/indexer_common.rb', line 336

def enum_fields
  return @enum_fields if @enum_fields

  enum_fields = []
  queue = JSONModel.models.map {|_,model| model.schema['properties']}.flatten.uniq

  while !queue.empty?
    elt = queue.shift

    if elt.is_a?(Hash)
      elt.each do |k, v|
        if v.is_a?(Hash)
          enum_fields.push(k) if v['dynamic_enum'] || v.dig('items', 'dynamic_enum')
        end
        queue << v
      end
    elsif elt.is_a?(Array)
      queue.concat(elt)
    end
  end

  enum_fields.delete('items') # not an enum, creeps in through dynamic enum lists
  @enum_fields = enum_fields.uniq
end

#get_record_scope(uri) ⇒ Object



1095
1096
1097
# File 'indexer/app/lib/indexer_common.rb', line 1095

def get_record_scope(uri)
  JSONModel.parse_reference(uri)[:repository] || "global"
end

#index_batch(batch, timing = IndexerTiming.new, opts = {}) ⇒ Object



1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
# File 'indexer/app/lib/indexer_common.rb', line 1270

def index_batch(batch, timing = IndexerTiming.new, opts = {})
  timing ||= IndexerTiming.new

  timing.time_block(:batch_hooks_ms) do
    # Allow hooks to operate on the entire batch if desired
    @batch_hooks.each_with_index do |hook|
      hook.call(batch)
    end
  end

  if !batch.empty?
    # For any record we're updating, delete any child records first (where applicable)
    records_with_children = self.records_with_children.map {|record_type|
      batch.record_info_for_type(record_type).map {|info| '"%s"' % [info[:id]]}
    }.flatten

    if !records_with_children.empty?
      req = Net::HTTP::Post.new("#{solr_url.path}/update")
      req['Content-Type'] = 'application/json'
      req.body = {:delete => {'query' => opts.fetch(:parent_id_field, 'parent_id') + ":(" + records_with_children.join(" OR ") + ")"}}.to_json
      response = do_http_request(solr_url, req)
    end

    # Now apply the updates
    req = Net::HTTP::Post.new("#{solr_url.path}/update")
    req['Content-Type'] = 'application/json'

    # Note: We call to_json_stream before asking for the count because this
    # writes out the closing array and newline.
    stream = batch.to_json_stream
    req['Content-Length'] = batch.byte_count

    req.body_stream = stream

    timing.time_block(:solr_add_ms) do
      response = do_http_request(solr_url, req)

      stream.close
      batch.destroy

      if response.code != '200'
        Log.error "SolrIndexerError when indexing records: #{response.body}"
      end
    end
  end
end

#index_records(records, timing = IndexerTiming.new) ⇒ Object



1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
# File 'indexer/app/lib/indexer_common.rb', line 1200

def index_records(records, timing = IndexerTiming.new)
  batch = IndexBatch.new

  records = dedupe_by_uri(records)

  timing.time_block(:conversion_ms) do
    records.each do |record|
      values = record['record']
      uri = record['uri']

      reference = JSONModel.parse_reference(uri)
      record_type = reference && reference[:type]

      if !record_type || skip_index_record?(record) || (record_type != 'repository' && !record_types.include?(record_type.intern))
        next
      end

      doc = {}

      doc['id'] = uri
      doc['uri'] = uri

      if ( !values["finding_aid_filing_title"].nil? && values["finding_aid_filing_title"].length > 0 )
        doc['title'] = values["finding_aid_filing_title"]
      else
        doc['title'] =  values['title']
      end

      doc['primary_type'] = record_type
      doc['types'] = [record_type]
      doc['json'] = ASUtils.to_json(sanitize_json(values))
      doc['suppressed'] = values.has_key?('suppressed') && values['suppressed']
      if doc['suppressed']
        doc['publish'] = false
      elsif is_repository_unpublished?(uri, values)
        doc['publish'] = false
      elsif values['has_unpublished_ancestor']
        doc['publish'] = false
      else
        doc['publish'] = values.has_key?('publish') && values['publish']
      end
      doc['system_generated'] = values.has_key?('system_generated') ? values['system_generated'].to_s : 'false'
      doc['repository'] = get_record_scope(uri)

      @document_prepare_hooks.each do |hook|
        hook.call(doc, record)
      end

      doc['title_sort'] ||= clean_for_sort(doc['title'])

      # do this last of all so we know for certain the doc is published
      apply_pui_fields(doc, record)

      next if skip_index_doc?(doc)

      batch << clean_whitespace(doc)

      # Allow a single record to spawn multiple Solr documents if desired
      @extra_documents_hooks.each do |hook|
        batch.concat(hook.call(record))
      end
    end
  end

  index_batch(batch, timing)

  timing
end

#is_repository_unpublished?(uri, values) ⇒ Boolean

Returns:

  • (Boolean)


1100
1101
1102
1103
1104
1105
1106
# File 'indexer/app/lib/indexer_common.rb', line 1100

def is_repository_unpublished?(uri, values)
  repo_id = get_record_scope(uri)

  return false if (repo_id == "global")

  values['repository']['_resolved']['publish'] == false
end

#loginObject



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
# File 'indexer/app/lib/indexer_common.rb', line 1067

def 
  if @current_session
    return @current_session
  end

  username = AppConfig[:search_username]
  password = AppConfig[:search_user_secret]

  url = URI.parse(@backend_url + "/users/#{username}/login")

  request = Net::HTTP::Post.new(url.request_uri)
  request.set_form_data("expiring" => "false",
                        "password" => password)

  response = do_http_request(url, request)

  if response.code == '200'
    auth = ASUtils.json_parse(response.body)

    @current_session = auth['session']
    JSONModel::HTTP.current_backend_session = auth['session']

  else
    raise "Authentication to backend failed: #{response.body}"
  end
end

#paused?Boolean

Returns:

  • (Boolean)


1334
1335
1336
# File 'indexer/app/lib/indexer_common.rb', line 1334

def paused?
  self.singleton_class.class_variable_get(:@@paused_until) > Time.now
end

#record_has_children(record_type) ⇒ Object



1017
1018
1019
# File 'indexer/app/lib/indexer_common.rb', line 1017

def record_has_children(record_type)
  @@records_with_children << record_type.to_s
end

#record_typesObject



46
47
48
# File 'indexer/app/lib/indexer_common.rb', line 46

def record_types
  @@record_types
end

#records_with_childrenObject



1022
1023
1024
# File 'indexer/app/lib/indexer_common.rb', line 1022

def records_with_children
  @@records_with_children || []
end

#reset_sessionObject



1062
1063
1064
# File 'indexer/app/lib/indexer_common.rb', line 1062

def reset_session
  @current_session = nil
end

#resolved_attributesObject



42
43
44
# File 'indexer/app/lib/indexer_common.rb', line 42

def resolved_attributes
  @@resolved_attributes
end

#sanitize_json(json) ⇒ Object

ANW-1065 iterate through the do_not_index list and scrub out that part of the JSON tree



1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# File 'indexer/app/lib/indexer_common.rb', line 1179

def sanitize_json(json)
  IndexerCommonConfig.do_not_index.each do |k, v|
    if json["jsonmodel_type"] == k
      # subrec is a reference used to navigate inside of the JSON as specified by the v[:location] to find the part of the tree to sanitize
      subrec = json

      v[:location].each do |l|
        unless subrec.nil?
          subrec = subrec[l]
        end
      end

      unless subrec.nil?
        subrec[v[:to_clean]] = []
      end
    end
  end

  return json
end

#send_commit(type = :hard) ⇒ Object



1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
# File 'indexer/app/lib/indexer_common.rb', line 1318

def send_commit(type = :hard)
  req = Net::HTTP::Post.new("#{solr_url.path}/update")
  req['Content-Type'] = 'application/json'
  req.body = {:commit => {"softCommit" => (type == :soft) }}.to_json

  response = do_http_request(solr_url, req)

  if response.code != '200'
    if response.body =~ /exceeded limit of maxWarmingSearchers/
      Log.info "INFO: #{response.body}"
    else
      Log.error "SolrIndexerError when committing: #{response.body}"
    end
  end
end

#skip_index_doc?(doc) ⇒ Boolean

Returns:

  • (Boolean)


1342
1343
1344
# File 'indexer/app/lib/indexer_common.rb', line 1342

def skip_index_doc?(doc)
  false
end

#skip_index_record?(record) ⇒ Boolean

Returns:

  • (Boolean)


1338
1339
1340
# File 'indexer/app/lib/indexer_common.rb', line 1338

def skip_index_record?(record)
  false
end

#solr_urlObject



1042
1043
1044
# File 'indexer/app/lib/indexer_common.rb', line 1042

def solr_url
  URI.parse(AppConfig[:solr_url])
end

#trim_ark_value(s) ⇒ Object



361
362
363
# File 'indexer/app/lib/indexer_common.rb', line 361

def trim_ark_value(s)
  s.gsub(/\A.*ark:/, 'ark:')
end