elasticsearch-model 0.1.9 → 7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -2
  3. data/Gemfile +22 -0
  4. data/LICENSE.txt +199 -10
  5. data/README.md +96 -43
  6. data/Rakefile +49 -35
  7. data/elasticsearch-model.gemspec +53 -41
  8. data/examples/activerecord_article.rb +18 -1
  9. data/examples/activerecord_associations.rb +86 -17
  10. data/examples/activerecord_custom_analyzer.rb +152 -0
  11. data/examples/activerecord_mapping_completion.rb +33 -16
  12. data/examples/activerecord_mapping_edge_ngram.rb +118 -0
  13. data/examples/couchbase_article.rb +17 -0
  14. data/examples/datamapper_article.rb +28 -1
  15. data/examples/mongoid_article.rb +17 -0
  16. data/examples/ohm_article.rb +17 -0
  17. data/examples/riak_article.rb +17 -0
  18. data/gemfiles/3.0.gemfile +23 -1
  19. data/gemfiles/4.0.gemfile +25 -1
  20. data/gemfiles/5.0.gemfile +35 -0
  21. data/gemfiles/6.0.gemfile +36 -0
  22. data/lib/elasticsearch/model/adapter.rb +17 -0
  23. data/lib/elasticsearch/model/adapters/active_record.rb +31 -27
  24. data/lib/elasticsearch/model/adapters/default.rb +17 -0
  25. data/lib/elasticsearch/model/adapters/mongoid.rb +27 -3
  26. data/lib/elasticsearch/model/adapters/multiple.rb +29 -4
  27. data/lib/elasticsearch/model/callbacks.rb +17 -0
  28. data/lib/elasticsearch/model/client.rb +17 -0
  29. data/lib/elasticsearch/model/ext/active_record.rb +17 -0
  30. data/lib/elasticsearch/model/hash_wrapper.rb +32 -0
  31. data/lib/elasticsearch/model/importing.rb +61 -17
  32. data/lib/elasticsearch/model/indexing.rb +87 -49
  33. data/lib/elasticsearch/model/multimodel.rb +17 -0
  34. data/lib/elasticsearch/model/naming.rb +33 -2
  35. data/lib/elasticsearch/model/proxy.rb +61 -18
  36. data/lib/elasticsearch/model/response/aggregations.rb +55 -0
  37. data/lib/elasticsearch/model/response/base.rb +25 -3
  38. data/lib/elasticsearch/model/response/pagination/kaminari.rb +126 -0
  39. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +112 -0
  40. data/lib/elasticsearch/model/response/pagination.rb +19 -192
  41. data/lib/elasticsearch/model/response/records.rb +17 -1
  42. data/lib/elasticsearch/model/response/result.rb +19 -2
  43. data/lib/elasticsearch/model/response/results.rb +17 -0
  44. data/lib/elasticsearch/model/response/suggestions.rb +20 -1
  45. data/lib/elasticsearch/model/response.rb +28 -10
  46. data/lib/elasticsearch/model/searching.rb +17 -0
  47. data/lib/elasticsearch/model/serializing.rb +17 -0
  48. data/lib/elasticsearch/model/version.rb +18 -1
  49. data/lib/elasticsearch/model.rb +36 -39
  50. data/spec/elasticsearch/model/adapter_spec.rb +136 -0
  51. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +351 -0
  52. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +395 -0
  53. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +35 -0
  54. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +193 -0
  55. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +127 -0
  56. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +55 -0
  57. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +332 -0
  58. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +92 -0
  59. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +78 -0
  60. data/spec/elasticsearch/model/adapters/active_record_spec.rb +224 -0
  61. data/spec/elasticsearch/model/adapters/default_spec.rb +58 -0
  62. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +284 -0
  63. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +83 -0
  64. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +252 -0
  65. data/spec/elasticsearch/model/adapters/multiple_spec.rb +142 -0
  66. data/spec/elasticsearch/model/callbacks_spec.rb +50 -0
  67. data/spec/elasticsearch/model/client_spec.rb +83 -0
  68. data/spec/elasticsearch/model/hash_wrapper_spec.rb +29 -0
  69. data/spec/elasticsearch/model/importing_spec.rb +243 -0
  70. data/spec/elasticsearch/model/indexing_spec.rb +1014 -0
  71. data/spec/elasticsearch/model/module_spec.rb +94 -0
  72. data/spec/elasticsearch/model/multimodel_spec.rb +72 -0
  73. data/spec/elasticsearch/model/naming_spec.rb +203 -0
  74. data/spec/elasticsearch/model/proxy_spec.rb +124 -0
  75. data/spec/elasticsearch/model/response/aggregations_spec.rb +83 -0
  76. data/spec/elasticsearch/model/response/base_spec.rb +107 -0
  77. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +472 -0
  78. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +279 -0
  79. data/spec/elasticsearch/model/response/records_spec.rb +135 -0
  80. data/spec/elasticsearch/model/response/response_spec.rb +148 -0
  81. data/spec/elasticsearch/model/response/result_spec.rb +139 -0
  82. data/spec/elasticsearch/model/response/results_spec.rb +73 -0
  83. data/spec/elasticsearch/model/searching_search_request_spec.rb +129 -0
  84. data/spec/elasticsearch/model/searching_spec.rb +66 -0
  85. data/spec/elasticsearch/model/serializing_spec.rb +39 -0
  86. data/spec/spec_helper.rb +193 -0
  87. data/spec/support/app/answer.rb +50 -0
  88. data/spec/support/app/article.rb +39 -0
  89. data/spec/support/app/article_for_pagination.rb +29 -0
  90. data/spec/support/app/article_no_type.rb +37 -0
  91. data/spec/support/app/article_with_custom_serialization.rb +30 -0
  92. data/spec/support/app/article_with_dynamic_index_name.rb +32 -0
  93. data/spec/support/app/author.rb +26 -0
  94. data/spec/support/app/authorship.rb +21 -0
  95. data/spec/support/app/category.rb +20 -0
  96. data/spec/support/app/comment.rb +20 -0
  97. data/spec/support/app/episode.rb +28 -0
  98. data/spec/support/app/image.rb +36 -0
  99. data/spec/support/app/import_article.rb +29 -0
  100. data/spec/support/app/mongoid_article.rb +38 -0
  101. data/spec/support/app/namespaced_book.rb +27 -0
  102. data/spec/support/app/parent_and_child_searchable.rb +41 -0
  103. data/spec/support/app/post.rb +31 -0
  104. data/spec/support/app/question.rb +44 -0
  105. data/spec/support/app/searchable.rb +65 -0
  106. data/spec/support/app/series.rb +28 -0
  107. data/spec/support/app.rb +46 -0
  108. data/spec/support/model.json +1 -0
  109. data/{test → spec}/support/model.yml +0 -0
  110. metadata +175 -121
  111. data/test/integration/active_record_associations_parent_child.rb +0 -139
  112. data/test/integration/active_record_associations_test.rb +0 -326
  113. data/test/integration/active_record_basic_test.rb +0 -234
  114. data/test/integration/active_record_custom_serialization_test.rb +0 -62
  115. data/test/integration/active_record_import_test.rb +0 -109
  116. data/test/integration/active_record_namespaced_model_test.rb +0 -49
  117. data/test/integration/active_record_pagination_test.rb +0 -145
  118. data/test/integration/dynamic_index_name_test.rb +0 -47
  119. data/test/integration/mongoid_basic_test.rb +0 -177
  120. data/test/integration/multiple_models_test.rb +0 -172
  121. data/test/support/model.json +0 -1
  122. data/test/test_helper.rb +0 -93
  123. data/test/unit/adapter_active_record_test.rb +0 -157
  124. data/test/unit/adapter_default_test.rb +0 -41
  125. data/test/unit/adapter_mongoid_test.rb +0 -104
  126. data/test/unit/adapter_multiple_test.rb +0 -106
  127. data/test/unit/adapter_test.rb +0 -69
  128. data/test/unit/callbacks_test.rb +0 -31
  129. data/test/unit/client_test.rb +0 -27
  130. data/test/unit/importing_test.rb +0 -203
  131. data/test/unit/indexing_test.rb +0 -650
  132. data/test/unit/module_test.rb +0 -57
  133. data/test/unit/multimodel_test.rb +0 -38
  134. data/test/unit/naming_test.rb +0 -103
  135. data/test/unit/proxy_test.rb +0 -100
  136. data/test/unit/response_base_test.rb +0 -40
  137. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  138. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  139. data/test/unit/response_records_test.rb +0 -91
  140. data/test/unit/response_result_test.rb +0 -90
  141. data/test/unit/response_results_test.rb +0 -31
  142. data/test/unit/response_test.rb +0 -104
  143. data/test/unit/searching_search_request_test.rb +0 -78
  144. data/test/unit/searching_test.rb +0 -41
  145. data/test/unit/serializing_test.rb +0 -17
@@ -1,38 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::MultimodelTest < Test::Unit::TestCase
4
-
5
- context "Multimodel class" do
6
- setup do
7
- title = stub('Foo', index_name: 'foo_index', document_type: 'foo')
8
- series = stub('Bar', index_name: 'bar_index', document_type: 'bar')
9
- @multimodel = Elasticsearch::Model::Multimodel.new(title, series)
10
- end
11
-
12
- should "have an index_name" do
13
- assert_equal ['foo_index', 'bar_index'], @multimodel.index_name
14
- end
15
-
16
- should "have a document_type" do
17
- assert_equal ['foo', 'bar'], @multimodel.document_type
18
- end
19
-
20
- should "have a client" do
21
- assert_equal Elasticsearch::Model.client, @multimodel.client
22
- end
23
-
24
- should "include models in the registry" do
25
- class ::JustAModel
26
- include Elasticsearch::Model
27
- end
28
-
29
- class ::JustAnotherModel
30
- include Elasticsearch::Model
31
- end
32
-
33
- multimodel = Elasticsearch::Model::Multimodel.new
34
- assert multimodel.models.include?(::JustAModel)
35
- assert multimodel.models.include?(::JustAnotherModel)
36
- end
37
- end
38
- end
@@ -1,103 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::NamingTest < Test::Unit::TestCase
4
- context "Naming module" do
5
- class ::DummyNamingModel
6
- extend ActiveModel::Naming
7
-
8
- extend Elasticsearch::Model::Naming::ClassMethods
9
- include Elasticsearch::Model::Naming::InstanceMethods
10
- end
11
-
12
- module ::MyNamespace
13
- class DummyNamingModelInNamespace
14
- extend ActiveModel::Naming
15
-
16
- extend Elasticsearch::Model::Naming::ClassMethods
17
- include Elasticsearch::Model::Naming::InstanceMethods
18
- end
19
- end
20
-
21
- should "return the default index_name" do
22
- assert_equal 'dummy_naming_models', DummyNamingModel.index_name
23
- assert_equal 'dummy_naming_models', DummyNamingModel.new.index_name
24
- end
25
-
26
- should "return the sanitized default index_name for namespaced model" do
27
- assert_equal 'my_namespace-dummy_naming_model_in_namespaces', ::MyNamespace::DummyNamingModelInNamespace.index_name
28
- assert_equal 'my_namespace-dummy_naming_model_in_namespaces', ::MyNamespace::DummyNamingModelInNamespace.new.index_name
29
- end
30
-
31
- should "return the default document_type" do
32
- assert_equal 'dummy_naming_model', DummyNamingModel.document_type
33
- assert_equal 'dummy_naming_model', DummyNamingModel.new.document_type
34
- end
35
-
36
- should "set and return the index_name" do
37
- DummyNamingModel.index_name 'foobar'
38
- assert_equal 'foobar', DummyNamingModel.index_name
39
-
40
- d = DummyNamingModel.new
41
- d.index_name 'foobar_d'
42
- assert_equal 'foobar_d', d.index_name
43
-
44
- modifier = 'r'
45
- d.index_name Proc.new{ "foobar_#{modifier}" }
46
- assert_equal 'foobar_r', d.index_name
47
-
48
- modifier = 'z'
49
- assert_equal 'foobar_z', d.index_name
50
-
51
- modifier = 'f'
52
- d.index_name { "foobar_#{modifier}" }
53
- assert_equal 'foobar_f', d.index_name
54
-
55
- modifier = 't'
56
- assert_equal 'foobar_t', d.index_name
57
- end
58
-
59
- should "set the index_name with setter" do
60
- DummyNamingModel.index_name = 'foobar_index_S'
61
- assert_equal 'foobar_index_S', DummyNamingModel.index_name
62
-
63
- d = DummyNamingModel.new
64
- d.index_name = 'foobar_index_s'
65
- assert_equal 'foobar_index_s', d.index_name
66
-
67
- assert_equal 'foobar_index_S', DummyNamingModel.index_name
68
-
69
- modifier2 = 'y'
70
- DummyNamingModel.index_name = Proc.new{ "foobar_index_#{modifier2}" }
71
- assert_equal 'foobar_index_y', DummyNamingModel.index_name
72
-
73
- modifier = 'r'
74
- d.index_name = Proc.new{ "foobar_index_#{modifier}" }
75
- assert_equal 'foobar_index_r', d.index_name
76
-
77
- modifier = 'z'
78
- assert_equal 'foobar_index_z', d.index_name
79
-
80
- assert_equal 'foobar_index_y', DummyNamingModel.index_name
81
- end
82
-
83
- should "set and return the document_type" do
84
- DummyNamingModel.document_type 'foobar'
85
- assert_equal 'foobar', DummyNamingModel.document_type
86
-
87
- d = DummyNamingModel.new
88
- d.document_type 'foobar_d'
89
- assert_equal 'foobar_d', d.document_type
90
- end
91
-
92
- should "set the document_type with setter" do
93
- DummyNamingModel.document_type = 'foobar_type_S'
94
- assert_equal 'foobar_type_S', DummyNamingModel.document_type
95
-
96
- d = DummyNamingModel.new
97
- d.document_type = 'foobar_type_s'
98
- assert_equal 'foobar_type_s', d.document_type
99
-
100
- assert_equal 'foobar_type_S', DummyNamingModel.document_type
101
- end
102
- end
103
- end
@@ -1,100 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::SearchTest < Test::Unit::TestCase
4
- context "Searching module" do
5
- class ::DummyProxyModel
6
- include Elasticsearch::Model::Proxy
7
-
8
- def self.foo
9
- 'classy foo'
10
- end
11
-
12
- def bar
13
- 'insta barr'
14
- end
15
-
16
- def as_json(options)
17
- {foo: 'bar'}
18
- end
19
- end
20
-
21
- class ::DummyProxyModelWithCallbacks
22
- def self.before_save(&block)
23
- (@callbacks ||= {})[block.hash] = block
24
- end
25
-
26
- def changed_attributes; [:foo]; end
27
-
28
- def changes
29
- {:foo => ['One', 'Two']}
30
- end
31
- end
32
-
33
- should "setup the class proxy method" do
34
- assert_respond_to DummyProxyModel, :__elasticsearch__
35
- end
36
-
37
- should "setup the instance proxy method" do
38
- assert_respond_to DummyProxyModel.new, :__elasticsearch__
39
- end
40
-
41
- should "register the hook for before_save callback" do
42
- ::DummyProxyModelWithCallbacks.expects(:before_save).returns(true)
43
- DummyProxyModelWithCallbacks.__send__ :include, Elasticsearch::Model::Proxy
44
- end
45
-
46
- should "set the @__changed_attributes variable before save" do
47
- instance = ::DummyProxyModelWithCallbacks.new
48
- instance.__elasticsearch__.expects(:instance_variable_set).with do |name, value|
49
- assert_equal :@__changed_attributes, name
50
- assert_equal({foo: 'Two'}, value)
51
- true
52
- end
53
-
54
- ::DummyProxyModelWithCallbacks.__send__ :include, Elasticsearch::Model::Proxy
55
-
56
- ::DummyProxyModelWithCallbacks.instance_variable_get(:@callbacks).each do |n,b|
57
- instance.instance_eval(&b)
58
- end
59
- end
60
-
61
- should "delegate methods to the target" do
62
- assert_respond_to DummyProxyModel.__elasticsearch__, :foo
63
- assert_respond_to DummyProxyModel.new.__elasticsearch__, :bar
64
-
65
- assert_raise(NoMethodError) { DummyProxyModel.__elasticsearch__.xoxo }
66
- assert_raise(NoMethodError) { DummyProxyModel.new.__elasticsearch__.xoxo }
67
-
68
- assert_equal 'classy foo', DummyProxyModel.__elasticsearch__.foo
69
- assert_equal 'insta barr', DummyProxyModel.new.__elasticsearch__.bar
70
- end
71
-
72
- should "reset the proxy target for duplicates" do
73
- model = DummyProxyModel.new
74
- model_target = model.__elasticsearch__.target
75
- duplicate = model.dup
76
- duplicate_target = duplicate.__elasticsearch__.target
77
-
78
- assert_not_equal model, duplicate
79
- assert_equal model, model_target
80
- assert_equal duplicate, duplicate_target
81
- end
82
-
83
- should "return the proxy class from instance proxy" do
84
- assert_equal Elasticsearch::Model::Proxy::ClassMethodsProxy, DummyProxyModel.new.__elasticsearch__.class.class
85
- end
86
-
87
- should "return the origin class from instance proxy" do
88
- assert_equal DummyProxyModel, DummyProxyModel.new.__elasticsearch__.klass
89
- end
90
-
91
- should "delegate as_json from the proxy to target" do
92
- assert_equal({foo: 'bar'}, DummyProxyModel.new.__elasticsearch__.as_json)
93
- end
94
-
95
- should "have inspect method indicating the proxy" do
96
- assert_match /PROXY/, DummyProxyModel.__elasticsearch__.inspect
97
- assert_match /PROXY/, DummyProxyModel.new.__elasticsearch__.inspect
98
- end
99
- end
100
- end
@@ -1,40 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::BaseTest < Test::Unit::TestCase
4
- context "Response base module" do
5
- class OriginClass
6
- def self.index_name; 'foo'; end
7
- def self.document_type; 'bar'; end
8
- end
9
-
10
- class DummyBaseClass
11
- include Elasticsearch::Model::Response::Base
12
- end
13
-
14
- RESPONSE = { 'hits' => { 'total' => 123, 'max_score' => 456, 'hits' => [] } }
15
-
16
- setup do
17
- @search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
18
- @response = Elasticsearch::Model::Response::Response.new OriginClass, @search
19
- @search.stubs(:execute!).returns(RESPONSE)
20
- end
21
-
22
- should "access klass, response, total and max_score" do
23
- r = DummyBaseClass.new OriginClass, @response
24
-
25
- assert_equal OriginClass, r.klass
26
- assert_equal @response, r.response
27
- assert_equal RESPONSE, r.response.response
28
- assert_equal 123, r.total
29
- assert_equal 456, r.max_score
30
- end
31
-
32
- should "have abstract methods results and records" do
33
- r = DummyBaseClass.new OriginClass, @response
34
-
35
- assert_raise(Elasticsearch::Model::NotImplemented) { |e| r.results }
36
- assert_raise(Elasticsearch::Model::NotImplemented) { |e| r.records }
37
- end
38
-
39
- end
40
- end