esse 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rubocop.yml +128 -0
  4. data/CHANGELOG.md +0 -0
  5. data/Gemfile +7 -0
  6. data/Gemfile.lock +60 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +50 -0
  9. data/Rakefile +4 -0
  10. data/bin/console +22 -0
  11. data/bin/setup +8 -0
  12. data/esse.gemspec +39 -0
  13. data/exec/esse +9 -0
  14. data/lib/esse.rb +7 -0
  15. data/lib/esse/backend/index.rb +38 -0
  16. data/lib/esse/backend/index/aliases.rb +69 -0
  17. data/lib/esse/backend/index/create.rb +56 -0
  18. data/lib/esse/backend/index/delete.rb +38 -0
  19. data/lib/esse/backend/index/documents.rb +23 -0
  20. data/lib/esse/backend/index/existance.rb +23 -0
  21. data/lib/esse/backend/index/update.rb +19 -0
  22. data/lib/esse/backend/index_type.rb +32 -0
  23. data/lib/esse/backend/index_type/documents.rb +203 -0
  24. data/lib/esse/cli.rb +29 -0
  25. data/lib/esse/cli/base.rb +11 -0
  26. data/lib/esse/cli/generate.rb +51 -0
  27. data/lib/esse/cli/index.rb +14 -0
  28. data/lib/esse/cli/templates/index.rb.erb +59 -0
  29. data/lib/esse/cli/templates/mappings.json +6 -0
  30. data/lib/esse/cli/templates/serializer.rb.erb +14 -0
  31. data/lib/esse/cluster.rb +58 -0
  32. data/lib/esse/config.rb +73 -0
  33. data/lib/esse/core.rb +89 -0
  34. data/lib/esse/index.rb +21 -0
  35. data/lib/esse/index/actions.rb +10 -0
  36. data/lib/esse/index/backend.rb +13 -0
  37. data/lib/esse/index/base.rb +118 -0
  38. data/lib/esse/index/descendants.rb +17 -0
  39. data/lib/esse/index/inheritance.rb +18 -0
  40. data/lib/esse/index/mappings.rb +47 -0
  41. data/lib/esse/index/naming.rb +64 -0
  42. data/lib/esse/index/settings.rb +48 -0
  43. data/lib/esse/index/type.rb +31 -0
  44. data/lib/esse/index_mapping.rb +38 -0
  45. data/lib/esse/index_setting.rb +41 -0
  46. data/lib/esse/index_type.rb +10 -0
  47. data/lib/esse/index_type/actions.rb +11 -0
  48. data/lib/esse/index_type/backend.rb +13 -0
  49. data/lib/esse/index_type/mappings.rb +42 -0
  50. data/lib/esse/index_type/serializer.rb +87 -0
  51. data/lib/esse/primitives.rb +3 -0
  52. data/lib/esse/primitives/hstring.rb +85 -0
  53. data/lib/esse/template_loader.rb +46 -0
  54. data/lib/esse/types/mapping.rb +0 -0
  55. data/lib/esse/version.rb +5 -0
  56. metadata +215 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7f309243fb21b878b0d14b85c16a2d344dd595659d7972cadc7cd40c246ffd9a
4
+ data.tar.gz: 670f66a022b7fe568fe9f995c0d8cd9832d65da5a9bfb21490a4a7e222540023
5
+ SHA512:
6
+ metadata.gz: 99b6fa0017361c185cdf71c7505f6a37dcfc28127f04504496dc1fbdddf8b7ca4a2c06e6c2e56dd30464c90f3b2012fc982bcdb3ed5159a1b331bfbe3c2e30d1
7
+ data.tar.gz: db75895be7643f9bbedb9c81c071b4438e1f277637b2447dd7d8444c719820c41a97bdf69f0230e422a60412b2de93b674d2e459d0a324cb21bd49cbf2763f91
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /.rspec_status
9
+ /tmp/
10
+ esse-*.gem
11
+ /app
12
+ /.env
data/.rubocop.yml ADDED
@@ -0,0 +1,128 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/**/*'
4
+ - 'log/**/*'
5
+ - 'tmp/**/*'
6
+
7
+ Layout/HashAlignment:
8
+ Enabled: true
9
+ EnforcedLastArgumentHashStyle: always_ignore
10
+
11
+ Layout/ParameterAlignment:
12
+ EnforcedStyle: with_fixed_indentation
13
+
14
+ Layout/CaseIndentation:
15
+ EnforcedStyle: end
16
+
17
+ Layout/FirstHashElementIndentation:
18
+ Enabled: false
19
+
20
+ Layout/MultilineMethodCallIndentation:
21
+ EnforcedStyle: indented
22
+
23
+ Layout/MultilineOperationIndentation:
24
+ EnforcedStyle: indented
25
+
26
+ Layout/SpaceInsideHashLiteralBraces:
27
+ Enabled: false
28
+
29
+ Metrics/AbcSize:
30
+ Max: 20
31
+
32
+ Metrics/BlockLength:
33
+ Exclude:
34
+ - '**/*.rake'
35
+ - 'spec/**/*.rb'
36
+
37
+ Metrics/ClassLength:
38
+ Max: 100
39
+
40
+ Layout/LineLength:
41
+ Max: 130
42
+
43
+ Metrics/MethodLength:
44
+ CountComments: false
45
+ Max: 20
46
+
47
+ Metrics/ModuleLength:
48
+ Max: 100
49
+
50
+ Style/AsciiComments:
51
+ Enabled: false
52
+
53
+ Style/BlockDelimiters:
54
+ Exclude:
55
+ - spec/**/*_spec.rb
56
+
57
+ Style/TrailingCommaInHashLiteral:
58
+ EnforcedStyleForMultiline: consistent_comma
59
+
60
+ Layout/SpaceAroundMethodCallOperator:
61
+ Enabled: true
62
+
63
+ Lint/RaiseException:
64
+ Enabled: true
65
+
66
+ Lint/StructNewOverride:
67
+ Enabled: true
68
+
69
+ Style/ExponentialNotation:
70
+ Enabled: true
71
+
72
+ Style/HashEachMethods:
73
+ Enabled: true
74
+
75
+ Style/HashTransformKeys:
76
+ Enabled: true
77
+
78
+ Style/HashTransformValues:
79
+ Enabled: true
80
+
81
+ Style/CollectionMethods:
82
+ Enabled: true
83
+ PreferredMethods:
84
+ collect: map
85
+ collect!: map!
86
+ detect: find
87
+ find_all: select
88
+ inject: reduce
89
+ length: size
90
+
91
+ Style/Documentation:
92
+ Enabled: false
93
+
94
+ Style/FormatString:
95
+ EnforcedStyle: format
96
+
97
+ Style/StringLiterals:
98
+ EnforcedStyle: single_quotes
99
+
100
+ Style/GuardClause:
101
+ Enabled: false
102
+
103
+ Style/IfUnlessModifier:
104
+ Enabled: false
105
+
106
+ Style/Lambda:
107
+ Enabled: false
108
+
109
+ Style/RaiseArgs:
110
+ Enabled: false
111
+
112
+ Style/SignalException:
113
+ Enabled: false
114
+
115
+ Style/SingleLineBlockParams:
116
+ Enabled: false
117
+
118
+ Style/TrailingCommaInArguments:
119
+ EnforcedStyleForMultiline: consistent_comma
120
+
121
+ Style/TrivialAccessors:
122
+ AllowPredicates: true
123
+
124
+ Style/WordArray:
125
+ Enabled: false
126
+
127
+ Layout/EndAlignment:
128
+ EnforcedStyleAlignWith: variable
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ esse (0.0.1)
5
+ elasticsearch
6
+ multi_json
7
+ thor (>= 0.19)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ awesome_print (1.8.0)
13
+ coderay (1.1.2)
14
+ diff-lcs (1.3)
15
+ dotenv (2.7.5)
16
+ elasticsearch (7.6.0)
17
+ elasticsearch-api (= 7.6.0)
18
+ elasticsearch-transport (= 7.6.0)
19
+ elasticsearch-api (7.6.0)
20
+ multi_json
21
+ elasticsearch-transport (7.6.0)
22
+ faraday (~> 1)
23
+ multi_json
24
+ faraday (1.0.1)
25
+ multipart-post (>= 1.2, < 3)
26
+ method_source (0.9.1)
27
+ multi_json (1.14.1)
28
+ multipart-post (2.1.1)
29
+ pry (0.12.0)
30
+ coderay (~> 1.1.0)
31
+ method_source (~> 0.9.0)
32
+ rake (10.5.0)
33
+ rspec (3.9.0)
34
+ rspec-core (~> 3.9.0)
35
+ rspec-expectations (~> 3.9.0)
36
+ rspec-mocks (~> 3.9.0)
37
+ rspec-core (3.9.1)
38
+ rspec-support (~> 3.9.1)
39
+ rspec-expectations (3.9.1)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-mocks (3.9.1)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-support (3.9.2)
46
+ thor (1.0.1)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ awesome_print
53
+ dotenv
54
+ esse!
55
+ pry
56
+ rake (~> 10.0)
57
+ rspec (~> 3.0)
58
+
59
+ BUNDLED WITH
60
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Marcos G. Zimmermann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # esse
2
+
3
+ Simple and efficient way to organize queries/mapping/indices/tasks based on the official elasticsearch-ruby.
4
+
5
+ ## Why to use it?
6
+ Some facts to use this library:
7
+
8
+ ### Don't spend time learning our DLS
9
+ You don't need to spend time learning our DSL or gem usage to start using it. All you need know is the elasticsearch syntax. You are free to build your queries/mappings/settings using JSON/RubyHash flexibility. And keeping simple any elasticsearch upgrade and its syntax changes.
10
+
11
+ ### Multiple ElasticSearch Versions
12
+ You can use multiple elasticsearch servers with different versions in an elegant way. Take a look at [LINK TO TOPIC](#anchors-id-here) for more details.
13
+
14
+ ### It's pure Ruby
15
+ Yeah!! Nor [activesupport](http://github.com/rails/rails/tree/master/activesupport) dependency and all its monkey patchings. But if you are using rails, suggest install `esse-rails` extension that makes things even easier. Use the [Get started with esse-rails](#anchors-id-here) for more details.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'esse'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle install
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install esse
32
+
33
+ ## Usage
34
+
35
+ TODO: Write usage instructions here
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcosgz/esse.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'dotenv/load'
5
+ require 'esse'
6
+ require 'pry'
7
+ require 'awesome_print'
8
+
9
+ Esse.config.clusters.client = { url: ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200') }
10
+ Esse.config.clusters.index_prefix = 'esse_console'
11
+
12
+ class ApplicationIndex < Esse::Index
13
+ self.abstract_class = true
14
+ end
15
+ class GeosIndex < ApplicationIndex
16
+ self.index_version = 'v1'
17
+
18
+ define_type :county
19
+ define_type :city
20
+ end
21
+
22
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/esse.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/esse/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'esse'
7
+ spec.version = Esse::VERSION
8
+ spec.authors = ['Marcos G. Zimmermann']
9
+ spec.email = ['mgzmaster@gmail.com']
10
+
11
+ spec.summary = %[Pure Ruby toolkit based on official elasticsearch-ruby library. (No! It isn't a new DSL)]
12
+ spec.description = 'All the elegance of ruby with the elasticsearch flexibility. This gem is a pretty simple ' \
13
+ 'but excential helpers to deal with mapping, indexing, serialization and search.'
14
+ spec.homepage = 'https://github.com/marcosgz/esse'
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/marcosgz/esse'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/marcosgz/esse/blob/master/CHANGELOG.md'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'elasticsearch'
32
+ spec.add_dependency 'multi_json'
33
+ spec.add_dependency 'thor', '>= 0.19'
34
+ spec.add_development_dependency 'dotenv'
35
+ spec.add_development_dependency 'awesome_print'
36
+ spec.add_development_dependency 'pry'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.0'
39
+ end
data/exec/esse ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
5
+
6
+ require 'esse'
7
+ require 'esse/cli'
8
+
9
+ Esse::CLI.start(ARGV)
data/lib/esse.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'esse/index'
4
+
5
+ module Esse
6
+ class Error < StandardError; end
7
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Esse
6
+ module Backend
7
+ class Index
8
+ require_relative 'index/aliases'
9
+ require_relative 'index/create'
10
+ require_relative 'index/delete'
11
+ require_relative 'index/existance'
12
+ require_relative 'index/update'
13
+ require_relative 'index/documents'
14
+
15
+ extend Forwardable
16
+
17
+ NAMING = %i[index_name index_version].freeze
18
+ DEFINITION = %i[settings_hash mappings_hash].freeze
19
+
20
+ def_delegators :@index, :type_hash, *(NAMING + DEFINITION)
21
+
22
+ def initialize(index)
23
+ @index = index
24
+ end
25
+
26
+ protected
27
+
28
+ def real_index_name(suffix = nil)
29
+ suffix = Hstring.new(suffix).underscore.presence || index_version || Esse.timestamp
30
+ [index_name, suffix].compact.join('_')
31
+ end
32
+
33
+ def client
34
+ @index.cluster.client
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esse
4
+ module Backend
5
+ class Index
6
+ module InstanceMethods
7
+ # Return a list of index aliases.
8
+ #
9
+ # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
10
+ # @param options [Array] :index list of serialized documents to be indexed(Optional)
11
+ #
12
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/7.5/indices-aliases.html
13
+ def aliases(**options)
14
+ response = client.indices.get_alias({ index: index_name, name: '*' }.merge(options))
15
+ idx_name = response.keys.find { |idx| idx.start_with?(index_name) }
16
+ return [] unless idx_name
17
+
18
+ response.dig(idx_name, 'aliases')&.keys || []
19
+ rescue Elasticsearch::Transport::Transport::Errors::NotFound
20
+ []
21
+ end
22
+
23
+ # Returns a list of indices.
24
+ #
25
+ # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
26
+ # @return [Array] list of indices that match with `index_name`.
27
+ def indices(**options)
28
+ client.indices.get_alias({ name: index_name }.merge(options)).keys
29
+ rescue Elasticsearch::Transport::Transport::Errors::NotFound
30
+ []
31
+ end
32
+
33
+ # Replaces all existing aliases by the respective suffixed index from argument.
34
+ #
35
+ # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
36
+ # @option [String] :suffix The suffix of the index used for versioning.
37
+ # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] in case of failure
38
+ # @return [Hash] the elasticsearch response
39
+ def update_aliases!(suffix:, **options)
40
+ raise(ArgumentError, 'index suffix cannot be nil') if suffix.nil?
41
+
42
+ options[:body] = {
43
+ actions: [
44
+ *indices.map do |index|
45
+ { remove: { index: index, alias: index_name } }
46
+ end,
47
+ { add: {index: real_index_name(suffix), alias: index_name } }
48
+ ],
49
+ }
50
+ client.indices.update_aliases(options)
51
+ end
52
+
53
+ # Replaces all existing aliases by the respective suffixed index from argument.
54
+ #
55
+ # @param options [Hash] Hash of paramenters that will be passed along to elasticsearch request
56
+ # @option [String] :suffix The suffix of the index used for versioning.
57
+ # @raise [Elasticsearch::Transport::Transport::Errors::NotFound] in case of failure
58
+ # @return [Hash, false] the elasticsearch response, or false in case of failure
59
+ def update_aliases(suffix:, **options)
60
+ update_aliases!(suffix: suffix, **options)
61
+ rescue Elasticsearch::Transport::Transport::Errors::NotFound
62
+ false
63
+ end
64
+ end
65
+
66
+ include InstanceMethods
67
+ end
68
+ end
69
+ end