Skip to content

Commit 00a0713

Browse files
author
Cosima Radu
authored
Merge pull request #18 from Sage/set_op_type
Adds support for `op_type` to be supplied for `ElasticSearchFramework::Repository#set`
2 parents 3646ca3 + d2e0469 commit 00a0713

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ rvm:
33
- 2.3.0
44
services:
55
- elasticsearch
6+
before_install:
7+
- find /home/travis/.rvm/rubies -wholename '*default/bundler-*.gemspec' -delete
8+
- gem install bundler -v '< 2'
69
before_script:
710
- sleep 10
811
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v2.3.0
2+
Adds support for `op_type` to be supplied for `ElasticSearchFramework::Repository#set` to allow control of PUT behaviour in create scenarios.
3+
See [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docs-index_.html#operation-type) for more information and accepted values.
4+
5+
## v2.2.0
6+
Adds index alias support.
7+
18
## v2.1.0
29
Ensures that the read timeout is set to either the value of `ENV['CONNECTION_READ_TIMEOUT']` or `5` as a default.
310
Previously this was being set to the value of `ENV['CONNECTION_OPEN_TIMEOUT']` or `1` as a default.

lib/elastic_search_framework/repository.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module ElasticSearchFramework
22
class Repository
33

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}")
66
hash = hash_helper.to_hash(entity)
77

88
request = Net::HTTP::Put.new(uri.request_uri)
@@ -13,12 +13,15 @@ def set(index:, entity:, type: 'default')
1313
client.request(request)
1414
end
1515

16-
unless valid_response?(response.code)
16+
if valid_response?(response.code)
17+
return true
18+
elsif op_type == 'create' && Integer(response.code) == 409
19+
return true
20+
else
1721
raise ElasticSearchFramework::Exceptions::IndexError.new(
1822
"An error occurred setting an index document. Response: #{response.body} | Code: #{response.code}"
1923
)
2024
end
21-
return true
2225
end
2326

2427
def get(index:, id:, type: 'default')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ElasticSearchFramework
2-
VERSION = '2.2.0'
2+
VERSION = '2.3.0'
33
end

spec/elastic_search_framework/repository_spec.rb

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,40 @@
6363
ExampleIndexWithId.create
6464
end
6565

66-
it 'should create, read and delete an index document' do
67-
subject.set(index: ExampleIndex, entity: item1)
68-
subject.set(index: ExampleIndex, entity: item2)
69-
subject.set(index: ExampleIndex, entity: item5)
70-
index_item1 = subject.get(index: ExampleIndex, id: item1.id)
71-
expect(index_item1[:id]).to eq item1.id
72-
expect(index_item1[:name]).to eq item1.name
73-
expect(index_item1[:timestamp]).to eq item1.timestamp
74-
expect(index_item1[:number]).to eq item1.number
75-
index_item2 = subject.get(index: ExampleIndex, id: item2.id)
76-
expect(index_item2[:id]).to eq item2.id
77-
expect(index_item2[:name]).to eq item2.name
78-
expect(index_item2[:timestamp]).to eq item2.timestamp
79-
expect(index_item2[:number]).to eq item2.number
80-
subject.drop(index: ExampleIndex, id: item1.id)
81-
expect(subject.get(index: ExampleIndex, id: item1.id)).to be_nil
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
82100
end
83101

84102
after do

0 commit comments

Comments
 (0)