Class: Lcms::Engine::Search::Document

Inherits:
ElasticSearchDocument show all
Defined in:
app/models/lcms/engine/search/document.rb

Constant Summary collapse

METADATA_FIELDS =
%w(description teaser title lesson_objective).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ElasticSearchDocument

#delete!, #index!, #read_attribute_for_serialization, #repository, repository

Class Method Details

.build_from(model) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/lcms/engine/search/document.rb', line 33

def build_from(model)
  case model
  when Lcms::Engine::Resource
    new(**attrs_from_resource(model))

  when Lcms::Engine::ExternalPage
    new(**attrs_from_page(model))

  else
    raise "Unsupported Type for Search : #{model.class.name}"
  end
end

.doc_type(model) ⇒ Object



46
47
48
# File 'app/models/lcms/engine/search/document.rb', line 46

def doc_type(model)
  model.resource_type == 'resource' ? model.curriculum_type : model.resource_type
end

.document_metadata(model) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/models/lcms/engine/search/document.rb', line 50

def (model)
  return unless model.document?

  METADATA_FIELDS.map do |k|
    value = model.document.[k]
    Nokogiri::HTML.fragment(value).text.presence
  end.compact.join(' ')
end

.grade_position(model) ⇒ Object

Position mask:

  • Since lessons uses 4 blocks of 2 numbers for (grade, mod, unit, lesson), we use 5 blocks to place them after lessons.

  • the first position is realted to the resource type (always starting with 9 to be placed after the lessons).

  • The second most significant is related to the grade

  • The last position is the number of different grades covered, i.e: a resource with 3 different grades show after one with 2, (more specific at the top, more generic at the bottom)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/lcms/engine/search/document.rb', line 68

def grade_position(model)
  if model.is_a?(Lcms::Engine::Resource) && model.generic?
    rtype = model[:resource_type] || 0
    # for generic resource use the min grade, instead the avg
    grade_pos = model.grades.list.map { |g| Lcms::Engine::Grades.grades.index(g) }.compact.min || 0
    last_pos = model.grades.list.size
  else
    rtype = 0
    grade_pos = model.grades.average_number
    last_pos = 0
  end
  first_pos = 90 + rtype

  [first_pos, grade_pos, 0, 0, last_pos].map { |n| n.to_s.rjust(2, '0') }.join(' ')
end

.resource_position(model) ⇒ Object



84
85
86
87
88
89
90
# File 'app/models/lcms/engine/search/document.rb', line 84

def resource_position(model)
  if model.media? || model.generic?
    grade_position(model)
  else
    model.hierarchical_position
  end
end

.search(term, options = {}) ⇒ Object

Overrides ElasticSearchDocument.search to include standards search



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/lcms/engine/search/document.rb', line 93

def search(term, options = {})
  return repository.empty_response unless repository.index_exists?
  return repository.search(repository.all_query(options)) unless term.present?

  repository.multisearch(
    [
      repository.standards_query(term, options),
      repository.tags_query(term, [:tag_keywords], options),
      repository.tags_query(term, %i(tag_authors tag_texts), options),
      repository.fts_query(term, options)
    ]
  ).max_by(&:total)
end

Instance Method Details

#gradesObject



149
150
151
# File 'app/models/lcms/engine/search/document.rb', line 149

def grades
  @grades ||= Lcms::Engine::Grades.new(self)
end