Sha256: 5aad762911870f0bf41b1efc5158e0ffecf76c0252e2f4b12d55d50cba1ff58b

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    module Search
      class ElasticSearchDocument
        # return the corresponding repository
        def self.repository
          @repository ||= Lcms::Engine::Search::Repository.new
        end

        def repository
          self.class.repository
        end

        def index!
          repository.save self
        end

        def delete!
          repository.delete self
        end

        # 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
        #
        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

        # this is necessary for the ActiveModel::ArraySerializer#as_json method to work
        # (used on the Pagination#serialize_with_pagination)
        # NOTE: https://github.com/rails-api/active_model_serializers/issues/891
        def read_attribute_for_serialization(key)
          if key.try(:to_sym) == :id
            attributes.fetch(key) { id }
          else
            attributes[key]
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lcms-engine-0.1.4 app/models/lcms/engine/search/elastic_search_document.rb
lcms-engine-0.1.3 app/models/lcms/engine/search/elastic_search_document.rb
lcms-engine-0.1.2 app/models/lcms/engine/search/elastic_search_document.rb
lcms-engine-0.1.0 app/models/lcms/engine/search/elastic_search_document.rb