elastic 0.2.0
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/Guardfile +40 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/elastic.gemspec +39 -0
- data/lib/elastic.rb +42 -0
- data/lib/elastic/buffer.rb +33 -0
- data/lib/elastic/client.rb +136 -0
- data/lib/elastic/client/error.rb +23 -0
- data/lib/elastic/configuration.rb +17 -0
- data/lib/elastic/helpers.rb +27 -0
- data/lib/elastic/index.rb +143 -0
- data/lib/elastic/scroll.rb +81 -0
- data/lib/elastic/version.rb +3 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 458e661226819bc4863e739ece55ec3ae137eed17f9cbf6e1cb88e1438fc897b
|
4
|
+
data.tar.gz: 3e5e1ce97570f1889f6ffae1aaf4b0f0d22b649a5b161a46ddc08fd936c54d72
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 12a887d50d3e0c8a2f7200bf27e0e52be01572237ee719988d6620bc9437090f18adb10edab55bcbb01080521480555be489910d2cddd72816e4c3cdca0025da
|
7
|
+
data.tar.gz: b82c32e97c2e4f8c283d615ca01ba29a25b41ff4369471276eab606ff08cf45d351f3c4a694ef3c7bdd09315193619a1539bca4a3284777032c51102e1a83ef1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
sudo: false
|
2
|
+
services:
|
3
|
+
- elasticsearch
|
4
|
+
language: ruby
|
5
|
+
rvm:
|
6
|
+
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- 2.7
|
9
|
+
before_install:
|
10
|
+
- curl https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-amd64.deb -o elasticsearch.deb
|
11
|
+
- sudo dpkg -i --force-confnew elasticsearch.deb
|
12
|
+
- sudo chown -R elasticsearch:elasticsearch /etc/default/elasticsearch
|
13
|
+
- sudo service elasticsearch restart
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# RSpec files
|
32
|
+
rspec = dsl.rspec
|
33
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
34
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
35
|
+
watch(rspec.spec_files)
|
36
|
+
|
37
|
+
# Ruby files
|
38
|
+
ruby = dsl.ruby
|
39
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
40
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Michał Szajbe
|
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,58 @@
|
|
1
|
+
# Elastic
|
2
|
+
|
3
|
+
Elasticsearch utilities built on top of official `elasticsearch` gem.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'elastic'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Configuration
|
14
|
+
|
15
|
+
Configure the lib in your application's initializers:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
Elastic.configure do |config|
|
19
|
+
config.namespace = "#{ENV['ELASTICSEARCH_NAMESPACE'] || "myapp-#{Rails.env}"
|
20
|
+
|
21
|
+
config.logger =
|
22
|
+
if !!(ENV['ELASTICSEARCH_LOGGER'] || Rails.env.development?)
|
23
|
+
(ENV['ELASTICSEARCH_LOGGER'] || Rails.logger)
|
24
|
+
end
|
25
|
+
|
26
|
+
config.clusters = {
|
27
|
+
default: ENV['ELASTICSEARCH_URL'],
|
28
|
+
|
29
|
+
# Optional, additional clusters
|
30
|
+
analytics: ENV['ELASTICSEARCH_ANALYTICS_CLUSTER_URL'],
|
31
|
+
search: ENV['ELASTICSEARCH_SEARCH_CLUSTER_URL']
|
32
|
+
}
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Elasticsearch compatibility
|
37
|
+
|
38
|
+
This library is compatible with Elasticsearch 7 and higher.
|
39
|
+
|
40
|
+
## Multi-cluster support
|
41
|
+
|
42
|
+
You can connect to any cluster defined in configuration by specifying its name, e.g. `Elastic.client(:search)`, or connect to a default one with `Elastic.client`.
|
43
|
+
|
44
|
+
## Documentation
|
45
|
+
|
46
|
+
TBD
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
See LICENSE.txt file.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "elastic"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/elastic.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "elastic/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "elastic"
|
8
|
+
spec.version = Elastic::VERSION
|
9
|
+
spec.authors = ["Michał Szajbe"]
|
10
|
+
spec.email = ["michal.szajbe@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Elasticsearch utilities}
|
13
|
+
spec.homepage = "https://github.com/sellektor/elastic-rb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_dependency "elasticsearch", "~> 7.0"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
spec.add_development_dependency "guard", "~> 2.14.0"
|
38
|
+
spec.add_development_dependency "guard-rspec", "~> 4.7.0"
|
39
|
+
end
|
data/lib/elastic.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "elastic/buffer"
|
2
|
+
require "elastic/client"
|
3
|
+
require "elastic/client/error"
|
4
|
+
require "elastic/configuration"
|
5
|
+
require "elastic/helpers"
|
6
|
+
require "elastic/index"
|
7
|
+
require "elastic/scroll"
|
8
|
+
require "elastic/version"
|
9
|
+
|
10
|
+
module Elastic extend self
|
11
|
+
attr_reader :configuration
|
12
|
+
@configuration = Configuration.new
|
13
|
+
|
14
|
+
def configure
|
15
|
+
@configuration = Configuration.new
|
16
|
+
yield(configuration)
|
17
|
+
end
|
18
|
+
|
19
|
+
def client(cluster = :default)
|
20
|
+
@clients ||= {}
|
21
|
+
|
22
|
+
unless @clients[cluster]
|
23
|
+
@clients[cluster] = Client.new(host: host(cluster), log: !!logger, logger: logger)
|
24
|
+
end
|
25
|
+
|
26
|
+
@clients[cluster]
|
27
|
+
end
|
28
|
+
|
29
|
+
def namespace
|
30
|
+
configuration.namespace
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def logger
|
36
|
+
configuration.logger
|
37
|
+
end
|
38
|
+
|
39
|
+
def host(cluster)
|
40
|
+
configuration.clusters[cluster] || configuration.clusters[:default]
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Elastic
|
2
|
+
class Buffer
|
3
|
+
DEFAULT_SIZE = 1_000
|
4
|
+
|
5
|
+
attr_reader :size, :queue
|
6
|
+
|
7
|
+
def initialize(size: DEFAULT_SIZE, &blk)
|
8
|
+
@size = size
|
9
|
+
@callback = blk
|
10
|
+
@queue = []
|
11
|
+
@lock = Mutex.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def <<(object)
|
15
|
+
@lock.synchronize do
|
16
|
+
@queue << object
|
17
|
+
flush! if @queue.size >= size
|
18
|
+
end
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def flush!
|
23
|
+
if @queue.any?
|
24
|
+
@callback.call(@queue) if @callback.is_a? Proc
|
25
|
+
@queue = []
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def any?
|
30
|
+
@queue.any?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require "elasticsearch"
|
2
|
+
|
3
|
+
module Elastic
|
4
|
+
class Client
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def initialize(connection_options = {})
|
8
|
+
@client = Elasticsearch::Client.new(connection_options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def indices(options = {})
|
12
|
+
options = options.merge(format: 'json')
|
13
|
+
execute { cat.indices(options) }
|
14
|
+
rescue => ex
|
15
|
+
ex.status == 404 ? [] : raise
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_index(options)
|
19
|
+
execute { indices.create(options) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete_index(options)
|
23
|
+
execute { indices.delete(options) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def refresh_index(options)
|
27
|
+
execute { indices.refresh(options) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def index_exists?(options)
|
31
|
+
execute { indices.exists?(options) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def alias_exists?(options)
|
35
|
+
execute { indices.exists_alias?(options) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_alias(options)
|
39
|
+
execute { indices.get_alias(options) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def resolve_alias(options)
|
43
|
+
if alias_exists?(options)
|
44
|
+
index_alias = get_alias(name: options[:name])
|
45
|
+
index_alias.keys
|
46
|
+
else
|
47
|
+
[]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def alias_index(options)
|
52
|
+
if alias_exists?(name: options[:name])
|
53
|
+
index_names = resolve_alias(name: options[:name])
|
54
|
+
|
55
|
+
actions = index_names.map do |index|
|
56
|
+
{ remove: { alias: options[:name], index: index } }
|
57
|
+
end
|
58
|
+
actions << { add: { alias: options[:name], index: options[:index] } }
|
59
|
+
|
60
|
+
execute { indices.update_aliases(body: { actions: actions }) }
|
61
|
+
else
|
62
|
+
execute { indices.put_alias(options) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def index_aliased?(options)
|
67
|
+
if alias_exists?(options)
|
68
|
+
index_alias = get_alias(options)
|
69
|
+
index_alias.has_key?(options[:index])
|
70
|
+
else
|
71
|
+
false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def bulk(data, options = {})
|
76
|
+
options = options.merge(body: data)
|
77
|
+
execute { bulk(options) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def bulk_operation(action, index, id, data = {})
|
81
|
+
metadata = {
|
82
|
+
_index: index,
|
83
|
+
_id: id,
|
84
|
+
}
|
85
|
+
|
86
|
+
metadata[:data] = data if data && !data.empty?
|
87
|
+
|
88
|
+
{ action.to_sym => metadata }
|
89
|
+
end
|
90
|
+
|
91
|
+
def mget(index, ids)
|
92
|
+
ids = Array(ids)
|
93
|
+
return [] if ids.empty?
|
94
|
+
|
95
|
+
docs = ids.map { |id| { _index: index, _id: id } }
|
96
|
+
|
97
|
+
options = {
|
98
|
+
index: index,
|
99
|
+
body: {
|
100
|
+
docs: docs
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
results = execute { mget(options) }
|
105
|
+
results['docs'].select { |doc| doc['found'] }
|
106
|
+
end
|
107
|
+
|
108
|
+
def search(*args)
|
109
|
+
execute { search(*args) }
|
110
|
+
end
|
111
|
+
|
112
|
+
def count(*args)
|
113
|
+
execute { count(*args) }
|
114
|
+
end
|
115
|
+
|
116
|
+
def scroll(*args)
|
117
|
+
execute { scroll(*args) }
|
118
|
+
end
|
119
|
+
|
120
|
+
def clear_scroll(*args)
|
121
|
+
execute { clear_scroll(*args) }
|
122
|
+
rescue
|
123
|
+
nil
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def execute(&blk)
|
129
|
+
begin
|
130
|
+
client.instance_eval(&blk)
|
131
|
+
rescue => ex
|
132
|
+
raise ex.extend(Error)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Elastic
|
4
|
+
class Client
|
5
|
+
module Error
|
6
|
+
def status
|
7
|
+
return @status if defined?(@status)
|
8
|
+
|
9
|
+
if captures = message.match(/^\[(?<status>\d+)\]/)
|
10
|
+
@status = captures[:status].to_i
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def response
|
15
|
+
return @response if defined?(@response)
|
16
|
+
|
17
|
+
if captures = message.match(/(?<response>\{.+\})$/)
|
18
|
+
@response = JSON.parse(captures[:response])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Elastic
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :namespace, :logger, :clusters
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@namespace = "elastic"
|
7
|
+
@clusters = {
|
8
|
+
default: ENV['ELASTICSEARCH_URL']
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
def namespace=(namespace)
|
13
|
+
raise ArgumentError.new("Namespace can't be blank") if namespace.to_s.empty?
|
14
|
+
@namespace = namespace
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Elastic
|
2
|
+
module Helpers extend self
|
3
|
+
def to_alias_name(klass)
|
4
|
+
underscore(demodulize(klass.to_s))
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def demodulize(str)
|
10
|
+
if i = str.rindex("::")
|
11
|
+
str[(i + 2)..-1]
|
12
|
+
else
|
13
|
+
str
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def underscore(camel_cased_word)
|
18
|
+
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
|
19
|
+
word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze)
|
20
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
|
21
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
|
22
|
+
word.tr!("-".freeze, "_".freeze)
|
23
|
+
word.downcase!
|
24
|
+
word
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
module Elastic
|
2
|
+
class Index
|
3
|
+
class << self
|
4
|
+
attr_accessor :client, :alias_name, :settings, :mappings
|
5
|
+
|
6
|
+
def inherited(klass)
|
7
|
+
klass.alias_name = Elastic::Helpers.to_alias_name(klass)
|
8
|
+
klass.settings = {}
|
9
|
+
klass.mappings = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def resolve
|
13
|
+
if index_name = client.resolve_alias(name: alias_name).first
|
14
|
+
new(index_name)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate_index_name
|
19
|
+
"#{alias_name}-#{Time.now.to_i}".downcase
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :index_name
|
24
|
+
|
25
|
+
def initialize(index_name = nil)
|
26
|
+
@index_name = (index_name || self.class.generate_index_name).downcase
|
27
|
+
end
|
28
|
+
|
29
|
+
def create
|
30
|
+
body = {
|
31
|
+
settings: self.class.settings,
|
32
|
+
mappings: self.class.mappings,
|
33
|
+
}
|
34
|
+
|
35
|
+
client.create_index(index: index_name, body: body)
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete
|
39
|
+
client.delete_index(index: index_name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def exists?
|
43
|
+
client.index_exists?(index: index_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
def promote
|
47
|
+
client.alias_index(name: alias_name, index: index_name)
|
48
|
+
end
|
49
|
+
|
50
|
+
def promoted?
|
51
|
+
client.index_aliased?(name: alias_name, index: index_name)
|
52
|
+
end
|
53
|
+
|
54
|
+
def refresh
|
55
|
+
@dirty = false
|
56
|
+
client.refresh_index(index: index_name)
|
57
|
+
end
|
58
|
+
|
59
|
+
def dirty?
|
60
|
+
!!@dirty
|
61
|
+
end
|
62
|
+
|
63
|
+
def count(query = {})
|
64
|
+
response = client.count(index: index_name, body: query)
|
65
|
+
response['count']
|
66
|
+
end
|
67
|
+
|
68
|
+
def get(id)
|
69
|
+
mget([id]).first
|
70
|
+
end
|
71
|
+
|
72
|
+
def mget(ids)
|
73
|
+
docs = client.mget(index_name, ids)
|
74
|
+
docs.map { |doc| source_with_id(doc) }
|
75
|
+
end
|
76
|
+
|
77
|
+
def documents(query = {}, options = {})
|
78
|
+
defaults = { body: query }
|
79
|
+
scroll_options = defaults.merge(options)
|
80
|
+
scroll = Scroll.new(client, index_name, scroll_options)
|
81
|
+
docs = scroll.each
|
82
|
+
|
83
|
+
docs.lazy.map { |doc| source_with_id(doc) }
|
84
|
+
end
|
85
|
+
|
86
|
+
def document_ids(query = {}, options = {})
|
87
|
+
defaults = {}
|
88
|
+
scroll_options = defaults.merge(options).merge(stored_fields: ['_id'])
|
89
|
+
docs = documents(query, scroll_options)
|
90
|
+
|
91
|
+
docs.lazy.map { |doc| doc['_id'] }
|
92
|
+
end
|
93
|
+
|
94
|
+
def bulk(action, id, data = {})
|
95
|
+
buffer << bulk_operation(action, id, data)
|
96
|
+
end
|
97
|
+
|
98
|
+
def bulk_operation(action, id, data = {})
|
99
|
+
metadata = {
|
100
|
+
_index: index_name,
|
101
|
+
_id: id,
|
102
|
+
retry_on_conflict: 3
|
103
|
+
}
|
104
|
+
|
105
|
+
if action.to_sym == :upsert
|
106
|
+
data ||= {}
|
107
|
+
data[:doc_as_upsert] = true
|
108
|
+
|
109
|
+
action = :update
|
110
|
+
end
|
111
|
+
|
112
|
+
metadata[:data] = data unless data.empty?
|
113
|
+
|
114
|
+
{ action.to_sym => metadata }
|
115
|
+
end
|
116
|
+
|
117
|
+
def buffer
|
118
|
+
@buffer ||=
|
119
|
+
Buffer.new do |operations|
|
120
|
+
@dirty = true
|
121
|
+
client.bulk(operations)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def alias_name
|
126
|
+
self.class.alias_name
|
127
|
+
end
|
128
|
+
|
129
|
+
def ==(other)
|
130
|
+
self.class == other.class && index_name == other.index_name
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def source_with_id(doc)
|
136
|
+
(doc['_source'] || {}).merge('_id' => doc['_id'])
|
137
|
+
end
|
138
|
+
|
139
|
+
def client
|
140
|
+
self.class.client
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Elastic
|
2
|
+
class Scroll
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(client, index_name, size: 1_000, scroll: '5m', body: {}, stored_fields: [])
|
6
|
+
@client = client
|
7
|
+
@index_name = index_name
|
8
|
+
@size = size
|
9
|
+
@scroll = scroll
|
10
|
+
@body = body
|
11
|
+
@stored_fields = stored_fields
|
12
|
+
end
|
13
|
+
|
14
|
+
def each
|
15
|
+
if block_given?
|
16
|
+
while (docs = next_page) && docs.any?
|
17
|
+
docs.each do |doc|
|
18
|
+
begin
|
19
|
+
yield(doc) if block_given?
|
20
|
+
rescue => ex
|
21
|
+
if defined?(Raven)
|
22
|
+
Raven.extra_context(document: doc)
|
23
|
+
end
|
24
|
+
raise ex
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
else
|
29
|
+
to_enum(:each)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def next_page
|
34
|
+
response =
|
35
|
+
if @scroll_id
|
36
|
+
@client.scroll(scroll_params)
|
37
|
+
else
|
38
|
+
@client.search(initial_params)
|
39
|
+
end
|
40
|
+
|
41
|
+
if response
|
42
|
+
@scroll_id = response['_scroll_id']
|
43
|
+
hits = response['hits']['hits']
|
44
|
+
|
45
|
+
clear! if !hits || hits.empty?
|
46
|
+
|
47
|
+
hits
|
48
|
+
else
|
49
|
+
clear!
|
50
|
+
end
|
51
|
+
|
52
|
+
rescue
|
53
|
+
clear!
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def initial_params
|
59
|
+
{
|
60
|
+
index: @index_name,
|
61
|
+
size: @size,
|
62
|
+
scroll: @scroll,
|
63
|
+
body: @body,
|
64
|
+
stored_fields: @stored_fields
|
65
|
+
}.delete_if { |_, v| !v || (v.respond_to?(:empty?) && v.empty?) }
|
66
|
+
end
|
67
|
+
|
68
|
+
def scroll_params
|
69
|
+
{
|
70
|
+
body: {
|
71
|
+
scroll_id: @scroll_id,
|
72
|
+
scroll: @scroll
|
73
|
+
}
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
def clear!
|
78
|
+
@client.clear_scroll(scroll_id: @scroll_id) if @scroll_id
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elastic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michał Szajbe
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: elasticsearch
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.14.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.14.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 4.7.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 4.7.0
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- michal.szajbe@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- Guardfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- elastic.gemspec
|
115
|
+
- lib/elastic.rb
|
116
|
+
- lib/elastic/buffer.rb
|
117
|
+
- lib/elastic/client.rb
|
118
|
+
- lib/elastic/client/error.rb
|
119
|
+
- lib/elastic/configuration.rb
|
120
|
+
- lib/elastic/helpers.rb
|
121
|
+
- lib/elastic/index.rb
|
122
|
+
- lib/elastic/scroll.rb
|
123
|
+
- lib/elastic/version.rb
|
124
|
+
homepage: https://github.com/sellektor/elastic-rb
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata:
|
128
|
+
allowed_push_host: https://rubygems.org
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubygems_version: 3.1.4
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Elasticsearch utilities
|
148
|
+
test_files: []
|