Class: Lcms::Engine::Search::Document
- Inherits:
-
ElasticSearchDocument
- Object
- ElasticSearchDocument
- Lcms::Engine::Search::Document
- 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
- .build_from(model) ⇒ Object
- .doc_type(model) ⇒ Object
- .document_metadata(model) ⇒ Object
-
.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.
- .resource_position(model) ⇒ Object
-
.search(term, options = {}) ⇒ Object
Overrides ElasticSearchDocument.search to include standards search.
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 |
# File 'app/models/lcms/engine/search/document.rb', line 33 def build_from(model) if model.is_a?(Lcms::Engine::Resource) new(**attrs_from_resource(model)) elsif model.is_a?(Lcms::Engine::ExternalPage) new(**attrs_from_page(model)) else raise "Unsupported Type for Search : #{model.class.name}" end end |
.doc_type(model) ⇒ Object
45 46 47 |
# File 'app/models/lcms/engine/search/document.rb', line 45 def doc_type(model) model.resource_type == 'resource' ? model.curriculum_type : model.resource_type end |
.document_metadata(model) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'app/models/lcms/engine/search/document.rb', line 49 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)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/lcms/engine/search/document.rb', line 67 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
83 84 85 86 87 88 89 |
# File 'app/models/lcms/engine/search/document.rb', line 83 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
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/lcms/engine/search/document.rb', line 92 def search(term, = {}) return repository.empty_response unless repository.index_exists? return repository.search(repository.all_query()) unless term.present? repository.multisearch( [ repository.standards_query(term, ), repository.(term, [:tag_keywords], ), repository.(term, %i(tag_authors tag_texts), ), repository.fts_query(term, ) ] ).max_by(&:total) end |