esse 0.0.3 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/exec/esse +3 -1
- data/lib/esse/backend/index/aliases.rb +8 -4
- data/lib/esse/backend/index/close.rb +6 -5
- data/lib/esse/backend/index/create.rb +20 -9
- data/lib/esse/backend/index/delete.rb +15 -14
- data/lib/esse/backend/index/documents.rb +2 -2
- data/lib/esse/backend/index/existance.rb +2 -3
- data/lib/esse/backend/index/open.rb +6 -5
- data/lib/esse/backend/index/refresh.rb +43 -0
- data/lib/esse/backend/index/reset.rb +33 -0
- data/lib/esse/backend/index/update.rb +37 -15
- data/lib/esse/backend/index.rb +18 -4
- data/lib/esse/backend/index_type/documents.rb +53 -42
- data/lib/esse/backend/index_type.rb +7 -2
- data/lib/esse/cli/event_listener.rb +87 -0
- data/lib/esse/cli/generate.rb +9 -4
- data/lib/esse/cli/index/base_operation.rb +76 -0
- data/lib/esse/cli/index/close.rb +26 -0
- data/lib/esse/cli/index/create.rb +26 -0
- data/lib/esse/cli/index/delete.rb +26 -0
- data/lib/esse/cli/index/import.rb +26 -0
- data/lib/esse/cli/index/open.rb +26 -0
- data/lib/esse/cli/index/reset.rb +26 -0
- data/lib/esse/cli/index/update_aliases.rb +32 -0
- data/lib/esse/cli/index/update_mapping.rb +33 -0
- data/lib/esse/cli/index/update_settings.rb +26 -0
- data/lib/esse/cli/index.rb +78 -2
- data/lib/esse/cli/templates/config.rb.erb +20 -0
- data/lib/esse/cli/templates/index.rb.erb +76 -11
- data/lib/esse/cli/templates/type_collection.rb.erb +41 -0
- data/lib/esse/cli/templates/{mappings.json → type_mappings.json} +0 -0
- data/lib/esse/cli/templates/type_serializer.rb.erb +23 -0
- data/lib/esse/cli.rb +75 -3
- data/lib/esse/cluster.rb +22 -6
- data/lib/esse/config.rb +39 -5
- data/lib/esse/core.rb +18 -36
- data/lib/esse/errors.rb +47 -0
- data/lib/esse/events/bus.rb +103 -0
- data/lib/esse/events/event.rb +64 -0
- data/lib/esse/events/publisher.rb +119 -0
- data/lib/esse/events.rb +49 -0
- data/lib/esse/index/backend.rb +2 -1
- data/lib/esse/index/base.rb +4 -6
- data/lib/esse/index/mappings.rb +2 -3
- data/lib/esse/index/settings.rb +7 -8
- data/lib/esse/index.rb +2 -1
- data/lib/esse/index_mapping.rb +2 -2
- data/lib/esse/index_setting.rb +8 -4
- data/lib/esse/index_type/actions.rb +2 -1
- data/lib/esse/index_type/backend.rb +2 -1
- data/lib/esse/index_type/mappings.rb +2 -2
- data/lib/esse/index_type.rb +6 -1
- data/lib/esse/logging.rb +19 -0
- data/lib/esse/object_document_mapper.rb +96 -0
- data/lib/esse/primitives/hash_utils.rb +40 -0
- data/lib/esse/primitives/hstring.rb +4 -3
- data/lib/esse/primitives/output.rb +64 -0
- data/lib/esse/primitives.rb +1 -0
- data/lib/esse/template_loader.rb +1 -1
- data/lib/esse/version.rb +1 -1
- data/lib/esse.rb +12 -3
- metadata +124 -21
- data/.gitignore +0 -12
- data/.rubocop.yml +0 -128
- data/CHANGELOG.md +0 -0
- data/Gemfile +0 -7
- data/Gemfile.lock +0 -60
- data/LICENSE.txt +0 -21
- data/README.md +0 -52
- data/Rakefile +0 -4
- data/bin/console +0 -22
- data/bin/setup +0 -8
- data/esse.gemspec +0 -39
- data/lib/esse/cli/templates/serializer.rb.erb +0 -14
- data/lib/esse/index_type/serializer.rb +0 -87
| @@ -7,26 +7,28 @@ module Esse | |
| 7 7 | 
             
                    # Resolve collection and index data
         | 
| 8 8 | 
             
                    #
         | 
| 9 9 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 10 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 10 11 | 
             
                    # @option [Hash] :context The collection context. This value will be passed as argument to the collection
         | 
| 11 12 | 
             
                    #   May be SQL condition or any other filter you have defined on the collection.
         | 
| 12 | 
            -
                    def import(context: {}, **options)
         | 
| 13 | 
            +
                    def import(context: {}, suffix: nil, **options)
         | 
| 13 14 | 
             
                      each_serialized_batch(context || {}) do |batch|
         | 
| 14 | 
            -
                        bulk(index: batch, **options)
         | 
| 15 | 
            +
                        bulk(index: batch, suffix: suffix, **options)
         | 
| 15 16 | 
             
                      end
         | 
| 16 17 | 
             
                    end
         | 
| 17 | 
            -
                     | 
| 18 | 
            +
                    alias_method :import!, :import
         | 
| 18 19 |  | 
| 19 20 | 
             
                    # Performs multiple indexing or delete operations in a single API call.
         | 
| 20 21 | 
             
                    # This reduces overhead and can greatly increase indexing speed.
         | 
| 21 22 | 
             
                    #
         | 
| 22 23 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 23 | 
            -
                    # @ | 
| 24 | 
            -
                    # @ | 
| 25 | 
            -
                    # @ | 
| 24 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 25 | 
            +
                    # @option [Array] :index list of serialized documents to be indexed(Optional)
         | 
| 26 | 
            +
                    # @option [Array] :delete list of serialized documents to be deleted(Optional)
         | 
| 27 | 
            +
                    # @option [Array] :create list of serialized documents to be created(Optional)
         | 
| 26 28 | 
             
                    # @return [Hash, nil] the elasticsearch response or nil if there is no data.
         | 
| 27 29 | 
             
                    #
         | 
| 28 30 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-bulk.html
         | 
| 29 | 
            -
                    def bulk(index: nil, delete: nil, create: nil, **options)
         | 
| 31 | 
            +
                    def bulk(index: nil, delete: nil, create: nil, suffix: nil, **options)
         | 
| 30 32 | 
             
                      body = []
         | 
| 31 33 | 
             
                      Array(index).each do |entry|
         | 
| 32 34 | 
             
                        id, data = Esse.doc_id!(entry)
         | 
| @@ -44,14 +46,14 @@ module Esse | |
| 44 46 | 
             
                      return if body.empty?
         | 
| 45 47 |  | 
| 46 48 | 
             
                      definition = {
         | 
| 47 | 
            -
                        index: index_name,
         | 
| 49 | 
            +
                        index: index_name(suffix: suffix),
         | 
| 48 50 | 
             
                        type: type_name,
         | 
| 49 51 | 
             
                        body: body,
         | 
| 50 52 | 
             
                      }.merge(options)
         | 
| 51 53 |  | 
| 52 54 | 
             
                      client.bulk(definition)
         | 
| 53 55 | 
             
                    end
         | 
| 54 | 
            -
                     | 
| 56 | 
            +
                    alias_method :bulk!, :bulk
         | 
| 55 57 |  | 
| 56 58 | 
             
                    # Adds a JSON document to the specified index and makes it searchable. If the document
         | 
| 57 59 | 
             
                    # already exists, updates the document and increments its version.
         | 
| @@ -59,32 +61,34 @@ module Esse | |
| 59 61 | 
             
                    #   UsersIndex::User.index(id: 1, body: { name: 'name' }) # { '_id' => 1, ...}
         | 
| 60 62 | 
             
                    #
         | 
| 61 63 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 62 | 
            -
                    # @ | 
| 63 | 
            -
                    # @ | 
| 64 | 
            +
                    # @option [String, Integer] :id The `_id` of the elasticsearch document
         | 
| 65 | 
            +
                    # @option [Hash] :body The JSON document that will be indexed (Required)
         | 
| 66 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 64 67 | 
             
                    # @return [Hash] the elasticsearch response Hash
         | 
| 65 68 | 
             
                    #
         | 
| 66 69 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-index_.html
         | 
| 67 | 
            -
                    def index(id:, body:, **options)
         | 
| 70 | 
            +
                    def index(id:, body:, suffix: nil, **options)
         | 
| 68 71 | 
             
                      client.index(
         | 
| 69 | 
            -
                         | 
| 72 | 
            +
                        index: index_name(suffix: suffix), type: type_name, id: id, body: body, **options
         | 
| 70 73 | 
             
                      )
         | 
| 71 74 | 
             
                    end
         | 
| 72 | 
            -
                     | 
| 75 | 
            +
                    alias_method :index!, :index
         | 
| 73 76 |  | 
| 74 77 | 
             
                    # Updates a document using the specified script.
         | 
| 75 78 | 
             
                    #
         | 
| 76 79 | 
             
                    #   UsersIndex::User.update!(id: 1, body: { doc: { ... } }) # { '_id' => 1, ...}
         | 
| 77 80 | 
             
                    #
         | 
| 78 81 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 79 | 
            -
                    # @ | 
| 80 | 
            -
                    # @ | 
| 82 | 
            +
                    # @option [String, Integer] :id The `_id` of the elasticsearch document
         | 
| 83 | 
            +
                    # @option [Hash] :body the body of the request
         | 
| 84 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 81 85 | 
             
                    # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when the doc does not exist
         | 
| 82 86 | 
             
                    # @return [Hash] elasticsearch response hash
         | 
| 83 87 | 
             
                    #
         | 
| 84 88 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html
         | 
| 85 | 
            -
                    def update!(id:, body:, **options)
         | 
| 89 | 
            +
                    def update!(id:, body:, suffix: nil, **options)
         | 
| 86 90 | 
             
                      client.update(
         | 
| 87 | 
            -
                         | 
| 91 | 
            +
                        index: index_name(suffix: suffix), type: type_name, id: id, body: body, **options
         | 
| 88 92 | 
             
                      )
         | 
| 89 93 | 
             
                    end
         | 
| 90 94 |  | 
| @@ -93,15 +97,16 @@ module Esse | |
| 93 97 | 
             
                    #   UsersIndex::User.update(id: 1, body: { doc: { ... } }) # { '_id' => 1, ...}
         | 
| 94 98 | 
             
                    #
         | 
| 95 99 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 96 | 
            -
                    # @ | 
| 97 | 
            -
                    # @ | 
| 98 | 
            -
                    # @ | 
| 100 | 
            +
                    # @option [String, Integer] :id The `_id` of the elasticsearch document
         | 
| 101 | 
            +
                    # @option [Hash] :body the body of the request
         | 
| 102 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 103 | 
            +
                    # @return [Hash] the elasticsearch response, or an hash with 'errors' as true in case of failure
         | 
| 99 104 | 
             
                    #
         | 
| 100 105 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html
         | 
| 101 | 
            -
                    def update(id:, body:, **options)
         | 
| 102 | 
            -
                      update!(id: id, body: body, **options)
         | 
| 106 | 
            +
                    def update(id:, body:, suffix: nil, **options)
         | 
| 107 | 
            +
                      update!(id: id, body: body, suffix: suffix, **options)
         | 
| 103 108 | 
             
                    rescue Elasticsearch::Transport::Transport::Errors::NotFound
         | 
| 104 | 
            -
                       | 
| 109 | 
            +
                      { 'errors' => true }
         | 
| 105 110 | 
             
                    end
         | 
| 106 111 |  | 
| 107 112 | 
             
                    # Removes a JSON document from the specified index.
         | 
| @@ -110,13 +115,14 @@ module Esse | |
| 110 115 | 
             
                    #   UsersIndex::User.delete!(id: 'missing') # raise Elasticsearch::Transport::Transport::Errors::NotFound
         | 
| 111 116 | 
             
                    #
         | 
| 112 117 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 113 | 
            -
                    # @ | 
| 118 | 
            +
                    # @option [String, Integer] :id The `_id` of the elasticsearch document
         | 
| 119 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 114 120 | 
             
                    # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when the doc does not exist
         | 
| 115 121 | 
             
                    # @return [Boolean] true when the operation is successfully completed
         | 
| 116 122 | 
             
                    #
         | 
| 117 123 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-delete.html
         | 
| 118 | 
            -
                    def delete!(id:, **options)
         | 
| 119 | 
            -
                      client.delete(options.merge(index: index_name, type: type_name, id: id))
         | 
| 124 | 
            +
                    def delete!(id:, suffix: nil, **options)
         | 
| 125 | 
            +
                      client.delete(options.merge(index: index_name(suffix: suffix), type: type_name, id: id))
         | 
| 120 126 | 
             
                    end
         | 
| 121 127 |  | 
| 122 128 | 
             
                    # Removes a JSON document from the specified index.
         | 
| @@ -125,13 +131,14 @@ module Esse | |
| 125 131 | 
             
                    #   UsersIndex::User.delete(id: 'missing') # false
         | 
| 126 132 | 
             
                    #
         | 
| 127 133 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 128 | 
            -
                    # @ | 
| 134 | 
            +
                    # @option [String, Integer] :id The `_id` of the elasticsearch document
         | 
| 135 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 129 136 | 
             
                    # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when the doc does not exist
         | 
| 130 137 | 
             
                    # @return [Boolean] true when the operation is successfully completed
         | 
| 131 138 | 
             
                    #
         | 
| 132 139 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-delete.html
         | 
| 133 | 
            -
                    def delete(id:, **options)
         | 
| 134 | 
            -
                      delete!(id: id, **options)
         | 
| 140 | 
            +
                    def delete(id:, suffix: nil, **options)
         | 
| 141 | 
            +
                      delete!(id: id, suffix: suffix, **options)
         | 
| 135 142 | 
             
                    rescue Elasticsearch::Transport::Transport::Errors::NotFound
         | 
| 136 143 | 
             
                      false
         | 
| 137 144 | 
             
                    end
         | 
| @@ -142,12 +149,13 @@ module Esse | |
| 142 149 | 
             
                    #   UsersIndex::User.count(body: { ... }) # 32
         | 
| 143 150 | 
             
                    #
         | 
| 144 151 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 145 | 
            -
                    # @ | 
| 152 | 
            +
                    # @option [Hash] :body A query to restrict the results specified with the Query DSL (optional)
         | 
| 153 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 146 154 | 
             
                    # @return [Integer] amount of documents found
         | 
| 147 155 | 
             
                    #
         | 
| 148 156 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-count.html
         | 
| 149 | 
            -
                    def count(**options)
         | 
| 150 | 
            -
                      response = client.count(options.merge(index: index_name, type: type_name))
         | 
| 157 | 
            +
                    def count(suffix: nil, **options)
         | 
| 158 | 
            +
                      response = client.count(options.merge(index: index_name(suffix: suffix), type: type_name))
         | 
| 151 159 | 
             
                      response['count']
         | 
| 152 160 | 
             
                    rescue Elasticsearch::Transport::Transport::Errors::NotFound
         | 
| 153 161 | 
             
                      0
         | 
| @@ -159,10 +167,11 @@ module Esse | |
| 159 167 | 
             
                    #   UsersIndex::User.exist?(id: 'missing') # false
         | 
| 160 168 | 
             
                    #
         | 
| 161 169 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 162 | 
            -
                    # @ | 
| 170 | 
            +
                    # @option [String, Integer] :id The `_id` of the elasticsearch document
         | 
| 171 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 163 172 | 
             
                    # @return [Boolean] true if the document exists
         | 
| 164 | 
            -
                    def exist?(id:, **options)
         | 
| 165 | 
            -
                      client.exists(options.merge(index: index_name, type: type_name, id: id))
         | 
| 173 | 
            +
                    def exist?(id:, suffix: nil, **options)
         | 
| 174 | 
            +
                      client.exists(options.merge(index: index_name(suffix: suffix), type: type_name, id: id))
         | 
| 166 175 | 
             
                    end
         | 
| 167 176 |  | 
| 168 177 | 
             
                    # Retrieves the specified JSON document from an index.
         | 
| @@ -171,13 +180,14 @@ module Esse | |
| 171 180 | 
             
                    #   UsersIndex::User.find!(id: 'missing') # raise Elasticsearch::Transport::Transport::Errors::NotFound
         | 
| 172 181 | 
             
                    #
         | 
| 173 182 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 174 | 
            -
                    # @ | 
| 183 | 
            +
                    # @option [String, Integer] :id The `_id` of the elasticsearch document
         | 
| 184 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 175 185 | 
             
                    # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] when the doc does not exist
         | 
| 176 186 | 
             
                    # @return [Hash] The elasticsearch document.
         | 
| 177 187 | 
             
                    #
         | 
| 178 188 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-get.html
         | 
| 179 | 
            -
                    def find!(id:, **options)
         | 
| 180 | 
            -
                      client.get(options.merge(index: index_name, type: type_name, id: id))
         | 
| 189 | 
            +
                    def find!(id:, suffix: nil, **options)
         | 
| 190 | 
            +
                      client.get(options.merge(index: index_name(suffix: suffix), type: type_name, id: id))
         | 
| 181 191 | 
             
                    end
         | 
| 182 192 |  | 
| 183 193 | 
             
                    # Retrieves the specified JSON document from an index.
         | 
| @@ -186,12 +196,13 @@ module Esse | |
| 186 196 | 
             
                    #   UsersIndex::User.find(id: 'missing') # nil
         | 
| 187 197 | 
             
                    #
         | 
| 188 198 | 
             
                    # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
         | 
| 189 | 
            -
                    # @ | 
| 199 | 
            +
                    # @option [String, Integer] :id The `_id` of the elasticsearch document
         | 
| 200 | 
            +
                    # @option [String, nil] :suffix The index suffix. Defaults to the nil.
         | 
| 190 201 | 
             
                    # @return [Hash, nil] The elasticsearch document
         | 
| 191 202 | 
             
                    #
         | 
| 192 203 | 
             
                    # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-get.html
         | 
| 193 | 
            -
                    def find(id:, **options)
         | 
| 194 | 
            -
                      find!(id: id, **options)
         | 
| 204 | 
            +
                    def find(id:, suffix: nil, **options)
         | 
| 205 | 
            +
                      find!(id: id, suffix: suffix, **options)
         | 
| 195 206 | 
             
                    rescue Elasticsearch::Transport::Transport::Errors::NotFound
         | 
| 196 207 | 
             
                      nil
         | 
| 197 208 | 
             
                    end
         | 
| @@ -11,8 +11,6 @@ module Esse | |
| 11 11 |  | 
| 12 12 | 
             
                  # Type delegators
         | 
| 13 13 | 
             
                  def_delegators :@index_type, :type_name, :each_serialized_batch, :serialize
         | 
| 14 | 
            -
                  # Index delegators
         | 
| 15 | 
            -
                  def_delegators :index_class, :index_name
         | 
| 16 14 |  | 
| 17 15 | 
             
                  def initialize(type)
         | 
| 18 16 | 
             
                    @index_type = type
         | 
| @@ -20,6 +18,13 @@ module Esse | |
| 20 18 |  | 
| 21 19 | 
             
                  protected
         | 
| 22 20 |  | 
| 21 | 
            +
                  def index_name(suffix: nil)
         | 
| 22 | 
            +
                    suffix = Hstring.new(suffix).underscore.presence
         | 
| 23 | 
            +
                    return index_class.index_name unless suffix
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                    [index_class.index_name, suffix].join('_')
         | 
| 26 | 
            +
                  end
         | 
| 27 | 
            +
             | 
| 23 28 | 
             
                  def index_class
         | 
| 24 29 | 
             
                    @index_type.index
         | 
| 25 30 | 
             
                  end
         | 
| @@ -0,0 +1,87 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative '../primitives'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                module EventListener
         | 
| 8 | 
            +
                  extend Output
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  module_function
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  def [](event_name)
         | 
| 13 | 
            +
                    method_name = Hstring.new(event_name).underscore.to_sym
         | 
| 14 | 
            +
                    return unless respond_to?(method_name)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    method(method_name)
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  def elasticsearch_create_index(event)
         | 
| 20 | 
            +
                    print_message '[%<runtime>s] Index %<name>s successfuly created',
         | 
| 21 | 
            +
                      name: colorize(event[:request][:index], :bold),
         | 
| 22 | 
            +
                      runtime: formatted_runtime(event[:runtime])
         | 
| 23 | 
            +
                    if (aliases = event.to_h.dig(:request, :body, :aliases)).is_a?(Hash)
         | 
| 24 | 
            +
                      print_message ' --> Aliases: %<aliases>s', aliases: aliases.keys.join(', ')
         | 
| 25 | 
            +
                    end
         | 
| 26 | 
            +
                  end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  def elasticsearch_delete_index(event)
         | 
| 29 | 
            +
                    print_message '[%<runtime>s] Index %<name>s successfuly deleted',
         | 
| 30 | 
            +
                      name: colorize(event[:request][:index], :bold),
         | 
| 31 | 
            +
                      runtime: formatted_runtime(event[:runtime])
         | 
| 32 | 
            +
                  end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                  def elasticsearch_close(event)
         | 
| 35 | 
            +
                    print_message '[%<runtime>s] Index %<name>s successfuly closed',
         | 
| 36 | 
            +
                      name: colorize(event[:request][:index], :bold),
         | 
| 37 | 
            +
                      runtime: formatted_runtime(event[:runtime])
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                  def elasticsearch_open(event)
         | 
| 41 | 
            +
                    print_message '[%<runtime>s] Index %<name>s successfuly opened',
         | 
| 42 | 
            +
                      name: colorize(event[:request][:index], :bold),
         | 
| 43 | 
            +
                      runtime: formatted_runtime(event[:runtime])
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                  def elasticsearch_update_mapping(event)
         | 
| 47 | 
            +
                    if event[:request][:type]
         | 
| 48 | 
            +
                      print_message '[%<runtime>s] Index %<name>s mapping for type %<type>s successfuly updated',
         | 
| 49 | 
            +
                        name: colorize(event[:request][:index], :bold),
         | 
| 50 | 
            +
                        type: event[:request][:type],
         | 
| 51 | 
            +
                        runtime: formatted_runtime(event[:runtime])
         | 
| 52 | 
            +
                    else
         | 
| 53 | 
            +
                      print_message '[%<runtime>s] Index %<name>s successfuly updated mapping',
         | 
| 54 | 
            +
                        name: colorize(event[:request][:index], :bold),
         | 
| 55 | 
            +
                        runtime: formatted_runtime(event[:runtime])
         | 
| 56 | 
            +
                    end
         | 
| 57 | 
            +
                  end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                  def elasticsearch_update_settings(event)
         | 
| 60 | 
            +
                    print_message '[%<runtime>s] Index %<name>s successfuly updated settings',
         | 
| 61 | 
            +
                      name: colorize(event[:request][:index], :bold),
         | 
| 62 | 
            +
                      runtime: formatted_runtime(event[:runtime])
         | 
| 63 | 
            +
                  end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                  def elasticsearch_update_aliases(event)
         | 
| 66 | 
            +
                    actions = event[:request][:body][:actions]
         | 
| 67 | 
            +
                    removed = actions.select { |a| a.key?(:remove) }
         | 
| 68 | 
            +
                    added = actions.select { |a| a.key?(:add) }
         | 
| 69 | 
            +
                    print_message '[%<runtime>s] Successfuly updated aliases:',
         | 
| 70 | 
            +
                      runtime: formatted_runtime(event[:runtime])
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                    removed.each do |action|
         | 
| 73 | 
            +
                      print_message '%<padding>s-> Index %<name>s removed from alias %<alias>s',
         | 
| 74 | 
            +
                        name: colorize(action[:remove][:index], :bold),
         | 
| 75 | 
            +
                        alias: colorize(action[:remove][:alias], :bold),
         | 
| 76 | 
            +
                        padding: runtime_padding(event[:runtime])
         | 
| 77 | 
            +
                    end
         | 
| 78 | 
            +
                    added.each do |action|
         | 
| 79 | 
            +
                      print_message '%<padding>s-> Index %<name>s added to alias %<alias>s',
         | 
| 80 | 
            +
                        name: colorize(action[:add][:index], :bold),
         | 
| 81 | 
            +
                        alias: colorize(action[:add][:alias], :bold),
         | 
| 82 | 
            +
                        padding: runtime_padding(event[:runtime])
         | 
| 83 | 
            +
                    end
         | 
| 84 | 
            +
                  end
         | 
| 85 | 
            +
                end
         | 
| 86 | 
            +
              end
         | 
| 87 | 
            +
            end
         | 
    
        data/lib/esse/cli/generate.rb
    CHANGED
    
    | @@ -7,7 +7,7 @@ require_relative 'base' | |
| 7 7 | 
             
            module Esse
         | 
| 8 8 | 
             
              module CLI
         | 
| 9 9 | 
             
                class Generate < Base
         | 
| 10 | 
            -
                  NAMESPACE_PATTERN_RE = %r{ | 
| 10 | 
            +
                  NAMESPACE_PATTERN_RE = %r{:|/|\\}i.freeze
         | 
| 11 11 |  | 
| 12 12 | 
             
                  def self.source_root
         | 
| 13 13 | 
             
                    File.dirname(__FILE__)
         | 
| @@ -17,11 +17,12 @@ module Esse | |
| 17 17 | 
             
                  def index(name, *types)
         | 
| 18 18 | 
             
                    ns_path = name.split(NAMESPACE_PATTERN_RE).tap(&:pop)
         | 
| 19 19 | 
             
                    @index_name = Hstring.new(name.to_s).modulize.sub(/Index$/, '') + 'Index'
         | 
| 20 | 
            +
                    @index_name = Hstring.new(@index_name)
         | 
| 20 21 | 
             
                    @types = types.map { |type| Hstring.new(type) }
         | 
| 21 22 | 
             
                    @base_class = base_index_class(*ns_path)
         | 
| 22 23 |  | 
| 23 24 | 
             
                    base_dir = Esse.config.indices_directory.join(*ns_path)
         | 
| 24 | 
            -
                    index_name =  | 
| 25 | 
            +
                    index_name = @index_name.demodulize.underscore.to_s
         | 
| 25 26 | 
             
                    template(
         | 
| 26 27 | 
             
                      'templates/index.rb.erb',
         | 
| 27 28 | 
             
                      base_dir.join("#{index_name}.rb"),
         | 
| @@ -29,13 +30,17 @@ module Esse | |
| 29 30 | 
             
                    @types.each do |type|
         | 
| 30 31 | 
             
                      @type = Hstring.new(type).underscore
         | 
| 31 32 | 
             
                      copy_file(
         | 
| 32 | 
            -
                        'templates/ | 
| 33 | 
            +
                        'templates/type_mappings.json',
         | 
| 33 34 | 
             
                        base_dir.join(index_name, 'templates', "#{@type}_mapping.json"),
         | 
| 34 35 | 
             
                      )
         | 
| 35 36 | 
             
                      template(
         | 
| 36 | 
            -
                        'templates/ | 
| 37 | 
            +
                        'templates/type_serializer.rb.erb',
         | 
| 37 38 | 
             
                        base_dir.join(index_name, 'serializers', "#{@type}_serializer.rb"),
         | 
| 38 39 | 
             
                      )
         | 
| 40 | 
            +
                      template(
         | 
| 41 | 
            +
                        'templates/type_collection.rb.erb',
         | 
| 42 | 
            +
                        base_dir.join(index_name, 'collections', "#{@type}_collection.rb"),
         | 
| 43 | 
            +
                      )
         | 
| 39 44 | 
             
                    end
         | 
| 40 45 | 
             
                  end
         | 
| 41 46 |  | 
| @@ -0,0 +1,76 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Esse
         | 
| 4 | 
            +
              module CLI
         | 
| 5 | 
            +
                class Index::BaseOperation
         | 
| 6 | 
            +
                  include Output
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def initialize(indices:, **options)
         | 
| 9 | 
            +
                    @indices = Array(indices)
         | 
| 10 | 
            +
                    @options = options
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  # @abstract
         | 
| 14 | 
            +
                  # @void
         | 
| 15 | 
            +
                  def run
         | 
| 16 | 
            +
                    raise NotImplementedError
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  private
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def validate_indices_option!
         | 
| 22 | 
            +
                    if @indices.empty?
         | 
| 23 | 
            +
                      raise InvalidOption.new(<<~END)
         | 
| 24 | 
            +
                        You must specify at least one index class.
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                        Example:
         | 
| 27 | 
            +
                          > esse index create CityIndex
         | 
| 28 | 
            +
                          > esse index create CityIndex, StateIndex
         | 
| 29 | 
            +
                      END
         | 
| 30 | 
            +
                    end
         | 
| 31 | 
            +
                  end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                  def indices
         | 
| 34 | 
            +
                    eager_load_indices!
         | 
| 35 | 
            +
                    @indices.map do |class_name|
         | 
| 36 | 
            +
                      const_exist = begin
         | 
| 37 | 
            +
                        Kernel.const_defined?(class_name)
         | 
| 38 | 
            +
                      rescue NameError
         | 
| 39 | 
            +
                        false
         | 
| 40 | 
            +
                      end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                      raise InvalidOption.new(<<~END, class_name: class_name) unless const_exist
         | 
| 43 | 
            +
                        Unrecognized index class: %<class_name>p. Are you sure you specified the correct index class?
         | 
| 44 | 
            +
                      END
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                      klass = Kernel.const_get(class_name)
         | 
| 47 | 
            +
                      unless klass < Esse::Index
         | 
| 48 | 
            +
                        path = Esse.config.indices_directory.join(Hstring.new(class_name).underscore.to_s)
         | 
| 49 | 
            +
                        raise InvalidOption.new(<<~END, class_name: class_name, path: path)
         | 
| 50 | 
            +
                          %<class_name>s must be a subclass of Esse::Index.
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                          Example:
         | 
| 53 | 
            +
                            # %<path>s.rb
         | 
| 54 | 
            +
                            class %<class_name>s < Esse::Index
         | 
| 55 | 
            +
                              # the index definition goes here
         | 
| 56 | 
            +
                            end
         | 
| 57 | 
            +
                        END
         | 
| 58 | 
            +
                      end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                      klass
         | 
| 61 | 
            +
                    end
         | 
| 62 | 
            +
                  end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                  def eager_load_indices!
         | 
| 65 | 
            +
                    return false unless Esse.config.indices_directory.exist?
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                    Esse.config.indices_directory.each_child do |path|
         | 
| 68 | 
            +
                      next unless path.extname == '.rb'
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                      require(path.expand_path.to_s)
         | 
| 71 | 
            +
                    end
         | 
| 72 | 
            +
                    true
         | 
| 73 | 
            +
                  end
         | 
| 74 | 
            +
                end
         | 
| 75 | 
            +
              end
         | 
| 76 | 
            +
            end
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative 'base_operation'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                class Index::Close < Index::BaseOperation
         | 
| 8 | 
            +
                  def run
         | 
| 9 | 
            +
                    validate_options!
         | 
| 10 | 
            +
                    indices.each do |index|
         | 
| 11 | 
            +
                      index.elasticsearch.close!(**options)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  private
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def options
         | 
| 18 | 
            +
                    @options.slice(*@options.keys - CLI_IGNORE_OPTS)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def validate_options!
         | 
| 22 | 
            +
                    validate_indices_option!
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative 'base_operation'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                class Index::Create < Index::BaseOperation
         | 
| 8 | 
            +
                  def run
         | 
| 9 | 
            +
                    validate_options!
         | 
| 10 | 
            +
                    indices.each do |index|
         | 
| 11 | 
            +
                      index.elasticsearch.create_index!(**options)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  private
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def options
         | 
| 18 | 
            +
                    @options.slice(*@options.keys - CLI_IGNORE_OPTS)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def validate_options!
         | 
| 22 | 
            +
                    validate_indices_option!
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative 'base_operation'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                class Index::Delete < Index::BaseOperation
         | 
| 8 | 
            +
                  def run
         | 
| 9 | 
            +
                    validate_options!
         | 
| 10 | 
            +
                    indices.each do |index|
         | 
| 11 | 
            +
                      index.elasticsearch.delete_index!(**options)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  private
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def options
         | 
| 18 | 
            +
                    @options.slice(*@options.keys - CLI_IGNORE_OPTS)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def validate_options!
         | 
| 22 | 
            +
                    validate_indices_option!
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative 'base_operation'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                class Index::Import < Index::BaseOperation
         | 
| 8 | 
            +
                  def run
         | 
| 9 | 
            +
                    validate_options!
         | 
| 10 | 
            +
                    indices.each do |index|
         | 
| 11 | 
            +
                      index.elasticsearch.import!(**options)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  private
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def options
         | 
| 18 | 
            +
                    @options.slice(*@options.keys - CLI_IGNORE_OPTS)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def validate_options!
         | 
| 22 | 
            +
                    validate_indices_option!
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative 'base_operation'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                class Index::Open < Index::BaseOperation
         | 
| 8 | 
            +
                  def run
         | 
| 9 | 
            +
                    validate_options!
         | 
| 10 | 
            +
                    indices.each do |index|
         | 
| 11 | 
            +
                      index.elasticsearch.open!(**options)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  private
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def options
         | 
| 18 | 
            +
                    @options.slice(*@options.keys - CLI_IGNORE_OPTS)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def validate_options!
         | 
| 22 | 
            +
                    validate_indices_option!
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative 'base_operation'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                class Index::Reset < Index::BaseOperation
         | 
| 8 | 
            +
                  def run
         | 
| 9 | 
            +
                    validate_options!
         | 
| 10 | 
            +
                    indices.each do |index|
         | 
| 11 | 
            +
                      index.elasticsearch.reset_index!(**options)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  private
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def options
         | 
| 18 | 
            +
                    @options.slice(*@options.keys - CLI_IGNORE_OPTS)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def validate_options!
         | 
| 22 | 
            +
                    validate_indices_option!
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative 'base_operation'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                class Index::UpdateAliases < Index::BaseOperation
         | 
| 8 | 
            +
                  def run
         | 
| 9 | 
            +
                    validate_options!
         | 
| 10 | 
            +
                    indices.each do |index|
         | 
| 11 | 
            +
                      index.elasticsearch.update_aliases!(**options)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  private
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def options
         | 
| 18 | 
            +
                    @options.slice(*@options.keys - CLI_IGNORE_OPTS)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def validate_options!
         | 
| 22 | 
            +
                    validate_indices_option!
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    if @options[:suffix].nil?
         | 
| 25 | 
            +
                      raise InvalidOption.new(<<~END)
         | 
| 26 | 
            +
                        You must specify a suffix to update the aliases.
         | 
| 27 | 
            +
                      END
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
            end
         | 
| @@ -0,0 +1,33 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require_relative 'base_operation'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module Esse
         | 
| 6 | 
            +
              module CLI
         | 
| 7 | 
            +
                class Index::UpdateMapping < Index::BaseOperation
         | 
| 8 | 
            +
                  def run
         | 
| 9 | 
            +
                    validate_options!
         | 
| 10 | 
            +
                    indices.each do |index|
         | 
| 11 | 
            +
                      if index.type_hash.any?
         | 
| 12 | 
            +
                        index.type_hash.each_value do |type|
         | 
| 13 | 
            +
                          # @idea Add update_mapping! to IndexType and use it here
         | 
| 14 | 
            +
                          index.elasticsearch.update_mapping!(type: type.type_name, **options)
         | 
| 15 | 
            +
                        end
         | 
| 16 | 
            +
                      else
         | 
| 17 | 
            +
                        index.elasticsearch.update_mapping!(**options)
         | 
| 18 | 
            +
                      end
         | 
| 19 | 
            +
                    end
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                  private
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                  def options
         | 
| 25 | 
            +
                    @options.slice(*@options.keys - CLI_IGNORE_OPTS)
         | 
| 26 | 
            +
                  end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  def validate_options!
         | 
| 29 | 
            +
                    validate_indices_option!
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
            end
         |