Class: Lcms::Engine::Search::ElasticSearchDocument

Inherits:
Object
  • Object
show all
Defined in:
app/models/lcms/engine/search/elastic_search_document.rb

Direct Known Subclasses

Document

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.repositoryObject

return the corresponding repository



8
9
10
# File 'app/models/lcms/engine/search/elastic_search_document.rb', line 8

def self.repository
  @repository ||= Lcms::Engine::Search::Repository.new
end

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

Default search

term: [String || Nil]

- [Nil]    : return a match_all query
- [String] : perform a `fts_query` on the repository

options: [Hash]

- per_page : number os results per page
- page     : results page number
- <filter> : any doc specific filters, e.g: 'grade', 'subject', etc


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

def self.search(term, options = {})
  return [] unless repository.index_exists?

  query = if term.present?
            repository.fts_query(term, options)
          else
            repository.all_query(options)
          end
  repository.search query
end

Instance Method Details

#delete!Object



20
21
22
# File 'app/models/lcms/engine/search/elastic_search_document.rb', line 20

def delete!
  repository.delete self
end

#index!Object



16
17
18
# File 'app/models/lcms/engine/search/elastic_search_document.rb', line 16

def index!
  repository.save self
end

#read_attribute_for_serialization(key) ⇒ Object

this is necessary for the ActiveModel::Serializer::CollectionSerializer#as_json method to work (used on the Pagination#serialize_with_pagination) NOTE: github.com/rails-api/active_model_serializers/issues/891



49
50
51
52
53
54
55
# File 'app/models/lcms/engine/search/elastic_search_document.rb', line 49

def read_attribute_for_serialization(key)
  if key.try(:to_sym) == :id
    attributes.fetch(key) { id }
  else
    attributes[key]
  end
end

#repositoryObject



12
13
14
# File 'app/models/lcms/engine/search/elastic_search_document.rb', line 12

def repository
  self.class.repository
end