elastic_search_framework 1.4.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/elastic_search_framework.rb +1 -0
- data/lib/elastic_search_framework/index.rb +23 -20
- data/lib/elastic_search_framework/index_alias.rb +146 -0
- data/lib/elastic_search_framework/repository.rb +10 -7
- data/lib/elastic_search_framework/version.rb +1 -1
- data/spec/elastic_search_framework/index_alias_spec.rb +144 -0
- data/spec/elastic_search_framework/index_spec.rb +41 -4
- data/spec/elastic_search_framework/repository_spec.rb +34 -16
- data/spec/example_index.rb +8 -5
- data/spec/example_index_2.rb +31 -0
- data/spec/example_index_alias.rb +17 -0
- data/spec/example_index_alias_2.rb +8 -0
- data/spec/spec_helper.rb +3 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cb32d84eb11d74afb4b32b129fc7b6fb3654714ffc8cc68a22837641437c92a
|
4
|
+
data.tar.gz: bddf4744857f12fcb44272813c6c644b373c32029c46ae470f17b239e3279762
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7606703ced3bd3bf8e47312e408a687c45fdee2362a19d92cd007782b2748d9b1fc5bfa8d998d67040884697236c976d7c128eb4021173a758a713b9b2e725d
|
7
|
+
data.tar.gz: 721beafb796c691392c037b4651f3f51ea29ab24ce4d1b047fb8ade55887356c15cd4fb85af807125364382c5c387467ef466c23526f037ae06b6378354b287b
|
@@ -8,6 +8,7 @@ require_relative 'elastic_search_framework/logger'
|
|
8
8
|
require_relative 'elastic_search_framework/exceptions'
|
9
9
|
require_relative 'elastic_search_framework/repository'
|
10
10
|
require_relative 'elastic_search_framework/index'
|
11
|
+
require_relative 'elastic_search_framework/index_alias'
|
11
12
|
require_relative 'elastic_search_framework/query'
|
12
13
|
|
13
14
|
module ElasticSearchFramework
|
@@ -1,15 +1,20 @@
|
|
1
1
|
module ElasticSearchFramework
|
2
2
|
module Index
|
3
|
-
|
3
|
+
attr_accessor :index_settings
|
4
|
+
|
5
|
+
def index(name:, version: nil)
|
4
6
|
unless instance_variable_defined?(:@elastic_search_index_def)
|
5
|
-
instance_variable_set(:@elastic_search_index_def, {
|
6
|
-
|
7
|
-
})
|
7
|
+
instance_variable_set(:@elastic_search_index_def, name: "#{name}#{version}")
|
8
|
+
instance_variable_set(:@elastic_search_index_version, version: version) unless version.nil?
|
8
9
|
else
|
9
|
-
raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index description. Name: #{name}
|
10
|
+
raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index description. Name: #{name}.")
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
14
|
+
def version
|
15
|
+
instance_variable_defined?(:@elastic_search_index_version) ? instance_variable_get(:@elastic_search_index_version) : 0
|
16
|
+
end
|
17
|
+
|
13
18
|
def id(field)
|
14
19
|
unless instance_variable_defined?(:@elastic_search_index_id)
|
15
20
|
instance_variable_set(:@elastic_search_index_id, field)
|
@@ -41,8 +46,7 @@ module ElasticSearchFramework
|
|
41
46
|
ElasticSearchFramework.logger.debug { "[#{self.class}] - Index already exists."}
|
42
47
|
return
|
43
48
|
end
|
44
|
-
|
45
|
-
payload = create_payload(description: description, mappings: mappings)
|
49
|
+
payload = create_payload
|
46
50
|
|
47
51
|
put(payload: payload)
|
48
52
|
end
|
@@ -101,27 +105,26 @@ module ElasticSearchFramework
|
|
101
105
|
is_valid_response?(response.code) || Integer(response.code) == 404
|
102
106
|
end
|
103
107
|
|
104
|
-
def
|
105
|
-
|
108
|
+
def settings(name:, type: nil, value:)
|
109
|
+
self.index_settings = {} if index_settings.nil?
|
110
|
+
index_settings[name] = {} if index_settings[name].nil?
|
111
|
+
return index_settings[name][type] = value if type
|
112
|
+
index_settings[name] = value
|
113
|
+
end
|
106
114
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
}
|
111
|
-
end
|
115
|
+
def create_payload
|
116
|
+
payload = { }
|
117
|
+
payload[:settings] = index_settings unless index_settings.nil?
|
112
118
|
|
113
|
-
|
119
|
+
unless mappings.keys.empty?
|
114
120
|
payload[:mappings] = {}
|
115
121
|
|
116
122
|
mappings.keys.each do |name|
|
117
|
-
payload[:mappings][name] = {
|
118
|
-
properties: {}
|
119
|
-
}
|
123
|
+
payload[:mappings][name] = { properties: {} }
|
120
124
|
mappings[name].keys.each do |field|
|
121
125
|
payload[:mappings][name][:properties][field] = mappings[name][field]
|
122
126
|
end
|
123
127
|
end
|
124
|
-
|
125
128
|
end
|
126
129
|
|
127
130
|
payload
|
@@ -162,7 +165,7 @@ module ElasticSearchFramework
|
|
162
165
|
end
|
163
166
|
|
164
167
|
def repository
|
165
|
-
ElasticSearchFramework::Repository.new
|
168
|
+
@repository ||= ElasticSearchFramework::Repository.new
|
166
169
|
end
|
167
170
|
|
168
171
|
def get_item(id:, type: 'default')
|
@@ -0,0 +1,146 @@
|
|
1
|
+
module ElasticSearchFramework
|
2
|
+
module IndexAlias
|
3
|
+
def index(klass, active:)
|
4
|
+
unless instance_variable_defined?(:@elastic_search_indexes)
|
5
|
+
instance_variable_set(:@elastic_search_indexes, [])
|
6
|
+
end
|
7
|
+
indexes = self.instance_variable_get(:@elastic_search_indexes)
|
8
|
+
indexes << {klass: klass, active: active}
|
9
|
+
instance_variable_set(:@elastic_search_indexes, indexes)
|
10
|
+
end
|
11
|
+
|
12
|
+
def indexes
|
13
|
+
self.instance_variable_get(:@elastic_search_indexes)
|
14
|
+
end
|
15
|
+
|
16
|
+
def name(name)
|
17
|
+
unless instance_variable_defined?(:@elastic_search_index_alias_name)
|
18
|
+
instance_variable_set(:@elastic_search_index_alias_name, "#{name}")
|
19
|
+
else
|
20
|
+
raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index alias name: #{name}.")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid?
|
25
|
+
indexes.select { |i| i[:active] == true }.length == 1
|
26
|
+
end
|
27
|
+
|
28
|
+
def create
|
29
|
+
if !valid?
|
30
|
+
raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Invalid Index alias.")
|
31
|
+
end
|
32
|
+
|
33
|
+
uri = URI("#{host}/_aliases")
|
34
|
+
|
35
|
+
payload = {
|
36
|
+
actions: [],
|
37
|
+
}
|
38
|
+
|
39
|
+
indexes.each do |index|
|
40
|
+
action = nil
|
41
|
+
if exists?(index: index[:klass])
|
42
|
+
action = "remove" if !index[:active]
|
43
|
+
else
|
44
|
+
action = "add" if index[:active]
|
45
|
+
end
|
46
|
+
next if action.nil?
|
47
|
+
|
48
|
+
payload[:actions] << {action => {index: index[:klass].full_name, alias: self.full_name}}
|
49
|
+
end
|
50
|
+
|
51
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
52
|
+
request.body = JSON.dump(payload)
|
53
|
+
request.content_type = "application/json"
|
54
|
+
|
55
|
+
response = repository.with_client do |client|
|
56
|
+
client.request(request)
|
57
|
+
end
|
58
|
+
|
59
|
+
is_valid_response?(response.code) || Integer(response.code) == 404
|
60
|
+
end
|
61
|
+
|
62
|
+
def delete
|
63
|
+
uri = URI("#{host}/_all/_aliases/#{full_name}")
|
64
|
+
|
65
|
+
request = Net::HTTP::Delete.new(uri.request_uri)
|
66
|
+
|
67
|
+
response = repository.with_client do |client|
|
68
|
+
client.request(request)
|
69
|
+
end
|
70
|
+
|
71
|
+
is_valid_response?(response.code) || Integer(response.code) == 404
|
72
|
+
end
|
73
|
+
|
74
|
+
def exists?(index:)
|
75
|
+
uri = URI("#{host}/#{index.full_name}/_alias/#{full_name}")
|
76
|
+
|
77
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
78
|
+
|
79
|
+
response = repository.with_client do |client|
|
80
|
+
client.request(request)
|
81
|
+
end
|
82
|
+
|
83
|
+
return false if response.code == "404"
|
84
|
+
|
85
|
+
result = nil
|
86
|
+
if is_valid_response?(response.code)
|
87
|
+
result = JSON.parse(response.body)
|
88
|
+
end
|
89
|
+
|
90
|
+
return true if !result.nil? && result[index.full_name]["aliases"] != nil
|
91
|
+
return false
|
92
|
+
end
|
93
|
+
|
94
|
+
def is_valid_response?(code)
|
95
|
+
[200, 201, 202].include?(Integer(code))
|
96
|
+
end
|
97
|
+
|
98
|
+
def full_name
|
99
|
+
name = instance_variable_get(:@elastic_search_index_alias_name)
|
100
|
+
if ElasticSearchFramework.namespace != nil
|
101
|
+
"#{ElasticSearchFramework.namespace}#{ElasticSearchFramework.namespace_delimiter}#{name.downcase}"
|
102
|
+
else
|
103
|
+
name.downcase
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def description
|
108
|
+
index = indexes.last[:klass]
|
109
|
+
hash = index.instance_variable_get(:@elastic_search_index_def)
|
110
|
+
if index.instance_variable_defined?(:@elastic_search_index_id)
|
111
|
+
hash[:id] = index.instance_variable_get(:@elastic_search_index_id)
|
112
|
+
else
|
113
|
+
hash[:id] = :id
|
114
|
+
end
|
115
|
+
hash
|
116
|
+
end
|
117
|
+
|
118
|
+
def host
|
119
|
+
"#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}"
|
120
|
+
end
|
121
|
+
|
122
|
+
def repository
|
123
|
+
@repository ||= ElasticSearchFramework::Repository.new
|
124
|
+
end
|
125
|
+
|
126
|
+
def get_item(id:, type: "default")
|
127
|
+
repository.get(index: self, id: id, type: type)
|
128
|
+
end
|
129
|
+
|
130
|
+
def put_item(type: "default", item:)
|
131
|
+
indexes.each do |index|
|
132
|
+
repository.set(entity: item, index: index[:klass], type: type)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def delete_item(id:, type: "default")
|
137
|
+
indexes.each do |index|
|
138
|
+
repository.drop(index: index[:klass], id: id, type: type)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def query
|
143
|
+
ElasticSearchFramework::Query.new(index: self)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module ElasticSearchFramework
|
2
2
|
class Repository
|
3
3
|
|
4
|
-
def set(index:, entity:, type: 'default')
|
5
|
-
uri = URI("#{host}/#{index.full_name}/#{type.downcase}/#{get_id_value(index: index, entity: entity)}")
|
4
|
+
def set(index:, entity:, type: 'default', op_type: 'index')
|
5
|
+
uri = URI("#{host}/#{index.full_name}/#{type.downcase}/#{get_id_value(index: index, entity: entity)}?op_type=#{op_type}")
|
6
6
|
hash = hash_helper.to_hash(entity)
|
7
7
|
|
8
8
|
request = Net::HTTP::Put.new(uri.request_uri)
|
@@ -13,12 +13,15 @@ module ElasticSearchFramework
|
|
13
13
|
client.request(request)
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
if valid_response?(response.code)
|
17
|
+
return true
|
18
|
+
elsif op_type == 'create' && Integer(response.code) == 409
|
19
|
+
return true
|
20
|
+
else
|
17
21
|
raise ElasticSearchFramework::Exceptions::IndexError.new(
|
18
22
|
"An error occurred setting an index document. Response: #{response.body} | Code: #{response.code}"
|
19
23
|
)
|
20
24
|
end
|
21
|
-
return true
|
22
25
|
end
|
23
26
|
|
24
27
|
def get(index:, id:, type: 'default')
|
@@ -114,15 +117,15 @@ module ElasticSearchFramework
|
|
114
117
|
end
|
115
118
|
|
116
119
|
def idle_timeout
|
117
|
-
@idle_timeout ||= Integer(ENV['CONNECTION_IDLE_TIMEOUT'] ||
|
120
|
+
@idle_timeout ||= Integer(ENV['CONNECTION_IDLE_TIMEOUT'] || 5)
|
118
121
|
end
|
119
122
|
|
120
123
|
def read_timeout
|
121
|
-
@read_timeout ||= Integer(ENV['CONNECTION_READ_TIMEOUT'] ||
|
124
|
+
@read_timeout ||= Integer(ENV['CONNECTION_READ_TIMEOUT'] || 5)
|
122
125
|
end
|
123
126
|
|
124
127
|
def open_timeout
|
125
|
-
@
|
128
|
+
@open_timeout ||= Integer(ENV['CONNECTION_OPEN_TIMEOUT'] || 1)
|
126
129
|
end
|
127
130
|
|
128
131
|
def valid_response?(status)
|
@@ -0,0 +1,144 @@
|
|
1
|
+
RSpec.describe ElasticSearchFramework::Index do
|
2
|
+
describe '#full_name' do
|
3
|
+
let(:namespace) { 'uat' }
|
4
|
+
let(:namespace_delimiter) { '.' }
|
5
|
+
before do
|
6
|
+
ElasticSearchFramework.namespace = namespace
|
7
|
+
ElasticSearchFramework.namespace_delimiter = namespace_delimiter
|
8
|
+
end
|
9
|
+
it 'should return the full index name including namespace and delimiter' do
|
10
|
+
expect(ExampleIndexAlias.full_name).to eq "#{ElasticSearchFramework.namespace}#{ElasticSearchFramework.namespace_delimiter}example"
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'when the namespace is nil' do
|
14
|
+
before { ElasticSearchFramework.namespace = nil }
|
15
|
+
|
16
|
+
it 'returns the description name downcased' do
|
17
|
+
expect(ExampleIndexAlias.full_name).to eq 'example'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#valid?' do
|
23
|
+
context 'for a valid index definition' do
|
24
|
+
it 'should return true' do
|
25
|
+
expect(ExampleIndexAlias.valid?).to be true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
context 'for an invalid index definition' do
|
29
|
+
it 'should return true' do
|
30
|
+
expect(InvalidIndexAlias.valid?).to be false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#create' do
|
36
|
+
context 'when alias is valid and does not exist' do
|
37
|
+
before do
|
38
|
+
ExampleIndexAlias.delete
|
39
|
+
ExampleIndex.delete if ExampleIndex.exists?
|
40
|
+
ExampleIndex.create
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'creates an alias for the active index' do
|
44
|
+
expect(ExampleIndexAlias.exists?(index: ExampleIndex)).to be false
|
45
|
+
ExampleIndexAlias.create
|
46
|
+
expect(ExampleIndexAlias.exists?(index: ExampleIndex)).to be true
|
47
|
+
end
|
48
|
+
|
49
|
+
after do
|
50
|
+
ExampleIndexAlias.delete
|
51
|
+
ExampleIndex.delete
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when alias is not valid' do
|
56
|
+
before { allow(ExampleIndexAlias).to receive(:valid?).and_return(false) }
|
57
|
+
|
58
|
+
it 'raises an error' do
|
59
|
+
expect(ExampleIndexAlias.exists?(index: ExampleIndex)).to be false
|
60
|
+
expect { ExampleIndexAlias.create }.to raise_error(
|
61
|
+
ElasticSearchFramework::Exceptions::IndexError
|
62
|
+
)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when alias is valid but already exists' do
|
67
|
+
before do
|
68
|
+
ExampleIndex.delete if ExampleIndex.exists?
|
69
|
+
ExampleIndex.create
|
70
|
+
ExampleIndexAlias.create
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'does not try to create a new alias' do
|
74
|
+
ExampleIndexAlias.create
|
75
|
+
end
|
76
|
+
|
77
|
+
after do
|
78
|
+
ExampleIndexAlias.delete
|
79
|
+
ExampleIndex.delete
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when alias is valid and does not exist and requires updating' do
|
84
|
+
before do
|
85
|
+
ExampleIndexAlias.delete
|
86
|
+
ExampleIndex.delete if ExampleIndex.exists?
|
87
|
+
ExampleIndex2.delete if ExampleIndex2.exists?
|
88
|
+
ExampleIndex.create
|
89
|
+
ExampleIndex2.create
|
90
|
+
ExampleIndexAlias.create
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'modifies the alias to the active index' do
|
94
|
+
expect(ExampleIndexAlias.exists?(index: ExampleIndex)).to be true
|
95
|
+
ExampleIndexAlias2.create
|
96
|
+
expect(ExampleIndexAlias.exists?(index: ExampleIndex)).to be false
|
97
|
+
expect(ExampleIndexAlias2.exists?(index: ExampleIndex2)).to be true
|
98
|
+
end
|
99
|
+
|
100
|
+
after do
|
101
|
+
ExampleIndexAlias.delete
|
102
|
+
ExampleIndex.delete
|
103
|
+
ExampleIndex2.delete
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe '#get_item' do
|
109
|
+
let(:id) { 10 }
|
110
|
+
let(:type) { 'default' }
|
111
|
+
it 'should call get on the repository' do
|
112
|
+
expect(ExampleIndexAlias.repository).to receive(:get).with(index: ExampleIndexAlias, id: id, type: type).once
|
113
|
+
ExampleIndexAlias.get_item(id: id, type: type)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe '#put_item' do
|
118
|
+
let(:id) { 10 }
|
119
|
+
let(:type) { 'default' }
|
120
|
+
let(:item) do
|
121
|
+
TestItem.new.tap do |i|
|
122
|
+
i.id = id
|
123
|
+
i.name = 'abc'
|
124
|
+
i.timestamp = Time.now.to_i
|
125
|
+
i.number = 5
|
126
|
+
end
|
127
|
+
end
|
128
|
+
it 'should call set on the repository for each index of the alias' do
|
129
|
+
expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex, type: type).once
|
130
|
+
expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex2, type: type).once
|
131
|
+
ExampleIndexAlias.put_item(type: type, item: item)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe '#delete_item' do
|
136
|
+
let(:id) { 10 }
|
137
|
+
let(:type) { 'default' }
|
138
|
+
it 'should call drop on the repository for each index of the alias' do
|
139
|
+
expect(ExampleIndexAlias.repository).to receive(:drop).with(index: ExampleIndex, id: id, type: type).once
|
140
|
+
expect(ExampleIndexAlias.repository).to receive(:drop).with(index: ExampleIndex2, id: id, type: type).once
|
141
|
+
ExampleIndexAlias.delete_item(id: id, type: type)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -5,7 +5,6 @@ RSpec.describe ElasticSearchFramework::Index do
|
|
5
5
|
expect(ExampleIndex.description[:name]).to eq 'example_index'
|
6
6
|
expect(ExampleIndex.description[:id]).to eq :id
|
7
7
|
expect(ExampleIndexWithId.description[:id]).to eq :number
|
8
|
-
expect(ExampleIndexWithShard.description[:shards]).to eq 1
|
9
8
|
end
|
10
9
|
end
|
11
10
|
|
@@ -16,7 +15,7 @@ RSpec.describe ElasticSearchFramework::Index do
|
|
16
15
|
it 'raises an index error' do
|
17
16
|
expect { ExampleIndex.index(name: 'test') }.to raise_error(
|
18
17
|
ElasticSearchFramework::Exceptions::IndexError,
|
19
|
-
|
18
|
+
'[Class] - Duplicate index description. Name: test.'
|
20
19
|
)
|
21
20
|
end
|
22
21
|
end
|
@@ -64,6 +63,44 @@ RSpec.describe ElasticSearchFramework::Index do
|
|
64
63
|
end
|
65
64
|
end
|
66
65
|
|
66
|
+
describe '#analysis' do
|
67
|
+
context 'when analysis is nil' do
|
68
|
+
before { ExampleIndex.delete if ExampleIndex.exists? }
|
69
|
+
|
70
|
+
it 'does not add analysis to the index' do
|
71
|
+
ExampleIndex.create
|
72
|
+
expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'analysis')).to be_nil
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when analysis is not nil' do
|
77
|
+
before { ExampleIndexWithSettings.delete if ExampleIndexWithSettings.exists? }
|
78
|
+
let(:expected) do
|
79
|
+
{
|
80
|
+
'normalizer' => {
|
81
|
+
'custom_normalizer' => {
|
82
|
+
'filter' => ['lowercase'],
|
83
|
+
'type' => 'custom'
|
84
|
+
}
|
85
|
+
},
|
86
|
+
'analyzer' => {
|
87
|
+
'custom_analyzer' => {
|
88
|
+
'filter' => ['lowercase'],
|
89
|
+
'type' => 'custom',
|
90
|
+
'tokenizer' => 'standard'
|
91
|
+
}
|
92
|
+
}
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'adds analysis to the index' do
|
97
|
+
ExampleIndexWithSettings.create
|
98
|
+
expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'number_of_shards')).to eq('1')
|
99
|
+
expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'analysis')).to eq(expected)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
67
104
|
describe '#valid?' do
|
68
105
|
context 'for a valid index definition' do
|
69
106
|
it 'should return true' do
|
@@ -239,8 +276,8 @@ RSpec.describe ElasticSearchFramework::Index do
|
|
239
276
|
it 'should return a ElasticSearchFramework::Repository instance' do
|
240
277
|
expect(ExampleIndex.repository).to be_a(ElasticSearchFramework::Repository)
|
241
278
|
end
|
242
|
-
it 'should return
|
243
|
-
expect(ExampleIndex.repository).
|
279
|
+
it 'should return the same ElasticSearchFramework::Repository instance for multiple calls' do
|
280
|
+
expect(ExampleIndex.repository).to eq ExampleIndex.repository
|
244
281
|
end
|
245
282
|
end
|
246
283
|
|
@@ -63,22 +63,40 @@ RSpec.describe ElasticSearchFramework::Repository do
|
|
63
63
|
ExampleIndexWithId.create
|
64
64
|
end
|
65
65
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
66
|
+
context 'PUT with op_type: index' do
|
67
|
+
it 'should create, read and delete an index document' do
|
68
|
+
subject.set(index: ExampleIndex, entity: item1)
|
69
|
+
subject.set(index: ExampleIndex, entity: item1.tap { |i| i.timestamp += 100 })
|
70
|
+
subject.set(index: ExampleIndex, entity: item1.tap { |i| i.timestamp += 100 }, op_type: 'index')
|
71
|
+
subject.set(index: ExampleIndex, entity: item2)
|
72
|
+
subject.set(index: ExampleIndex, entity: item5)
|
73
|
+
index_item1 = subject.get(index: ExampleIndex, id: item1.id)
|
74
|
+
expect(index_item1[:id]).to eq item1.id
|
75
|
+
expect(index_item1[:name]).to eq item1.name
|
76
|
+
expect(index_item1[:timestamp]).to eq item1.timestamp
|
77
|
+
expect(index_item1[:number]).to eq item1.number
|
78
|
+
index_item2 = subject.get(index: ExampleIndex, id: item2.id)
|
79
|
+
expect(index_item2[:id]).to eq item2.id
|
80
|
+
expect(index_item2[:name]).to eq item2.name
|
81
|
+
expect(index_item2[:timestamp]).to eq item2.timestamp
|
82
|
+
expect(index_item2[:number]).to eq item2.number
|
83
|
+
subject.drop(index: ExampleIndex, id: item1.id)
|
84
|
+
expect(subject.get(index: ExampleIndex, id: item1.id)).to be_nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'PUT with op_type: create' do
|
89
|
+
let!(:original_timestamp) { item1.timestamp }
|
90
|
+
|
91
|
+
it 'should not update item' do
|
92
|
+
subject.set(index: ExampleIndex, entity: item1)
|
93
|
+
subject.set(index: ExampleIndex, entity: item1.tap { |i| i.timestamp += 100 }, op_type: 'create')
|
94
|
+
index_item1 = subject.get(index: ExampleIndex, id: item1.id)
|
95
|
+
expect(index_item1[:id]).to eq item1.id
|
96
|
+
expect(index_item1[:name]).to eq item1.name
|
97
|
+
expect(index_item1[:timestamp]).to eq original_timestamp
|
98
|
+
expect(index_item1[:number]).to eq item1.number
|
99
|
+
end
|
82
100
|
end
|
83
101
|
|
84
102
|
after do
|
data/spec/example_index.rb
CHANGED
@@ -4,7 +4,6 @@ class ExampleIndex
|
|
4
4
|
index name: 'example_index'
|
5
5
|
|
6
6
|
mapping name: 'default', field: :name, type: :keyword, index: true
|
7
|
-
|
8
7
|
end
|
9
8
|
|
10
9
|
class ExampleIndexWithId
|
@@ -15,16 +14,20 @@ class ExampleIndexWithId
|
|
15
14
|
id :number
|
16
15
|
|
17
16
|
mapping name: 'default', field: :name, type: :keyword, index: true
|
18
|
-
|
19
17
|
end
|
20
18
|
|
21
|
-
class
|
19
|
+
class ExampleIndexWithSettings
|
22
20
|
extend ElasticSearchFramework::Index
|
23
21
|
|
24
|
-
index name: 'example_index'
|
22
|
+
index name: 'example_index'
|
25
23
|
|
26
|
-
|
24
|
+
normalizer_value = { custom_normalizer: { type: 'custom', char_filter: [], filter: ['lowercase'] } }
|
25
|
+
analyzer_value = { custom_analyzer: { type: 'custom', tokenizer: 'standard', filter: %w(lowercase) } }
|
27
26
|
|
27
|
+
settings name: :number_of_shards, value: 1
|
28
|
+
settings name: :analysis, type: :normalizer, value: normalizer_value
|
29
|
+
settings name: :analysis, type: :analyzer, value: analyzer_value
|
30
|
+
mapping name: 'default', field: :name, type: :keyword, index: true
|
28
31
|
end
|
29
32
|
|
30
33
|
class InvalidExampleIndex
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class ExampleIndex2
|
2
|
+
extend ElasticSearchFramework::Index
|
3
|
+
|
4
|
+
index name: 'example_index', version: 2
|
5
|
+
|
6
|
+
mapping name: 'default', field: :name, type: :keyword, index: true
|
7
|
+
end
|
8
|
+
|
9
|
+
class ExampleIndexWithId2
|
10
|
+
extend ElasticSearchFramework::Index
|
11
|
+
|
12
|
+
index name: 'example_index', version: 2
|
13
|
+
|
14
|
+
id :number
|
15
|
+
|
16
|
+
mapping name: 'default', field: :name, type: :keyword, index: true
|
17
|
+
end
|
18
|
+
|
19
|
+
class ExampleIndexWithSettings2
|
20
|
+
extend ElasticSearchFramework::Index
|
21
|
+
|
22
|
+
index name: 'example_index', version: 2
|
23
|
+
|
24
|
+
normalizer_value = { custom_normalizer: { type: 'custom', char_filter: [], filter: ['lowercase'] } }
|
25
|
+
analyzer_value = { custom_analyzer: { type: 'custom', tokenizer: 'standard', filter: %w(lowercase) } }
|
26
|
+
|
27
|
+
settings name: :number_of_shards, value: 1
|
28
|
+
settings name: :analysis, type: :normalizer, value: normalizer_value
|
29
|
+
settings name: :analysis, type: :analyzer, value: analyzer_value
|
30
|
+
mapping name: 'default', field: :name, type: :keyword, index: true
|
31
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class ExampleIndexAlias
|
2
|
+
extend ElasticSearchFramework::IndexAlias
|
3
|
+
|
4
|
+
index ExampleIndex, active: true
|
5
|
+
index ExampleIndex2, active: false
|
6
|
+
|
7
|
+
name :example
|
8
|
+
end
|
9
|
+
|
10
|
+
class InvalidIndexAlias
|
11
|
+
extend ElasticSearchFramework::IndexAlias
|
12
|
+
|
13
|
+
index ExampleIndex, active: false
|
14
|
+
index ExampleIndex2, active: false
|
15
|
+
|
16
|
+
name :example
|
17
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -9,6 +9,9 @@ end
|
|
9
9
|
require 'elastic_search_framework'
|
10
10
|
require_relative '../spec/test_item.rb'
|
11
11
|
require_relative '../spec/example_index'
|
12
|
+
require_relative '../spec/example_index_2'
|
13
|
+
require_relative '../spec/example_index_alias'
|
14
|
+
require_relative '../spec/example_index_alias_2'
|
12
15
|
require 'pry'
|
13
16
|
|
14
17
|
RSpec.configure do |config|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastic_search_framework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- vaughanbrittonsage
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -119,14 +119,19 @@ files:
|
|
119
119
|
- lib/elastic_search_framework/exceptions.rb
|
120
120
|
- lib/elastic_search_framework/exceptions/index_error.rb
|
121
121
|
- lib/elastic_search_framework/index.rb
|
122
|
+
- lib/elastic_search_framework/index_alias.rb
|
122
123
|
- lib/elastic_search_framework/logger.rb
|
123
124
|
- lib/elastic_search_framework/query.rb
|
124
125
|
- lib/elastic_search_framework/repository.rb
|
125
126
|
- lib/elastic_search_framework/version.rb
|
127
|
+
- spec/elastic_search_framework/index_alias_spec.rb
|
126
128
|
- spec/elastic_search_framework/index_spec.rb
|
127
129
|
- spec/elastic_search_framework/query_spec.rb
|
128
130
|
- spec/elastic_search_framework/repository_spec.rb
|
129
131
|
- spec/example_index.rb
|
132
|
+
- spec/example_index_2.rb
|
133
|
+
- spec/example_index_alias.rb
|
134
|
+
- spec/example_index_alias_2.rb
|
130
135
|
- spec/spec_helper.rb
|
131
136
|
- spec/test_item.rb
|
132
137
|
homepage: https://github.com/sage/elastic_search_framework
|
@@ -148,8 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
153
|
- !ruby/object:Gem::Version
|
149
154
|
version: '0'
|
150
155
|
requirements: []
|
151
|
-
|
152
|
-
rubygems_version: 2.7.7
|
156
|
+
rubygems_version: 3.0.8
|
153
157
|
signing_key:
|
154
158
|
specification_version: 4
|
155
159
|
summary: A lightweight framework to for working with elastic search.
|