Module: Elasticsearch::Persistence::Model

Extended by:
ActiveSupport::Autoload
Defined in:
lib/elasticsearch/persistence/model.rb,
lib/elasticsearch/persistence.rb,
lib/elasticsearch/persistence/model/base.rb,
lib/elasticsearch/persistence/model/find.rb,
lib/elasticsearch/persistence/model/rails.rb,
lib/elasticsearch/persistence/model/store.rb,
lib/elasticsearch/persistence/model/errors.rb,
lib/elasticsearch/persistence/model/callbacks.rb,
lib/elasticsearch/persistence/model/hash_wrapper.rb,
lib/elasticsearch/persistence/model/gateway_delegation.rb

Overview

When included, extends a plain Ruby class with persistence-related features via the ActiveRecord pattern

Examples:

Include the repository in a custom class


require 'elasticsearch/persistence/model'

class MyObject
  include Elasticsearch::Persistence::Repository
end

Defined Under Namespace

Modules: Base, Callbacks, Find, GatewayDelegation, Rails, Store, Utils Classes: DocumentNotPersisted, DocumentNotSaved, HashWrapper, QueryOptionMissing

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/elasticsearch/persistence/model.rb', line 29

def self.included(base)
  base.class_eval do
    include ActiveModel::Naming
    include ActiveModel::Conversion
    include ActiveModel::Serialization
    include ActiveModel::Serializers::JSON
    include ActiveModel::Validations
    include ActiveModel::Validations::Callbacks

    include Virtus.model
    extend ActiveModel::Callbacks

    define_model_callbacks :create, :save, :update, :destroy
    define_model_callbacks :find, :touch, only: :after

    include Elasticsearch::Persistence::Model::Callbacks

    include Elasticsearch::Persistence::Model::Base::InstanceMethods

    extend Elasticsearch::Persistence::Model::Store::ClassMethods
    include Elasticsearch::Persistence::Model::Store::InstanceMethods

    extend Elasticsearch::Persistence::Model::GatewayDelegation

    extend Elasticsearch::Persistence::Model::Find::ClassMethods
    extend Elasticsearch::Persistence::Querying
    extend Elasticsearch::Persistence::Inheritence
    extend Elasticsearch::Persistence::Delegation::DelegateCache

    include Elasticsearch::Persistence::Scoping

    class << self

      # Re-define the Virtus' `attribute` method, to configure Elasticsearch mapping as well
      #
      def attribute(name, type = nil, options = {}, &block)
        mapping = options.delete(:mapping) || {}

        if type == :keyword || type.nil?
          type = String
          mapping = { type: "keyword" }.merge(mapping)
        end

        super

        gateway.mapping do
          indexes name, { type: Utils::lookup_type(type) }.merge(mapping)
        end

        gateway.mapping(&block) if block_given?
      end

      # Return the {Repository::Class} instance
      #
      def gateway(&block)
        @gateway ||= Elasticsearch::Persistence::Repository::Class.new host: self
        block.arity < 1 ? @gateway.instance_eval(&block) : block.call(@gateway) if block_given?
        @gateway
      end

      # Set the default sort key to be used in sort operations
      #
      def default_sort_key(field = nil)
        @default_sort_key = field unless field.nil?
        @default_sort_key
      end

      private

      # Return a Relation instance to chain queries
      #
      def relation
        Relation.create(self, {})
      end
    end

    # Configure the repository based on the model (set up index_name, etc)
    #
    gateway do
      klass base
      index_name base.model_name.collection.gsub(/\//, "-")
      document_type base.model_name.element

      def serialize(document)
        document.to_hash.except(:id, "id")
      end

      def deserialize(document)
        object = klass.new document["_source"] || document["fields"]

        # Set the meta attributes when fetching the document from Elasticsearch
        #
        object.instance_variable_set :@_id, document["_id"]
        object.instance_variable_set :@_index, document["_index"]
        object.instance_variable_set :@_type, document["_type"]
        object.instance_variable_set :@_version, document["_version"]

        # Store the "hit" information (highlighting, score, ...)
        #
        object.instance_variable_set :@hit,
                                     HashWrapper.new(document.except("_index", "_type", "_id", "_version", "_source"))

        object.instance_variable_set(:@persisted, true)
        object
      end
    end

    # Set up common attributes
    #
    attribute :created_at, DateTime, default: lambda { |o, a| Time.now.utc }
    attribute :updated_at, DateTime, default: lambda { |o, a| Time.now.utc }

    default_sort_key :created_at

    attr_reader :hit
  end
end