elasticsearch-persistence 5.1.0 → 6.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +2 -0
  3. data/Gemfile +9 -0
  4. data/README.md +164 -323
  5. data/Rakefile +8 -8
  6. data/elasticsearch-persistence.gemspec +4 -5
  7. data/lib/elasticsearch/persistence.rb +2 -110
  8. data/lib/elasticsearch/persistence/repository.rb +212 -53
  9. data/lib/elasticsearch/persistence/repository/dsl.rb +94 -0
  10. data/lib/elasticsearch/persistence/repository/find.rb +27 -10
  11. data/lib/elasticsearch/persistence/repository/response/results.rb +17 -5
  12. data/lib/elasticsearch/persistence/repository/search.rb +15 -4
  13. data/lib/elasticsearch/persistence/repository/serialize.rb +65 -7
  14. data/lib/elasticsearch/persistence/repository/store.rb +38 -44
  15. data/lib/elasticsearch/persistence/version.rb +1 -1
  16. data/spec/repository/find_spec.rb +179 -0
  17. data/spec/repository/response/results_spec.rb +105 -0
  18. data/spec/repository/search_spec.rb +181 -0
  19. data/spec/repository/serialize_spec.rb +53 -0
  20. data/spec/repository/store_spec.rb +327 -0
  21. data/spec/repository_spec.rb +716 -0
  22. data/spec/spec_helper.rb +28 -0
  23. metadata +25 -80
  24. data/lib/elasticsearch/persistence/client.rb +0 -51
  25. data/lib/elasticsearch/persistence/model.rb +0 -153
  26. data/lib/elasticsearch/persistence/model/base.rb +0 -87
  27. data/lib/elasticsearch/persistence/model/errors.rb +0 -8
  28. data/lib/elasticsearch/persistence/model/find.rb +0 -180
  29. data/lib/elasticsearch/persistence/model/rails.rb +0 -47
  30. data/lib/elasticsearch/persistence/model/store.rb +0 -254
  31. data/lib/elasticsearch/persistence/model/utils.rb +0 -0
  32. data/lib/elasticsearch/persistence/repository/class.rb +0 -71
  33. data/lib/elasticsearch/persistence/repository/naming.rb +0 -115
  34. data/lib/rails/generators/elasticsearch/model/model_generator.rb +0 -21
  35. data/lib/rails/generators/elasticsearch/model/templates/model.rb.tt +0 -9
  36. data/lib/rails/generators/elasticsearch_generator.rb +0 -2
  37. data/test/integration/model/model_basic_test.rb +0 -238
  38. data/test/integration/repository/custom_class_test.rb +0 -85
  39. data/test/integration/repository/customized_class_test.rb +0 -82
  40. data/test/integration/repository/default_class_test.rb +0 -116
  41. data/test/integration/repository/virtus_model_test.rb +0 -118
  42. data/test/test_helper.rb +0 -55
  43. data/test/unit/model_base_test.rb +0 -72
  44. data/test/unit/model_find_test.rb +0 -153
  45. data/test/unit/model_gateway_test.rb +0 -101
  46. data/test/unit/model_rails_test.rb +0 -112
  47. data/test/unit/model_store_test.rb +0 -576
  48. data/test/unit/persistence_test.rb +0 -32
  49. data/test/unit/repository_class_test.rb +0 -51
  50. data/test/unit/repository_client_test.rb +0 -32
  51. data/test/unit/repository_find_test.rb +0 -388
  52. data/test/unit/repository_indexing_test.rb +0 -37
  53. data/test/unit/repository_module_test.rb +0 -146
  54. data/test/unit/repository_naming_test.rb +0 -146
  55. data/test/unit/repository_response_results_test.rb +0 -98
  56. data/test/unit/repository_search_test.rb +0 -117
  57. data/test/unit/repository_serialize_test.rb +0 -57
  58. data/test/unit/repository_store_test.rb +0 -303
@@ -1,85 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Elasticsearch
4
- module Persistence
5
- class RepositoryCustomClassIntegrationTest < Elasticsearch::Test::IntegrationTestCase
6
-
7
- class ::MyNote
8
- attr_reader :attributes
9
-
10
- def initialize(attributes={})
11
- @attributes = Hashie::Mash.new(attributes)
12
- end
13
-
14
- def method_missing(method_name, *arguments, &block)
15
- attributes.respond_to?(method_name) ? attributes.__send__(method_name, *arguments, &block) : super
16
- end
17
-
18
- def respond_to?(method_name, include_private=false)
19
- attributes.respond_to?(method_name) || super
20
- end
21
-
22
- def to_hash
23
- @attributes
24
- end
25
- end
26
-
27
- context "A custom repository class" do
28
- setup do
29
- class ::MyNotesRepository
30
- include Elasticsearch::Persistence::Repository
31
-
32
- klass MyNote
33
-
34
- settings number_of_shards: 1 do
35
- mapping do
36
- indexes :title, analyzer: 'snowball'
37
- end
38
- end
39
-
40
- create_index!
41
-
42
- def deserialize(document)
43
- klass.new document.merge(document['_source'])
44
- end
45
- end
46
-
47
- @repository = MyNotesRepository.new
48
-
49
- @repository.client.cluster.health wait_for_status: 'yellow'
50
- end
51
-
52
- should "save the object under a correct index and type" do
53
- @repository.save MyNote.new(id: '1', title: 'Test')
54
- result = @repository.find(1)
55
-
56
- assert_instance_of MyNote, result
57
- assert_equal 'Test', result.title
58
-
59
- assert_not_nil Elasticsearch::Persistence.client.get index: 'my_notes_repository',
60
- type: 'my_note',
61
- id: '1'
62
- end
63
-
64
- should "delete the object" do
65
- note = MyNote.new id: 1, title: 'Test'
66
- @repository.save note
67
-
68
- assert_not_nil @repository.find(1)
69
-
70
- @repository.delete(note)
71
- assert_raise(Elasticsearch::Persistence::Repository::DocumentNotFound) { @repository.find(1) }
72
- end
73
-
74
- should "retrieve the object via a search query" do
75
- note = MyNote.new title: 'Testing'
76
- @repository.save note, refresh: true
77
-
78
- results = @repository.search query: { match: { title: 'Test' } }
79
- assert_equal 'Testing', results.first.title
80
- end
81
- end
82
-
83
- end
84
- end
85
- end
@@ -1,82 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Elasticsearch
4
- module Persistence
5
- class RepositoryCustomizedClassIntegrationTest < Elasticsearch::Test::IntegrationTestCase
6
-
7
- module ::My
8
- class Note
9
- attr_reader :attributes
10
-
11
- def initialize(attributes={})
12
- @attributes = attributes
13
- end
14
-
15
- def to_hash
16
- @attributes
17
- end
18
- end
19
- end
20
-
21
- context "A custom repository class" do
22
- setup do
23
- @repository = Elasticsearch::Persistence::Repository.new do
24
- index 'my_notes'
25
- type 'my_note'
26
- klass My::Note
27
-
28
- settings number_of_shards: 1 do
29
- mapping do
30
- indexes :title, analyzer: 'snowball'
31
- end
32
- end
33
-
34
- create_index!
35
- end
36
-
37
- @repository.client.cluster.health wait_for_status: 'yellow'
38
- end
39
-
40
- should "save the object under a correct index and type" do
41
- @repository.save My::Note.new(id: '1', title: 'Test')
42
-
43
- assert_instance_of My::Note, @repository.find(1)
44
- assert_not_nil Elasticsearch::Persistence.client.get index: 'my_notes', type: 'my_note', id: '1'
45
- end
46
-
47
- should "update the document" do
48
- @repository.save Note.new(id: 1, title: 'Test')
49
-
50
- @repository.update 1, doc: { title: 'UPDATED' }
51
- assert_equal 'UPDATED', @repository.find(1).attributes['title']
52
- end
53
-
54
- should "update the document with a script" do
55
- @repository.save Note.new(id: 1, title: 'Test')
56
-
57
- @repository.update 1, script: 'ctx._source.title = "UPDATED"'
58
- assert_equal 'UPDATED', @repository.find(1).attributes['title']
59
- end
60
-
61
- should "delete the object" do
62
- note = My::Note.new id: 1, title: 'Test'
63
- @repository.save note
64
-
65
- assert_not_nil @repository.find(1)
66
-
67
- @repository.delete(note)
68
- assert_raise(Elasticsearch::Persistence::Repository::DocumentNotFound) { @repository.find(1) }
69
- end
70
-
71
- should "create the index with correct mapping" do
72
- note = My::Note.new title: 'Testing'
73
- @repository.save note, refresh: true
74
-
75
- results = @repository.search query: { match: { title: 'Test' } }
76
- assert_equal 'Testing', results.first.attributes['title']
77
- end
78
- end
79
-
80
- end
81
- end
82
- end
@@ -1,116 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Elasticsearch
4
- module Persistence
5
- class RepositoryDefaultClassIntegrationTest < Elasticsearch::Test::IntegrationTestCase
6
-
7
- class ::Note
8
- attr_reader :attributes
9
-
10
- def initialize(attributes={})
11
- @attributes = attributes
12
- end
13
-
14
- def to_hash
15
- @attributes
16
- end
17
- end
18
-
19
- context "The default repository class" do
20
- setup do
21
- @repository = Elasticsearch::Persistence::Repository.new
22
- @repository.client.cluster.health wait_for_status: 'yellow'
23
- end
24
-
25
- should "save the object with a custom ID and find it" do
26
- @repository.save Note.new(id: '1', title: 'Test')
27
-
28
- assert_equal 'Test', @repository.find(1).attributes['title']
29
- end
30
-
31
- should "save the object with an auto-generated ID and find it" do
32
- response = @repository.save Note.new(title: 'Test')
33
-
34
- assert_equal 'Test', @repository.find(response['_id']).attributes['title']
35
- end
36
-
37
- should "update the document" do
38
- @repository.save Note.new(id: 1, title: 'Test')
39
-
40
- @repository.update 1, type: 'note', doc: { title: 'UPDATED' }
41
- assert_equal 'UPDATED', @repository.find(1).attributes['title']
42
- end
43
-
44
- should "update the document with a script" do
45
- @repository.save Note.new(id: 1, title: 'Test')
46
-
47
- @repository.update 1, type: 'note', script: 'ctx._source.title = "UPDATED"'
48
- assert_equal 'UPDATED', @repository.find(1).attributes['title']
49
- end
50
-
51
- should "save the document with an upsert" do
52
- @repository.update 1, type: 'note', script: 'ctx._source.clicks += 1', upsert: { clicks: 1 }
53
- assert_equal 1, @repository.find(1).attributes['clicks']
54
- end
55
-
56
- should "delete an object" do
57
- note = Note.new(id: '1', title: 'Test')
58
-
59
- @repository.save(note)
60
- assert_not_nil @repository.find(1)
61
- @repository.delete(note)
62
- assert_raise(Elasticsearch::Persistence::Repository::DocumentNotFound) { @repository.find(1) }
63
- end
64
-
65
- should "find multiple objects" do
66
- (1..5).each { |i| @repository.save Note.new(id: i, title: "Test #{i}") }
67
-
68
- assert_equal 5, @repository.find(1, 2, 3, 4, 5).size
69
- assert_equal 5, @repository.find([1, 2, 3, 4, 5]).size
70
- end
71
-
72
- should "pass options to save and find" do
73
- note = Note.new(id: '1', title: 'Test')
74
- @repository.save note, routing: 'ABC'
75
-
76
- @repository.client.cluster.health level: 'indices', wait_for_status: 'yellow'
77
-
78
- assert_raise Elasticsearch::Persistence::Repository::DocumentNotFound do
79
- @repository.find(1, routing: 'DEF')
80
- end
81
-
82
- assert_nothing_raised do
83
- note = @repository.find(1, routing: 'ABC')
84
- assert_instance_of Note, note
85
- end
86
- end
87
-
88
- should "find notes with full text search" do
89
- @repository.save Note.new(title: 'Test')
90
- @repository.save Note.new(title: 'Test Test')
91
- @repository.save Note.new(title: 'Crust')
92
- @repository.client.indices.refresh index: @repository.index_name
93
-
94
- results = @repository.search 'test'
95
- assert_equal 2, results.size
96
-
97
- results = @repository.search query: { match: { title: 'Test' } }
98
- assert_equal 2, results.size
99
- end
100
-
101
- should "count notes" do
102
- @repository.save Note.new(title: 'Test')
103
- @repository.client.indices.refresh index: @repository.index_name
104
- assert_equal 1, @repository.count
105
- end
106
-
107
- should "save and find a plain hash" do
108
- @repository.save id: 1, title: 'Hash'
109
- result = @repository.find(1)
110
- assert_equal 'Hash', result['_source']['title']
111
- end
112
- end
113
-
114
- end
115
- end
116
- end
@@ -1,118 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'virtus'
4
-
5
- module Elasticsearch
6
- module Persistence
7
- class RepositoryWithVirtusIntegrationTest < Elasticsearch::Test::IntegrationTestCase
8
-
9
- class ::Page
10
- include Virtus.model
11
-
12
- attribute :id, String, writer: :private
13
- attribute :title, String
14
- attribute :views, Integer, default: 0
15
- attribute :published, Boolean, default: false
16
- attribute :slug, String, default: lambda { |page, attr| page.title.downcase.gsub(' ', '-') }
17
-
18
- def set_id(id)
19
- self.id = id
20
- end
21
- end
22
-
23
- context "The repository with a Virtus model" do
24
- setup do
25
- @repository = Elasticsearch::Persistence::Repository.new do
26
- index :pages
27
- klass Page
28
-
29
- def deserialize(document)
30
- page = klass.new document['_source']
31
- page.set_id document['_id']
32
- page
33
- end
34
- end
35
- end
36
-
37
- should "save and find the object" do
38
- page = Page.new title: 'Test Page'
39
-
40
- response = @repository.save page
41
- id = response['_id']
42
-
43
- result = @repository.find(id)
44
-
45
- assert_instance_of Page, result
46
- assert_equal 'Test Page', result.title
47
- assert_equal 0, result.views
48
-
49
- assert_not_nil Elasticsearch::Persistence.client.get index: 'pages',
50
- type: 'page',
51
- id: id
52
- end
53
-
54
- should "update the object with a partial document" do
55
- response = @repository.save Page.new(title: 'Test')
56
- id = response['_id']
57
-
58
- page = @repository.find(id)
59
-
60
- assert_equal 'Test', page.title
61
-
62
- @repository.update page.id, doc: { title: 'UPDATE' }
63
-
64
- page = @repository.find(id)
65
- assert_equal 'UPDATE', page.title
66
- end
67
-
68
- should "update the object with a Hash" do
69
- response = @repository.save Page.new(title: 'Test')
70
- id = response['_id']
71
-
72
- page = @repository.find(id)
73
-
74
- assert_equal 'Test', page.title
75
-
76
- @repository.update id: page.id, title: 'UPDATE'
77
-
78
- page = @repository.find(id)
79
- assert_equal 'UPDATE', page.title
80
- end
81
-
82
- should "update the object with a script" do
83
- response = @repository.save Page.new(title: 'Test Page')
84
- id = response['_id']
85
-
86
- page = @repository.find(id)
87
-
88
- assert_not_nil page.id
89
- assert_equal 0, page.views
90
-
91
- @repository.update page.id, script: 'ctx._source.views += 1'
92
-
93
- page = @repository.find(id)
94
- assert_equal 1, page.views
95
-
96
- @repository.update id: page.id, script: 'ctx._source.views += 1'
97
-
98
- page = @repository.find(id)
99
- assert_equal 2, page.views
100
- end
101
-
102
- should "update the object with a script and params" do
103
- response = @repository.save Page.new(title: 'Test Page')
104
-
105
- @repository.update id: response['_id'],
106
- script: {
107
- inline: 'ctx._source.views += params.count',
108
- params: { count: 3 }
109
- }
110
-
111
- page = @repository.find(response['_id'])
112
- assert_equal 3, page.views
113
- end
114
- end
115
-
116
- end
117
- end
118
- end
@@ -1,55 +0,0 @@
1
- RUBY_1_8 = defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
2
-
3
- exit(0) if RUBY_1_8
4
-
5
- $LOAD_PATH.unshift File.expand_path('../../../elasticsearch-model/lib', __FILE__) if File.exists? File.expand_path('../../../elasticsearch-model/lib', __FILE__)
6
-
7
- require 'simplecov' and SimpleCov.start { add_filter "/test|test_/" } if ENV["COVERAGE"]
8
-
9
- # Register `at_exit` handler for integration tests shutdown.
10
- # MUST be called before requiring `test/unit`.
11
- at_exit { Elasticsearch::Test::IntegrationTestCase.__run_at_exit_hooks } if ENV['SERVER']
12
-
13
- if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
14
- require 'test-unit'
15
- require 'mocha/test_unit'
16
- else
17
- require 'minitest/autorun'
18
- require 'mocha/mini_test'
19
- end
20
-
21
- require 'shoulda-context'
22
-
23
- require 'turn' unless ENV["TM_FILEPATH"] || ENV["NOTURN"] || defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
24
-
25
- require 'ansi'
26
- require 'oj'
27
-
28
- require 'elasticsearch/extensions/test/cluster'
29
- require 'elasticsearch/extensions/test/startup_shutdown'
30
-
31
- require 'elasticsearch/persistence'
32
-
33
- module Elasticsearch
34
- module Test
35
- class IntegrationTestCase < ::Test::Unit::TestCase
36
- extend Elasticsearch::Extensions::Test::StartupShutdown
37
-
38
- startup { Elasticsearch::Extensions::Test::Cluster.start(nodes: 1) if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running? }
39
- shutdown { Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] && started? }
40
- context "IntegrationTest" do; should "noop on Ruby 1.8" do; end; end if RUBY_1_8
41
-
42
- def setup
43
- tracer = ::Logger.new(STDERR)
44
- tracer.formatter = lambda { |s, d, p, m| "#{m.gsub(/^.*$/) { |n| ' ' + n }.ansi(:faint)}\n" }
45
- Elasticsearch::Persistence.client = Elasticsearch::Client.new \
46
- host: "localhost:#{(ENV['TEST_CLUSTER_PORT'] || 9250)}",
47
- tracer: (ENV['QUIET'] ? nil : tracer)
48
- end
49
-
50
- def teardown
51
- Elasticsearch::Persistence.client.indices.delete index: '_all'
52
- end
53
- end
54
- end
55
- end