elasticsearch-persistence 5.0.2 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +5 -5
  2. data/.rspec +2 -0
  3. data/Gemfile +9 -0
  4. data/README.md +206 -338
  5. data/Rakefile +15 -12
  6. data/elasticsearch-persistence.gemspec +6 -7
  7. data/examples/notes/application.rb +3 -4
  8. data/lib/elasticsearch/persistence.rb +2 -110
  9. data/lib/elasticsearch/persistence/repository.rb +212 -53
  10. data/lib/elasticsearch/persistence/repository/dsl.rb +94 -0
  11. data/lib/elasticsearch/persistence/repository/find.rb +27 -10
  12. data/lib/elasticsearch/persistence/repository/response/results.rb +21 -8
  13. data/lib/elasticsearch/persistence/repository/search.rb +30 -18
  14. data/lib/elasticsearch/persistence/repository/serialize.rb +65 -7
  15. data/lib/elasticsearch/persistence/repository/store.rb +38 -44
  16. data/lib/elasticsearch/persistence/version.rb +1 -1
  17. data/spec/repository/find_spec.rb +179 -0
  18. data/spec/repository/response/results_spec.rb +128 -0
  19. data/spec/repository/search_spec.rb +181 -0
  20. data/spec/repository/serialize_spec.rb +53 -0
  21. data/spec/repository/store_spec.rb +327 -0
  22. data/spec/repository_spec.rb +723 -0
  23. data/spec/spec_helper.rb +32 -0
  24. metadata +26 -104
  25. data/examples/music/album.rb +0 -54
  26. data/examples/music/artist.rb +0 -70
  27. data/examples/music/artists/_form.html.erb +0 -8
  28. data/examples/music/artists/artists_controller.rb +0 -67
  29. data/examples/music/artists/artists_controller_test.rb +0 -53
  30. data/examples/music/artists/index.html.erb +0 -60
  31. data/examples/music/artists/show.html.erb +0 -54
  32. data/examples/music/assets/application.css +0 -257
  33. data/examples/music/assets/autocomplete.css +0 -48
  34. data/examples/music/assets/blank_artist.png +0 -0
  35. data/examples/music/assets/blank_cover.png +0 -0
  36. data/examples/music/assets/form.css +0 -113
  37. data/examples/music/index_manager.rb +0 -73
  38. data/examples/music/search/index.html.erb +0 -95
  39. data/examples/music/search/search_controller.rb +0 -41
  40. data/examples/music/search/search_controller_test.rb +0 -12
  41. data/examples/music/search/search_helper.rb +0 -15
  42. data/examples/music/suggester.rb +0 -69
  43. data/examples/music/template.rb +0 -430
  44. data/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.css +0 -7
  45. data/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.js +0 -6
  46. data/examples/music/vendor/assets/stylesheets/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  47. data/lib/elasticsearch/persistence/client.rb +0 -51
  48. data/lib/elasticsearch/persistence/model.rb +0 -135
  49. data/lib/elasticsearch/persistence/model/base.rb +0 -87
  50. data/lib/elasticsearch/persistence/model/errors.rb +0 -8
  51. data/lib/elasticsearch/persistence/model/find.rb +0 -180
  52. data/lib/elasticsearch/persistence/model/rails.rb +0 -47
  53. data/lib/elasticsearch/persistence/model/store.rb +0 -254
  54. data/lib/elasticsearch/persistence/model/utils.rb +0 -0
  55. data/lib/elasticsearch/persistence/repository/class.rb +0 -71
  56. data/lib/elasticsearch/persistence/repository/naming.rb +0 -115
  57. data/lib/rails/generators/elasticsearch/model/model_generator.rb +0 -21
  58. data/lib/rails/generators/elasticsearch/model/templates/model.rb.tt +0 -9
  59. data/lib/rails/generators/elasticsearch_generator.rb +0 -2
  60. data/test/integration/model/model_basic_test.rb +0 -233
  61. data/test/integration/repository/custom_class_test.rb +0 -85
  62. data/test/integration/repository/customized_class_test.rb +0 -82
  63. data/test/integration/repository/default_class_test.rb +0 -116
  64. data/test/integration/repository/virtus_model_test.rb +0 -118
  65. data/test/test_helper.rb +0 -55
  66. data/test/unit/model_base_test.rb +0 -72
  67. data/test/unit/model_find_test.rb +0 -153
  68. data/test/unit/model_gateway_test.rb +0 -101
  69. data/test/unit/model_rails_test.rb +0 -112
  70. data/test/unit/model_store_test.rb +0 -576
  71. data/test/unit/persistence_test.rb +0 -32
  72. data/test/unit/repository_class_test.rb +0 -51
  73. data/test/unit/repository_client_test.rb +0 -32
  74. data/test/unit/repository_find_test.rb +0 -388
  75. data/test/unit/repository_indexing_test.rb +0 -37
  76. data/test/unit/repository_module_test.rb +0 -146
  77. data/test/unit/repository_naming_test.rb +0 -146
  78. data/test/unit/repository_response_results_test.rb +0 -98
  79. data/test/unit/repository_search_test.rb +0 -117
  80. data/test/unit/repository_serialize_test.rb +0 -57
  81. data/test/unit/repository_store_test.rb +0 -303
@@ -1,37 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryIndexingTest < Test::Unit::TestCase
4
- context "The repository index methods" do
5
- class MyDocument; end
6
-
7
- setup do
8
- @shoulda_subject = Class.new() { include Elasticsearch::Model::Indexing::ClassMethods }.new
9
- @shoulda_subject.stubs(:index_name).returns('my_index')
10
- @shoulda_subject.stubs(:document_type).returns('my_document')
11
- end
12
-
13
- should "have the convenience index management methods" do
14
- %w( create_index! delete_index! refresh_index! ).each do |method|
15
- assert_respond_to subject, method
16
- end
17
- end
18
-
19
- context "mappings" do
20
- should "configure the mappings for the type" do
21
- subject.mappings do
22
- indexes :title
23
- end
24
-
25
- assert_equal( {:"my_document"=>{:properties=>{:title=>{:type=>"text"}}}}, subject.mappings.to_hash )
26
- end
27
- end
28
-
29
- context "settings" do
30
- should "configure the settings for the index" do
31
- subject.settings foo: 'bar'
32
- assert_equal( {foo: 'bar'}, subject.settings.to_hash)
33
- end
34
- end
35
-
36
- end
37
- end
@@ -1,146 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryModuleTest < Test::Unit::TestCase
4
- context "The repository module" do
5
-
6
- class DummyModel
7
- def initialize(attributes={})
8
- @attributes = attributes
9
- end
10
-
11
- def to_hash
12
- @attributes
13
- end
14
-
15
- def inspect
16
- "<Note #{@attributes.inspect}>"
17
- end
18
- end
19
-
20
- setup do
21
- class DummyRepository
22
- include Elasticsearch::Persistence::Repository
23
- end
24
- end
25
-
26
- teardown do
27
- Elasticsearch::Persistence::RepositoryModuleTest.__send__ :remove_const, :DummyRepository
28
- end
29
-
30
- context "when included" do
31
- should "set up the gateway for the class and instance" do
32
- assert_respond_to DummyRepository, :gateway
33
- assert_respond_to DummyRepository.new, :gateway
34
-
35
- assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.gateway
36
- assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.new.gateway
37
- end
38
-
39
- should "proxy repository methods from the class to the gateway" do
40
- class DummyRepository
41
- include Elasticsearch::Persistence::Repository
42
-
43
- index :foobar
44
- klass DummyModel
45
- type :my_dummy_model
46
- mapping { indexes :title, analyzer: 'snowball' }
47
- end
48
-
49
- repository = DummyRepository.new
50
-
51
- assert_equal :foobar, DummyRepository.index
52
- assert_equal DummyModel, DummyRepository.klass
53
- assert_equal :my_dummy_model, DummyRepository.type
54
- assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
55
-
56
- assert_equal :foobar, repository.index
57
- assert_equal DummyModel, repository.klass
58
- assert_equal :my_dummy_model, repository.type
59
- assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
60
- end
61
-
62
- should "correctly delegate to the gateway" do
63
- repository = DummyRepository.new
64
- assert_instance_of Method, repository.method(:index)
65
- end
66
-
67
- should "proxy repository methods from the instance to the gateway" do
68
- class DummyRepository
69
- include Elasticsearch::Persistence::Repository
70
- end
71
-
72
- repository = DummyRepository.new
73
- repository.index :foobar
74
- repository.klass DummyModel
75
- repository.type :my_dummy_model
76
- repository.mapping { indexes :title, analyzer: 'snowball' }
77
-
78
- assert_equal :foobar, DummyRepository.index
79
- assert_equal DummyModel, DummyRepository.klass
80
- assert_equal :my_dummy_model, DummyRepository.type
81
- assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
82
-
83
- assert_equal :foobar, repository.index
84
- assert_equal DummyModel, repository.klass
85
- assert_equal :my_dummy_model, repository.type
86
- assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
87
- end
88
-
89
- should "allow to define gateway methods in the class definition via block passed to the gateway method" do
90
- class DummyRepositoryWithGatewaySerialize
91
- include Elasticsearch::Persistence::Repository
92
-
93
- gateway do
94
- def serialize(document)
95
- 'FAKE!'
96
- end
97
- end
98
- end
99
-
100
- repository = DummyRepositoryWithGatewaySerialize.new
101
- repository.client.transport.logger = Logger.new(STDERR)
102
-
103
- client = mock
104
- client.expects(:index).with do |arguments|
105
- assert_equal('xxx', arguments[:id])
106
- assert_equal('FAKE!', arguments[:body])
107
- true
108
- end
109
- repository.gateway.expects(:client).returns(client)
110
-
111
- repository.gateway.expects(:__get_id_from_document).returns('xxx')
112
-
113
- repository.save( id: '123', foo: 'bar' )
114
- end
115
- end
116
-
117
- should "allow to define gateway methods in the class definition via regular method definition" do
118
- class DummyRepositoryWithDirectSerialize
119
- include Elasticsearch::Persistence::Repository
120
-
121
- def serialize(document)
122
- 'FAKE IN CLASS!'
123
- end
124
- end
125
-
126
- repository = DummyRepositoryWithDirectSerialize.new
127
- repository.client.transport.logger = Logger.new(STDERR)
128
-
129
- client = mock
130
- client.expects(:index).with do |arguments|
131
- assert_equal('xxx', arguments[:id])
132
- assert_equal('FAKE IN CLASS!', arguments[:body])
133
- true
134
- end
135
- repository.gateway.expects(:client).returns(client)
136
-
137
- repository.gateway.expects(:__get_id_from_document).returns('xxx')
138
-
139
- repository.save( id: '123', foo: 'bar' )
140
- end
141
-
142
- should "configure the index name in the shortcut initializer" do
143
- assert_equal 'repository', Elasticsearch::Persistence::Repository.new.index_name
144
- end
145
- end
146
- end
@@ -1,146 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryNamingTest < Test::Unit::TestCase
4
- context "The repository naming" do
5
- # Fake class for the naming tests
6
- class ::Foobar; end
7
- class ::FooBar; end
8
- module ::Foo; class Bar; end; end
9
-
10
- setup do
11
- @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Naming }.new
12
- end
13
-
14
- context "get Ruby class from the Elasticsearch type" do
15
- should "get a simple class" do
16
- assert_equal Foobar, subject.__get_klass_from_type('foobar')
17
- end
18
- should "get a camelcased class" do
19
- assert_equal FooBar, subject.__get_klass_from_type('foo_bar')
20
- end
21
- should "get a namespaced class" do
22
- assert_equal Foo::Bar, subject.__get_klass_from_type('foo/bar')
23
- end
24
- should "re-raise a NameError exception" do
25
- assert_raise NameError do
26
- subject.__get_klass_from_type('foobarbazbam')
27
- end
28
- end
29
- end
30
-
31
- context "get Elasticsearch type from the Ruby class" do
32
- should "encode a simple class" do
33
- assert_equal 'foobar', subject.__get_type_from_class(Foobar)
34
- end
35
- should "encode a camelcased class" do
36
- assert_equal 'foo_bar', subject.__get_type_from_class(FooBar)
37
- end
38
- should "encode a namespaced class" do
39
- assert_equal 'foo/bar', subject.__get_type_from_class(Foo::Bar)
40
- end
41
- end
42
-
43
- context "get an ID from the document" do
44
- should "get an ID from Hash" do
45
- assert_equal 1, subject.__get_id_from_document(id: 1)
46
- assert_equal 1, subject.__get_id_from_document(_id: 1)
47
- assert_equal 1, subject.__get_id_from_document('id' => 1)
48
- assert_equal 1, subject.__get_id_from_document('_id' => 1)
49
- end
50
- end
51
-
52
- context "extract an ID from the document" do
53
- should "delete the key from theHash" do
54
- d1 = { :id => 1 }
55
- d2 = { :_id => 1 }
56
- d3 = { 'id' => 1 }
57
- d4 = { '_id' => 1 }
58
-
59
- assert_equal 1, subject.__extract_id_from_document(d1)
60
- assert_nil d1[:id]
61
-
62
- assert_equal 1, subject.__extract_id_from_document(d2)
63
- assert_nil d1[:_id]
64
-
65
- assert_equal 1, subject.__extract_id_from_document(d3)
66
- assert_nil d1['id']
67
-
68
- assert_equal 1, subject.__extract_id_from_document(d4)
69
- assert_nil d1['_id']
70
- end
71
- end
72
-
73
- context "document class name" do
74
- should "be nil by default" do
75
- assert_nil subject.klass
76
- end
77
-
78
- should "be settable" do
79
- subject.klass = Foobar
80
- assert_equal Foobar, subject.klass
81
- end
82
-
83
- should "be settable by DSL" do
84
- subject.klass Foobar
85
- assert_equal Foobar, subject.klass
86
- end
87
- end
88
-
89
- context "index_name" do
90
- should "default to the class name" do
91
- subject.instance_eval do
92
- def self.class
93
- 'FakeRepository'
94
- end
95
- end
96
-
97
- assert_equal 'fake_repository', subject.index_name
98
- end
99
-
100
- should "be settable" do
101
- subject.index_name = 'foobar1'
102
- assert_equal 'foobar1', subject.index_name
103
-
104
- subject.index_name 'foobar2'
105
- assert_equal 'foobar2', subject.index_name
106
- end
107
-
108
- should "be aliased as `index`" do
109
- subject.index_name = 'foobar1'
110
- assert_equal 'foobar1', subject.index
111
- end
112
-
113
- should "be inferred from the host class" do
114
- class ::MySpecialRepository; end
115
- subject.define_singleton_method(:host) { MySpecialRepository }
116
- assert_equal 'my_special_repository', subject.index_name
117
- end
118
- end
119
-
120
- context "document_type" do
121
- should "be nil when no klass is set" do
122
- assert_equal nil, subject.document_type
123
- end
124
-
125
- should "default to klass" do
126
- subject.klass Foobar
127
- assert_equal 'foobar', subject.document_type
128
- end
129
-
130
- should "be aliased as `type`" do
131
- subject.klass Foobar
132
- assert_equal 'foobar', subject.type
133
- end
134
-
135
- should "be settable" do
136
- subject.document_type = 'foobar'
137
- assert_equal 'foobar', subject.document_type
138
- end
139
-
140
- should "be settable by DSL" do
141
- subject.document_type 'foobar'
142
- assert_equal 'foobar', subject.document_type
143
- end
144
- end
145
- end
146
- end
@@ -1,98 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryResponseResultsTest < Test::Unit::TestCase
4
- include Elasticsearch::Persistence
5
- class MyDocument; end
6
-
7
- context "Response results" do
8
- setup do
9
- @repository = Repository.new
10
-
11
- @response = { "took" => 2,
12
- "timed_out" => false,
13
- "_shards" => {"total" => 5, "successful" => 5, "failed" => 0},
14
- "hits" =>
15
- { "total" => 2,
16
- "max_score" => 0.19,
17
- "hits" =>
18
- [{"_index" => "my_index",
19
- "_type" => "note",
20
- "_id" => "1",
21
- "_score" => 0.19,
22
- "_source" => {"id" => 1, "title" => "Test 1"}},
23
-
24
- {"_index" => "my_index",
25
- "_type" => "note",
26
- "_id" => "2",
27
- "_score" => 0.19,
28
- "_source" => {"id" => 2, "title" => "Test 2"}}
29
- ]
30
- }
31
- }
32
-
33
- @shoulda_subject = Repository::Response::Results.new @repository, @response
34
- end
35
-
36
- should "provide the access to the repository" do
37
- assert_instance_of Repository::Class, subject.repository
38
- end
39
-
40
- should "provide the access to the response" do
41
- assert_equal 5, subject.response['_shards']['total']
42
- end
43
-
44
- should "wrap the response in HashWrapper" do
45
- assert_equal 5, subject.response._shards.total
46
- end
47
-
48
- should "return the total" do
49
- assert_equal 2, subject.total
50
- end
51
-
52
- should "return the max_score" do
53
- assert_equal 0.19, subject.max_score
54
- end
55
-
56
- should "delegate methods to results" do
57
- subject.repository
58
- .expects(:deserialize)
59
- .twice
60
- .returns(MyDocument.new)
61
-
62
- assert_equal 2, subject.size
63
- assert_respond_to subject, :each
64
- end
65
-
66
- should "respond to missing" do
67
- assert_instance_of Method, subject.method(:to_a)
68
- end
69
-
70
- should "yield each object with hit" do
71
- @shoulda_subject = Repository::Response::Results.new \
72
- @repository,
73
- { 'hits' => { 'hits' => [{'_id' => '1', 'foo' => 'bar'}] } }
74
-
75
- subject.repository
76
- .expects(:deserialize)
77
- .returns('FOO')
78
-
79
- subject.each_with_hit do |object, hit|
80
- assert_equal 'FOO', object
81
- assert_equal 'bar', hit.foo
82
- end
83
- end
84
-
85
- should "map objects and hits" do
86
- @shoulda_subject = Repository::Response::Results.new \
87
- @repository,
88
- { 'hits' => { 'hits' => [{'_id' => '1', 'foo' => 'bar'}] } }
89
-
90
- subject.repository
91
- .expects(:deserialize)
92
- .returns('FOO')
93
-
94
- assert_equal ['FOO---bar'], subject.map_with_hit { |object, hit| "#{object}---#{hit.foo}" }
95
- end
96
- end
97
-
98
- end
@@ -1,117 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositorySearchTest < Test::Unit::TestCase
4
- class MyDocument; end
5
-
6
- context "The repository search" do
7
- setup do
8
- @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Search }.new
9
-
10
- @client = mock
11
- @shoulda_subject.stubs(:document_type).returns(nil)
12
- @shoulda_subject.stubs(:klass).returns(nil)
13
- @shoulda_subject.stubs(:index_name).returns('test')
14
- @shoulda_subject.stubs(:client).returns(@client)
15
- end
16
-
17
- should "search in type based on klass" do
18
- subject.expects(:klass).returns(MyDocument).at_least_once
19
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
20
-
21
- @client.expects(:search).with do |arguments|
22
- assert_equal 'test', arguments[:index]
23
- assert_equal 'my_document', arguments[:type]
24
- assert_equal({foo: 'bar'}, arguments[:body])
25
- true
26
- end
27
-
28
- subject.search foo: 'bar'
29
- end
30
-
31
- should "search in type based on document_type" do
32
- subject.expects(:document_type).returns('my_special_document').at_least_once
33
- subject.expects(:__get_type_from_class).never
34
-
35
- @client.expects(:search).with do |arguments|
36
- assert_equal 'test', arguments[:index]
37
- assert_equal 'my_special_document', arguments[:type]
38
- assert_equal({foo: 'bar'}, arguments[:body])
39
- true
40
- end
41
-
42
- subject.search foo: 'bar'
43
- end
44
-
45
- should "search across all types" do
46
- subject.expects(:document_type).returns(nil).at_least_once
47
- subject.expects(:klass).returns(nil).at_least_once
48
- subject.expects(:__get_type_from_class).never
49
-
50
- @client.expects(:search).with do |arguments|
51
- assert_equal 'test', arguments[:index]
52
- assert_equal nil, arguments[:type]
53
- assert_equal({foo: 'bar'}, arguments[:body])
54
- true
55
- end
56
-
57
- assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
58
- subject.search(foo: 'bar')
59
- end
60
-
61
- should "pass options to the client" do
62
- subject.expects(:klass).returns(nil).at_least_once
63
- subject.expects(:__get_type_from_class).never
64
-
65
- @client.expects(:search).twice.with do |arguments|
66
- assert_equal 'bambam', arguments[:routing]
67
- true
68
- end
69
-
70
- assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
71
- subject.search( {foo: 'bar'}, { routing: 'bambam' } )
72
- assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
73
- subject.search( 'foobar', { routing: 'bambam' } )
74
- end
75
-
76
- should "search with simple search" do
77
- subject.expects(:klass).returns(nil).at_least_once
78
- subject.expects(:__get_type_from_class).never
79
-
80
- @client.expects(:search).with do |arguments|
81
- assert_equal 'foobar', arguments[:q]
82
- true
83
- end
84
-
85
- assert_instance_of Elasticsearch::Persistence::Repository::Response::Results,
86
- subject.search('foobar')
87
- end
88
-
89
- should "raise error for incorrect search definitions" do
90
- subject.expects(:klass).returns(nil).at_least_once
91
- subject.expects(:__get_type_from_class).never
92
-
93
- assert_raise ArgumentError do
94
- subject.search 123
95
- end
96
- end
97
-
98
- should "return the number of domain objects" do
99
- subject.client.expects(:count).returns({'count' => 1})
100
- assert_equal 1, subject.count
101
- end
102
-
103
- should "pass arguments to count" do
104
- subject.client.expects(:count)
105
- .with do |arguments|
106
- assert_equal 'test', arguments[:index]
107
- assert_equal 'bar', arguments[:body][:query][:match][:foo]
108
- assert_equal true, arguments[:ignore_unavailable]
109
- true
110
- end
111
- .returns({'count' => 1})
112
-
113
- assert_equal 1, subject.count( { query: { match: { foo: 'bar' } } }, { ignore_unavailable: true } )
114
- end
115
- end
116
-
117
- end