Skip to content

Commit e800f0f

Browse files
committed
elasticsearch-persistence: Updates to elasticsearch 8, removes type, whitespace cleanup
1 parent 34a3aa8 commit e800f0f

File tree

13 files changed

+92
-407
lines changed

13 files changed

+92
-407
lines changed

elasticsearch-persistence/elasticsearch-persistence.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Gem::Specification.new do |s|
4343

4444
s.add_dependency 'activemodel', '> 4'
4545
s.add_dependency 'activesupport', '> 4'
46-
s.add_dependency 'elasticsearch', '~> 7'
46+
s.add_dependency 'elasticsearch', '~> 8'
4747
s.add_dependency 'elasticsearch-model', '7.2.1'
4848
s.add_dependency 'hashie'
4949

elasticsearch-persistence/examples/notes/application.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class NoteRepository
7575
client Elasticsearch::Client.new url: ENV['ELASTICSEARCH_URL'], log: true
7676

7777
index_name :notes
78-
document_type :note
7978

8079
mapping do
8180
indexes :text, analyzer: 'snowball'

elasticsearch-persistence/lib/elasticsearch/persistence/repository.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ module ClassMethods
5858
# @param [ Proc ] block A block to evaluate on the new repository instance.
5959
#
6060
# @option options [ Symbol, String ] :index_name The name of the index.
61-
# @option options [ Symbol, String ] :document_type The type of documents persisted in this repository.
6261
# @option options [ Symbol, String ] :client The client used to handle requests to and from Elasticsearch.
6362
# @option options [ Symbol, String ] :klass The class used to instantiate an object when documents are
6463
# deserialized. The default is nil, in which case the raw document will be returned as a Hash.
@@ -95,7 +94,6 @@ def create(options = {}, &block)
9594
# @param [ Hash ] options The options to use.
9695
#
9796
# @option options [ Symbol, String ] :index_name The name of the index.
98-
# @option options [ Symbol, String ] :document_type The type of documents persisted in this repository.
9997
# @option options [ Symbol, String ] :client The client used to handle requests to and from Elasticsearch.
10098
# @option options [ Symbol, String ] :klass The class used to instantiate an object when documents are
10199
# deserialized. The default is nil, in which case the raw document will be returned as a Hash.
@@ -121,19 +119,6 @@ def client
121119
Elasticsearch::Client.new
122120
end
123121

124-
# Get the document type used by the repository object.
125-
#
126-
# @example
127-
# repository.document_type
128-
#
129-
# @return [ String, Symbol ] The repository's document type.
130-
#
131-
# @since 6.0.0
132-
def document_type
133-
@document_type ||= @options[:document_type] ||
134-
__get_class_value(:document_type)
135-
end
136-
137122
# Get the index name used by the repository.
138123
#
139124
# @example
@@ -180,7 +165,6 @@ def klass
180165
def mapping(*args)
181166
@memoized_mapping ||= @options[:mapping] || (begin
182167
if _mapping = __get_class_value(:mapping)
183-
_mapping.instance_variable_set(:@type, document_type)
184168
_mapping
185169
end
186170
end) || (super && @mapping)
@@ -229,7 +213,7 @@ def index_exists?(*args)
229213
#
230214
# @since 6.0.0
231215
def inspect
232-
"#<#{self.class}:0x#{object_id} index_name=#{index_name} document_type=#{document_type} klass=#{klass}>"
216+
"#<#{self.class}:0x#{object_id} index_name=#{index_name} klass=#{klass}>"
233217
end
234218

235219
private

elasticsearch-persistence/lib/elasticsearch/persistence/repository/dsl.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ module Repository
2323
#
2424
# @since 6.0.0
2525
module DSL
26-
2726
def self.included(base)
2827
base.send(:extend, Elasticsearch::Model::Indexing::ClassMethods)
2928
base.send(:extend, ClassMethods)
@@ -34,19 +33,6 @@ def self.included(base)
3433
#
3534
# @since 6.0.0
3635
module ClassMethods
37-
38-
# Get or set the class-level document type setting.
39-
#
40-
# @example
41-
# MyRepository.document_type
42-
#
43-
# @return [ String, Symbol ] _type The repository's document type.
44-
#
45-
# @since 6.0.0
46-
def document_type(_type = nil)
47-
@document_type ||= _type
48-
end
49-
5036
# Get or set the class-level index name setting.
5137
#
5238
# @example

elasticsearch-persistence/lib/elasticsearch/persistence/repository/find.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def find(*args)
6464
#
6565
def exists?(id, options={})
6666
request = { index: index_name, id: id }
67-
request[:type] = document_type if document_type
6867
client.exists(request.merge(options))
6968
end
7069

@@ -84,7 +83,6 @@ def exists?(id, options={})
8483
#
8584
def __find_one(id, options={})
8685
request = { index: index_name, id: id }
87-
request[:type] = document_type if document_type
8886
document = client.get(request.merge(options))
8987
deserialize(document)
9088
rescue Elastic::Transport::Transport::Errors::NotFound => e
@@ -95,7 +93,6 @@ def __find_one(id, options={})
9593
#
9694
def __find_many(ids, options={})
9795
request = { index: index_name, body: { ids: ids } }
98-
request[:type] = document_type if document_type
9996
documents = client.mget(request.merge(options))
10097
documents[DOCS].map do |document|
10198
deserialize(document) if document[FOUND]

elasticsearch-persistence/lib/elasticsearch/persistence/repository/search.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ module Search
6060
# @return [Elasticsearch::Persistence::Repository::Response::Results]
6161
#
6262
def search(query_or_definition, options={})
63-
request = { index: index_name,
64-
type: document_type }
63+
request = { index: index_name }
6564
if query_or_definition.respond_to?(:to_hash)
6665
request[:body] = query_or_definition.to_hash
6766
elsif query_or_definition.is_a?(String)
@@ -98,8 +97,7 @@ def search(query_or_definition, options={})
9897
#
9998
def count(query_or_definition=nil, options={})
10099
query_or_definition ||= { query: { match_all: {} } }
101-
request = { index: index_name,
102-
type: document_type }
100+
request = { index: index_name }
103101

104102
if query_or_definition.respond_to?(:to_hash)
105103
request[:body] = query_or_definition.to_hash

elasticsearch-persistence/lib/elasticsearch/persistence/repository/store.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module Store
2727
#
2828
# @example
2929
# repository.save(myobject)
30-
# => {"_index"=>"...", "_type"=>"...", "_id"=>"...", "_version"=>1, "created"=>true}
30+
# => {"_index"=>"...", "_id"=>"...", "_version"=>1, "created"=>true}
3131
#
3232
# @param [ Object ] document The document to save into Elasticsearch.
3333
# @param [ Hash ] options The save request options.
@@ -40,7 +40,6 @@ def save(document, options={})
4040
request = { index: index_name,
4141
id: id,
4242
body: serialized }
43-
request[:type] = document_type if document_type
4443
client.index(request.merge(options))
4544
end
4645

@@ -49,12 +48,12 @@ def save(document, options={})
4948
# @example Update the document with partial data
5049
#
5150
# repository.update id: 1, title: 'UPDATED', tags: []
52-
# # => {"_index"=>"...", "_type"=>"...", "_id"=>"1", "_version"=>2}
51+
# # => {"_index"=>"...", "_id"=>"1", "_version"=>2}
5352
#
5453
# @example Update the document with a script
5554
#
5655
# repository.update 1, script: 'ctx._source.views += 1'
57-
# # => {"_index"=>"...", "_type"=>"...", "_id"=>"1", "_version"=>3}
56+
# # => {"_index"=>"...", "_id"=>"1", "_version"=>3}
5857
#
5958
# @param [ Object ] document_or_id The document to update or the id of the document to update.
6059
# @param [ Hash ] options The update request options.
@@ -65,7 +64,6 @@ def update(document_or_id, options = {})
6564
if document_or_id.is_a?(String) || document_or_id.is_a?(Integer)
6665
id = document_or_id
6766
body = options
68-
type = document_type
6967
else
7068
document = serialize(document_or_id)
7169
id = __extract_id_from_document(document)
@@ -74,17 +72,16 @@ def update(document_or_id, options = {})
7472
else
7573
body = { doc: document }.merge(options)
7674
end
77-
type = document.delete(:type) || document_type
7875
end
79-
client.update(index: index_name, id: id, type: type, body: body)
76+
client.update(index: index_name, id: id, body: body)
8077
end
8178

8279
# Remove the serialized object or document with specified ID from Elasticsearch
8380
#
8481
# @example Remove the document with ID 1
8582
#
8683
# repository.delete(1)
87-
# # => {"_index"=>"...", "_type"=>"...", "_id"=>"1", "_version"=>4}
84+
# # => {"_index"=>"...", "_id"=>"1", "_version"=>4}
8885
#
8986
# @param [ Object ] document_or_id The document to delete or the id of the document to delete.
9087
# @param [ Hash ] options The delete request options.
@@ -98,7 +95,7 @@ def delete(document_or_id, options = {})
9895
serialized = serialize(document_or_id)
9996
id = __get_id_from_document(serialized)
10097
end
101-
client.delete({ index: index_name, type: document_type, id: id }.merge(options))
98+
client.delete({ index: index_name, id: id }.merge(options))
10299
end
103100
end
104101
end

elasticsearch-persistence/spec/repository/find_spec.rb

Lines changed: 14 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
require 'spec_helper'
1919

2020
describe Elasticsearch::Persistence::Repository::Find do
21-
2221
after do
2322
begin; repository.delete_index!; rescue; end
2423
end
@@ -28,9 +27,7 @@
2827
end
2928

3029
describe '#exists?' do
31-
3230
context 'when the document exists' do
33-
3431
let(:id) do
3532
repository.save(a: 1)['_id']
3633
end
@@ -41,30 +38,15 @@
4138
end
4239

4340
context 'when the document does not exist' do
44-
4541
it 'returns false' do
4642
expect(repository.exists?('1')).to be(false)
4743
end
4844
end
49-
50-
context 'when options are provided' do
51-
52-
let(:id) do
53-
repository.save(a: 1)['_id']
54-
end
55-
56-
it 'applies the options' do
57-
expect(repository.exists?(id, type: 'other_type')).to be(false)
58-
end
59-
end
6045
end
6146

6247
describe '#find' do
63-
6448
context 'when options are not provided' do
65-
6649
context 'when a single id is provided' do
67-
6850
let!(:id) do
6951
repository.save(a: 1)['_id']
7052
end
@@ -75,51 +57,53 @@
7557
end
7658

7759
context 'when an array of ids is provided' do
78-
7960
let!(:ids) do
8061
3.times.collect do |i|
8162
repository.save(a: i)['_id']
8263
end
8364
end
8465

8566
it 'retrieves the documents' do
86-
expect(repository.find(ids)).to eq([{ 'a' =>0 },
87-
{ 'a' => 1 },
88-
{ 'a' => 2 }])
67+
expect(repository.find(ids)).to eq([
68+
{ 'a' =>0 },
69+
{ 'a' => 1 },
70+
{ 'a' => 2 }
71+
])
8972
end
9073

9174
context 'when some documents are found and some are not' do
92-
9375
before do
9476
ids[1] = 22
9577
ids
9678
end
9779

9880
it 'returns nil in the result list for the documents not found' do
99-
expect(repository.find(ids)).to eq([{ 'a' =>0 },
81+
expect(repository.find(ids)).to eq([
82+
{ 'a' =>0 },
10083
nil,
101-
{ 'a' => 2 }])
84+
{ 'a' => 2 }
85+
])
10286
end
10387
end
10488
end
10589

10690
context 'when multiple ids are provided' do
107-
10891
let!(:ids) do
10992
3.times.collect do |i|
11093
repository.save(a: i)['_id']
11194
end
11295
end
11396

11497
it 'retrieves the documents' do
115-
expect(repository.find(*ids)).to eq([{ 'a' =>0 },
116-
{ 'a' => 1 },
117-
{ 'a' => 2 }])
98+
expect(repository.find(*ids)).to eq([
99+
{ 'a' =>0 },
100+
{ 'a' => 1 },
101+
{ 'a' => 2 }
102+
])
118103
end
119104
end
120105

121106
context 'when the document cannot be found' do
122-
123107
before do
124108
begin; repository.create_index!; rescue; end
125109
end
@@ -131,66 +115,5 @@
131115
end
132116
end
133117
end
134-
135-
context 'when options are provided' do
136-
137-
context 'when a single id is passed' do
138-
139-
let!(:id) do
140-
repository.save(a: 1)['_id']
141-
end
142-
143-
it 'applies the options' do
144-
expect {
145-
repository.find(id, type: 'none')
146-
}.to raise_exception(Elasticsearch::Persistence::Repository::DocumentNotFound)
147-
end
148-
end
149-
150-
context 'when an array of ids is passed' do
151-
152-
let!(:ids) do
153-
3.times.collect do |i|
154-
repository.save(a: i)['_id']
155-
end
156-
end
157-
158-
it 'applies the options' do
159-
expect(repository.find(ids, type: 'none')).to eq([nil, nil, nil])
160-
end
161-
end
162-
163-
context 'when multiple ids are passed' do
164-
165-
let!(:ids) do
166-
3.times.collect do |i|
167-
repository.save(a: i)['_id']
168-
end
169-
end
170-
171-
it 'applies the options' do
172-
expect(repository.find(*ids, type: 'none')).to eq([nil, nil, nil])
173-
end
174-
end
175-
end
176-
177-
context 'when a document_type is defined on the class' do
178-
179-
let(:repository) do
180-
MyTestRepository.new(document_type:'other_type', client: DEFAULT_CLIENT, index_name: 'test')
181-
end
182-
183-
let!(:ids) do
184-
3.times.collect do |i|
185-
repository.save(a: i)['_id']
186-
end
187-
end
188-
189-
it 'uses the document type in the query' do
190-
expect(repository.find(ids)).to eq([{ 'a' =>0 },
191-
{ 'a' => 1 },
192-
{ 'a' => 2 }])
193-
end
194-
end
195118
end
196119
end

0 commit comments

Comments
 (0)