es-elasticity 0.2.11 → 0.3.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 +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +5 -1
- data/elasticity.gemspec +1 -0
- data/lib/elasticity.rb +33 -5
- data/lib/elasticity/bulk.rb +53 -0
- data/lib/elasticity/config.rb +28 -0
- data/lib/elasticity/document.rb +89 -39
- data/lib/elasticity/instrumented_client.rb +35 -0
- data/lib/elasticity/log_subscriber.rb +49 -0
- data/lib/elasticity/multi_search.rb +22 -15
- data/lib/elasticity/railtie.rb +3 -17
- data/lib/elasticity/search.rb +190 -123
- data/lib/elasticity/strategies.rb +15 -0
- data/lib/elasticity/strategies/alias_index.rb +255 -0
- data/lib/elasticity/strategies/single_index.rb +97 -0
- data/lib/elasticity/version.rb +1 -1
- data/spec/functional/persistence_spec.rb +167 -0
- data/spec/rspec_config.rb +7 -5
- data/spec/units/document_spec.rb +25 -34
- data/spec/units/multi_search_spec.rb +11 -3
- data/spec/units/search_spec.rb +41 -31
- data/spec/units/{index_spec.rb → strategies/single_index_spec.rb} +7 -10
- metadata +27 -6
- data/lib/elasticity/index.rb +0 -118
- data/lib/elasticity_base.rb +0 -56
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
RSpec.describe Elasticity::Index, elasticsearch: true do
|
1
|
+
RSpec.describe Elasticity::Strategies::SingleIndex, elasticsearch: true do
|
4
2
|
subject do
|
5
3
|
described_class.new(Elasticity.config.client, "test_index_name")
|
6
4
|
end
|
@@ -25,7 +23,7 @@ RSpec.describe Elasticity::Index, elasticsearch: true do
|
|
25
23
|
subject.create(index_def)
|
26
24
|
expect(subject.mappings).to eq({"document"=>{"properties"=>{"name"=>{"type"=>"string"}}}})
|
27
25
|
|
28
|
-
subject.recreate
|
26
|
+
subject.recreate(index_def)
|
29
27
|
expect(subject.mappings).to eq({"document"=>{"properties"=>{"name"=>{"type"=>"string"}}}})
|
30
28
|
|
31
29
|
subject.delete
|
@@ -62,6 +60,7 @@ RSpec.describe Elasticity::Index, elasticsearch: true do
|
|
62
60
|
b.index "document", 2, name: "bar"
|
63
61
|
b.delete "document", 1
|
64
62
|
end
|
63
|
+
|
65
64
|
expect(results_b).to include("errors"=>false, "items"=>[{"index"=>{"_index"=>"test_index_name", "_type"=>"document", "_id"=>"2", "_version"=>1, "status"=>201}}, {"delete"=>{"_index"=>"test_index_name", "_type"=>"document", "_id"=>"1", "_version"=>2, "status"=>200, "found"=>true}}])
|
66
65
|
|
67
66
|
subject.flush
|
@@ -73,13 +72,11 @@ RSpec.describe Elasticity::Index, elasticsearch: true do
|
|
73
72
|
it "allows searching documents" do
|
74
73
|
subject.index_document("document", 1, name: "test")
|
75
74
|
subject.flush
|
76
|
-
results = subject.search("document", filter: { term: { name: "test" }})
|
77
|
-
|
78
|
-
expect(results["hits"]["total"]).to be 1
|
79
75
|
|
80
|
-
|
81
|
-
|
82
|
-
expect(
|
76
|
+
search = subject.search("document", filter: { term: { name: "test" }})
|
77
|
+
hashes = search.document_hashes
|
78
|
+
expect(hashes.size).to be 1
|
79
|
+
expect(hashes[0]).to eq("_id" => "1", "_index" => "test_index_name", "_type" => "document", "_score" => 1.0, "_source" => { "name" => "test" })
|
83
80
|
end
|
84
81
|
|
85
82
|
it "allows deleting by queryu" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: es-elasticity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Kochenburger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: redis
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
126
|
name: activesupport
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -170,18 +184,24 @@ files:
|
|
170
184
|
- bin/rspec
|
171
185
|
- elasticity.gemspec
|
172
186
|
- lib/elasticity.rb
|
187
|
+
- lib/elasticity/bulk.rb
|
188
|
+
- lib/elasticity/config.rb
|
173
189
|
- lib/elasticity/document.rb
|
174
|
-
- lib/elasticity/
|
190
|
+
- lib/elasticity/instrumented_client.rb
|
191
|
+
- lib/elasticity/log_subscriber.rb
|
175
192
|
- lib/elasticity/multi_search.rb
|
176
193
|
- lib/elasticity/railtie.rb
|
177
194
|
- lib/elasticity/search.rb
|
195
|
+
- lib/elasticity/strategies.rb
|
196
|
+
- lib/elasticity/strategies/alias_index.rb
|
197
|
+
- lib/elasticity/strategies/single_index.rb
|
178
198
|
- lib/elasticity/version.rb
|
179
|
-
-
|
199
|
+
- spec/functional/persistence_spec.rb
|
180
200
|
- spec/rspec_config.rb
|
181
201
|
- spec/units/document_spec.rb
|
182
|
-
- spec/units/index_spec.rb
|
183
202
|
- spec/units/multi_search_spec.rb
|
184
203
|
- spec/units/search_spec.rb
|
204
|
+
- spec/units/strategies/single_index_spec.rb
|
185
205
|
homepage: ''
|
186
206
|
licenses:
|
187
207
|
- MIT
|
@@ -207,9 +227,10 @@ signing_key:
|
|
207
227
|
specification_version: 4
|
208
228
|
summary: ActiveModel-based library for working with Elasticsearch
|
209
229
|
test_files:
|
230
|
+
- spec/functional/persistence_spec.rb
|
210
231
|
- spec/rspec_config.rb
|
211
232
|
- spec/units/document_spec.rb
|
212
|
-
- spec/units/index_spec.rb
|
213
233
|
- spec/units/multi_search_spec.rb
|
214
234
|
- spec/units/search_spec.rb
|
235
|
+
- spec/units/strategies/single_index_spec.rb
|
215
236
|
has_rdoc:
|
data/lib/elasticity/index.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
module Elasticity
|
2
|
-
class Index
|
3
|
-
attr_reader :name
|
4
|
-
|
5
|
-
def initialize(client, index_name)
|
6
|
-
@client = client
|
7
|
-
@name = index_name
|
8
|
-
end
|
9
|
-
|
10
|
-
def exists?
|
11
|
-
@client.indices.exists(index: @name)
|
12
|
-
end
|
13
|
-
|
14
|
-
def create(index_def)
|
15
|
-
args = { index: @name, body: index_def }
|
16
|
-
instrument("index_create", args) { @client.indices.create(args) }
|
17
|
-
end
|
18
|
-
|
19
|
-
def create_if_undefined(index_def)
|
20
|
-
create(index_def) unless exists?
|
21
|
-
end
|
22
|
-
|
23
|
-
def delete
|
24
|
-
args = { index: @name }
|
25
|
-
instrument("index_delete", args) { @client.indices.delete(args) }
|
26
|
-
end
|
27
|
-
|
28
|
-
def delete_if_defined
|
29
|
-
delete if exists?
|
30
|
-
end
|
31
|
-
|
32
|
-
def recreate(index_def = nil)
|
33
|
-
index_def ||= { settings: settings, mappings: mappings }
|
34
|
-
delete_if_defined
|
35
|
-
create(index_def)
|
36
|
-
end
|
37
|
-
|
38
|
-
def index_document(type, id, attributes)
|
39
|
-
args = { index: @name, type: type, id: id, body: attributes }
|
40
|
-
instrument("index_document", args) { @client.index(args) }
|
41
|
-
end
|
42
|
-
|
43
|
-
def delete_document(type, id)
|
44
|
-
args = { index: @name, type: type, id: id }
|
45
|
-
instrument("delete_document", args) { @client.delete(args) }
|
46
|
-
end
|
47
|
-
|
48
|
-
def get_document(type, id)
|
49
|
-
args = { index: @name, type: type, id: id }
|
50
|
-
instrument("get_document", args) { @client.get(args) }
|
51
|
-
end
|
52
|
-
|
53
|
-
def search(type, body)
|
54
|
-
args = { index: @name, type: type, body: body }
|
55
|
-
instrument("search", args) { @client.search(args) }
|
56
|
-
end
|
57
|
-
|
58
|
-
def delete_by_query(type, body)
|
59
|
-
args = { index: @name, type: type, body: body }
|
60
|
-
instrument("delete_by_query", args) { @client.delete_by_query(args) }
|
61
|
-
end
|
62
|
-
|
63
|
-
def bulk
|
64
|
-
b = Bulk.new(@client, @name)
|
65
|
-
yield b
|
66
|
-
b.execute
|
67
|
-
end
|
68
|
-
|
69
|
-
def settings
|
70
|
-
args = { index: @name }
|
71
|
-
settings = instrument("settings", args) { @client.indices.get_settings(args) }
|
72
|
-
settings[@name]["settings"] if settings[@name]
|
73
|
-
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
74
|
-
nil
|
75
|
-
end
|
76
|
-
|
77
|
-
def mappings
|
78
|
-
args = { index: @name }
|
79
|
-
mappings = instrument("mappings", args) { @client.indices.get_mapping(args) }
|
80
|
-
mappings[@name]["mappings"] if mappings[@name]
|
81
|
-
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
82
|
-
nil
|
83
|
-
end
|
84
|
-
|
85
|
-
def flush
|
86
|
-
args = { index: @name }
|
87
|
-
instrument("flush", args) { @client.indices.flush(args) }
|
88
|
-
end
|
89
|
-
|
90
|
-
private
|
91
|
-
|
92
|
-
def instrument(name, extra = {})
|
93
|
-
ActiveSupport::Notifications.instrument("#{name}.elasticity", args: extra, backtrace: caller(1)) do
|
94
|
-
yield
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
class Bulk
|
99
|
-
def initialize(client, name)
|
100
|
-
@client = client
|
101
|
-
@name = name
|
102
|
-
@operations = []
|
103
|
-
end
|
104
|
-
|
105
|
-
def index(type, id, attributes)
|
106
|
-
@operations << { index: { _index: @name, _type: type, _id: id, data: attributes }}
|
107
|
-
end
|
108
|
-
|
109
|
-
def delete(type, id)
|
110
|
-
@operations << { delete: { _index: @name, _type: type, _id: id }}
|
111
|
-
end
|
112
|
-
|
113
|
-
def execute
|
114
|
-
@client.bulk(body: @operations)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
data/lib/elasticity_base.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "bundler/setup"
|
3
|
-
Bundler.setup
|
4
|
-
|
5
|
-
require "active_support"
|
6
|
-
require "active_support/core_ext"
|
7
|
-
require "active_model"
|
8
|
-
require "elasticsearch"
|
9
|
-
|
10
|
-
if defined?(Rails)
|
11
|
-
require "elasticity/railtie"
|
12
|
-
end
|
13
|
-
|
14
|
-
module Elasticity
|
15
|
-
class Config
|
16
|
-
attr_writer :logger, :client, :settings, :namespace, :pretty_json
|
17
|
-
|
18
|
-
def logger
|
19
|
-
return @logger if defined?(@logger)
|
20
|
-
|
21
|
-
if defined?(Rails)
|
22
|
-
@logger = Rails.logger
|
23
|
-
else
|
24
|
-
@logger = Logger.new(STDOUT)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def client
|
29
|
-
return @client if defined?(@client)
|
30
|
-
@client = Elasticsearch::Client.new
|
31
|
-
end
|
32
|
-
|
33
|
-
def settings
|
34
|
-
return @settings if defined?(@settings)
|
35
|
-
@settings = {}
|
36
|
-
end
|
37
|
-
|
38
|
-
def namespace
|
39
|
-
@namespace
|
40
|
-
end
|
41
|
-
|
42
|
-
def pretty_json
|
43
|
-
@pretty_json || false
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def self.configure
|
48
|
-
@config = Config.new
|
49
|
-
yield(@config)
|
50
|
-
end
|
51
|
-
|
52
|
-
def self.config
|
53
|
-
return @config if defined?(@config)
|
54
|
-
@config = Config.new
|
55
|
-
end
|
56
|
-
end
|