elastic_record 1.4.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/README.rdoc +8 -8
  4. data/elastic_record.gemspec +2 -2
  5. data/lib/elastic_record/index/deferred.rb +2 -2
  6. data/lib/elastic_record/index/documents.rb +2 -2
  7. data/lib/elastic_record/index/manage.rb +13 -7
  8. data/lib/elastic_record/index/mapping.rb +6 -3
  9. data/lib/elastic_record/index/percolator.rb +12 -14
  10. data/lib/elastic_record/index/warmer.rb +7 -7
  11. data/lib/elastic_record/index.rb +1 -1
  12. data/lib/elastic_record/relation/none.rb +4 -0
  13. data/lib/elastic_record/relation/search_methods.rb +14 -0
  14. data/lib/elastic_record/relation/value_methods.rb +1 -1
  15. data/lib/elastic_record/relation.rb +4 -0
  16. data/test/elastic_record/callbacks_test.rb +1 -1
  17. data/test/elastic_record/config_test.rb +1 -1
  18. data/test/elastic_record/connection_test.rb +1 -1
  19. data/test/elastic_record/index/configurator_test.rb +1 -1
  20. data/test/elastic_record/index/documents_test.rb +6 -6
  21. data/test/elastic_record/index/manage_test.rb +3 -3
  22. data/test/elastic_record/index/mapping_test.rb +4 -6
  23. data/test/elastic_record/index/percolator_test.rb +14 -3
  24. data/test/elastic_record/index/settings_test.rb +1 -1
  25. data/test/elastic_record/index/warmer_test.rb +1 -1
  26. data/test/elastic_record/index_test.rb +1 -1
  27. data/test/elastic_record/integration/active_record_test.rb +2 -2
  28. data/test/elastic_record/lucene_test.rb +1 -1
  29. data/test/elastic_record/model_test.rb +1 -1
  30. data/test/elastic_record/railties/controller_runtime_test.rb +1 -1
  31. data/test/elastic_record/relation/admin_test.rb +2 -2
  32. data/test/elastic_record/relation/batches_test.rb +1 -1
  33. data/test/elastic_record/relation/delegation_test.rb +1 -1
  34. data/test/elastic_record/relation/finder_methods_test.rb +15 -2
  35. data/test/elastic_record/relation/merging_test.rb +1 -1
  36. data/test/elastic_record/relation/none_test.rb +3 -2
  37. data/test/elastic_record/relation/search_methods_test.rb +13 -14
  38. data/test/elastic_record/relation_test.rb +10 -1
  39. data/test/elastic_record/searching_test.rb +1 -1
  40. data/test/helper.rb +10 -12
  41. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4c8b3152cde2dccd64daeba0c1b674e1f4baa59
4
- data.tar.gz: ecc417f58c2838fb097ddee6fcbc2e017eedf13e
3
+ metadata.gz: 6e7ce3ca4166690dfd09ab6072e1cb1d84e5efda
4
+ data.tar.gz: 61c4da83fe674d7c95a63e475ff3ba39daedde11
5
5
  SHA512:
6
- metadata.gz: a43f86409d56f0bfcb898262baab0a4a85c7c3db621a6645fae2c8ae96dfe3f27a5ccacebbb256b7e1c33dcdaf0d8c1c96d2a85c677889bd3a9d0da0250800dd
7
- data.tar.gz: f7750a39e91ba9b3133b915e8ebfd8f5f272df0fa802d5be5b164dded785085eaada91e3aef4cb6c9d015e3058bd1db505368c17fa149cbbe4d7c5db3d2ad9f1
6
+ metadata.gz: 9e463040d4d6ca0393b8ebdd54bf6df62366e6d65664ad8a46c1fc2eae4d51e805f21e643ba2f2b245cfe2637d95ad48fda1b12a5a8a306139399db660e3ed0d
7
+ data.tar.gz: 1078110a75c4837be1e422fecb8a05efcaf8a953198435baeb0ebf8988c75d32b9eb5370dd930f06a8c19768ae16d2d6f5bbb003a403cc23a45df2e1b0213a2f
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  services:
2
2
  - elasticsearch
3
3
  rvm:
4
- - 2.0.0
4
+ - 2.1.2
5
5
  env:
6
6
  - "GEM=ar:mysql2"
7
7
  - "GEM=ar:postgresql"
data/README.rdoc CHANGED
@@ -7,7 +7,7 @@ ElasticRecord is an elasticsearch ORM.
7
7
  == Setup
8
8
 
9
9
  The usual Gemfile addition:
10
-
10
+
11
11
  gem 'elastic_record'
12
12
 
13
13
 
@@ -73,17 +73,17 @@ To change the 'size' and 'from' values of a query, use offset and limit:
73
73
 
74
74
  search.limit(40).offset(80) # Creates a query with {size: 40, from: 80}
75
75
 
76
- === Facets
76
+ === Aggregations
77
77
 
78
- Since term facets are the most common, they are the easiest to add to a query:
78
+ Aggregations are added with the aggregate method:
79
79
 
80
- search.facet('colors')
80
+ search.aggregate('popular_colors' => {'terms' => {'field' => 'color'}})
81
81
 
82
- It is important to note that adding facets to a query is different than retrieving the results of the query:
82
+ It is important to note that adding aggregations to a query is different than retrieving the results of the query:
83
83
 
84
- search = search.facet('colors').facet('size')
85
- search.facets
86
- #=> {"colors" => ..., "size" => ...}
84
+ search = search.aggregate('popular_colors' => {'terms' => {'field' => 'color'}})
85
+ search.aggregations
86
+ #=> {"popular_colors" => {"buckets" => ...}}
87
87
 
88
88
  === Getting Results
89
89
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '1.4.2'
5
+ s.version = '2.0.0'
6
6
  s.summary = 'Use Elastic Search with your objects'
7
7
  s.description = 'Find your records with elastic search'
8
8
 
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.files = `git ls-files`.split("\n")
19
19
  s.test_files = `git ls-files -- {test}/*`.split("\n")
20
20
 
21
- s.add_dependency 'arelastic', '>= 0.4.0'
21
+ s.add_dependency 'arelastic', '>= 0.6.0'
22
22
  s.add_dependency 'activemodel'
23
23
  end
@@ -23,7 +23,7 @@ module ElasticRecord
23
23
  if writes_made
24
24
  begin
25
25
  index.disable_deferring!
26
- index.delete_by_query match_all: {}
26
+ index.delete_by_query query: {match_all: {}}
27
27
  ensure
28
28
  index.enable_deferring!
29
29
  end
@@ -84,4 +84,4 @@ module ElasticRecord
84
84
  end
85
85
  end
86
86
  end
87
- end
87
+ end
@@ -37,7 +37,7 @@ module ElasticRecord
37
37
  end
38
38
 
39
39
  def record_exists?(id)
40
- get(id)['exists']
40
+ get(id)['found']
41
41
  end
42
42
 
43
43
  def search(elastic_query, options = {})
@@ -99,4 +99,4 @@ module ElasticRecord
99
99
  end
100
100
  end
101
101
  end
102
- end
102
+ end
@@ -71,16 +71,22 @@ module ElasticRecord
71
71
  end
72
72
 
73
73
  def aliased_names
74
- json = connection.json_get '/_cluster/state'
75
- json["metadata"]["indices"].select { |name, status| status["aliases"].include?(alias_name) }.map { |name, status| name }
74
+ if (index_names = all_names).any?
75
+ json = connection.json_get "/#{index_names.join(',')}/_alias"
76
+ json.keys.select do |index_name|
77
+ json[index_name]['aliases'].keys.include?(alias_name)
78
+ end
79
+ else
80
+ []
81
+ end
76
82
  end
77
83
 
78
84
  def all_names
79
- json = connection.json_get '/_status'
80
-
81
- regex = %r{^#{alias_name}_?}
82
- json['indices'].keys.grep(regex)
85
+ connection.json_get("/#{alias_name}/_mapping/#{type}/").keys
86
+ rescue
87
+ # TODO: In ES 1.4, this returns empty rather than a 404
88
+ []
83
89
  end
84
90
  end
85
91
  end
86
- end
92
+ end
@@ -8,9 +8,12 @@ module ElasticRecord
8
8
  def update_mapping(index_name = alias_name)
9
9
  connection.json_put "/#{index_name}/#{type}/_mapping", type => mapping
10
10
  end
11
-
11
+
12
12
  def get_mapping(index_name = alias_name)
13
- connection.json_get "/#{index_name}/#{type}/_mapping"
13
+ json = connection.json_get "/#{index_name}/#{type}/_mapping"
14
+ unless json.empty?
15
+ json.values.first['mappings']
16
+ end
14
17
  end
15
18
 
16
19
  def delete_mapping(index_name = alias_name)
@@ -45,4 +48,4 @@ module ElasticRecord
45
48
  end
46
49
  end
47
50
  end
48
- end
51
+ end
@@ -2,42 +2,40 @@ module ElasticRecord
2
2
  class Index
3
3
  module Percolator
4
4
  def create_percolator(name, elastic_query)
5
- unless exists? percolator_index_name
6
- create percolator_index_name
7
- end
8
- connection.json_put "/_percolator/#{percolator_index_name}/#{name}", elastic_query
5
+ connection.json_put "/#{percolator_name}/.percolator/#{name}", elastic_query
9
6
  end
10
7
 
11
8
  def delete_percolator(name)
12
- connection.json_delete "/_percolator/#{percolator_index_name}/#{name}"
9
+ connection.json_delete "/#{percolator_name}/.percolator/#{name}"
13
10
  end
14
11
 
15
12
  def percolator_exists?(name)
16
- !get_percolator(name).nil?
13
+ connection.head("/#{percolator_name}/.percolator/#{name}") == '200'
17
14
  end
18
15
 
19
16
  def get_percolator(name)
20
- json = connection.json_get("/_percolator/#{percolator_index_name}/#{name}")
21
- json['_source'] if json['exists']
17
+ json = connection.json_get("/#{percolator_name}/.percolator/#{name}")
18
+ json['_source'] if json['found']
22
19
  end
23
20
 
24
21
  def percolate(document)
25
- connection.json_get("/#{percolator_index_name}/#{type}/_percolate", 'doc' => document)['matches']
22
+ hits = connection.json_get("/#{percolator_name}/#{type}/_percolate", 'doc' => document)['matches']
23
+ hits.map { |hits| hits['_id']}
26
24
  end
27
25
 
28
26
  def all_percolators
29
- if hits = connection.json_get("/_percolator/#{percolator_index_name}/_search?q=*&size=500")['hits']
27
+ if hits = connection.json_get("/#{percolator_name}/.percolator/_search?q=*&size=500")['hits']
30
28
  hits['hits'].map { |hit| hit['_id'] }
31
29
  end
32
30
  end
33
31
 
34
32
  def reset_percolators
35
- delete(percolator_index_name) if exists?(percolator_index_name)
36
- create(percolator_index_name)
33
+ delete(percolator_name) if exists?(percolator_name)
34
+ create(percolator_name)
37
35
  end
38
36
 
39
- def percolator_index_name
40
- @percolator_index_name ||= "percolate_#{alias_name}"
37
+ def percolator_name
38
+ "#{alias_name}_percolator"
41
39
  end
42
40
  end
43
41
  end
@@ -6,19 +6,19 @@ module ElasticRecord
6
6
  end
7
7
 
8
8
  def delete_warmer(name)
9
- connection.json_delete "/#{alias_name}/#{type}/_warmer/#{name}"
9
+ connection.json_delete "/#{alias_name}/_warmer/#{name}"
10
10
  end
11
11
 
12
12
  def get_warmer(name)
13
- connection.json_get("/#{alias_name}/#{type}/_warmer/#{name}").values.first['warmers'][name]
13
+ json = connection.json_get("/#{alias_name}/#{type}/_warmer/#{name}")
14
+ if json.any?
15
+ json.values.first['warmers'][name]
16
+ end
14
17
  end
15
18
 
16
19
  def warmer_exists?(name)
17
- get_warmer(name)
18
- true
19
- rescue ElasticRecord::ConnectionError
20
- false
20
+ !get_warmer(name).nil?
21
21
  end
22
22
  end
23
23
  end
24
- end
24
+ end
@@ -85,4 +85,4 @@ module ElasticRecord
85
85
  "#{alias_name}_#{(Time.now.to_f * 100).to_i}"
86
86
  end
87
87
  end
88
- end
88
+ end
@@ -13,6 +13,10 @@ module ElasticRecord
13
13
  {}
14
14
  end
15
15
 
16
+ def aggregations
17
+ {}
18
+ end
19
+
16
20
  def as_elastic
17
21
  Arelastic::Filters::Not.new(Arelastic::Queries::MatchAll.new).as_elastic
18
22
  end
@@ -92,6 +92,15 @@ module ElasticRecord
92
92
  clone.facet! facet_or_name, options
93
93
  end
94
94
 
95
+ def aggregate!(aggregation)
96
+ self.aggregation_values += [aggregation]
97
+ self
98
+ end
99
+
100
+ def aggregate(aggregation)
101
+ clone.aggregate! aggregation
102
+ end
103
+
95
104
  def order!(*args) # :nodoc:
96
105
  self.order_values += args.flatten
97
106
  self
@@ -141,6 +150,7 @@ module ElasticRecord
141
150
  build_limit(limit_value),
142
151
  build_offset(offset_value),
143
152
  build_facets(facet_values),
153
+ build_aggregations(aggregation_values),
144
154
  build_orders(order_values)
145
155
  ].compact
146
156
 
@@ -225,6 +235,10 @@ module ElasticRecord
225
235
  end
226
236
  end
227
237
 
238
+ def build_aggregations(aggregations)
239
+ Arelastic::Searches::Aggregations.new(aggregations) unless aggregations.empty?
240
+ end
241
+
228
242
  def build_facets(facets)
229
243
  Arelastic::Searches::Facets.new(facets) unless facets.empty?
230
244
  end
@@ -1,6 +1,6 @@
1
1
  module ElasticRecord
2
2
  class Relation
3
- MULTI_VALUE_METHODS = [:extending, :facet, :filter, :order, :select]
3
+ MULTI_VALUE_METHODS = [:extending, :facet, :filter, :order, :select, :aggregation]
4
4
  SINGLE_VALUE_METHODS = [:query, :limit, :offset, :reverse_order]
5
5
  end
6
6
  end
@@ -26,6 +26,10 @@ module ElasticRecord
26
26
  search_results['facets']
27
27
  end
28
28
 
29
+ def aggregations
30
+ search_results['aggregations']
31
+ end
32
+
29
33
  def explain(id)
30
34
  klass.elastic_index.explain(id, as_elastic)
31
35
  end
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::CallbacksTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::CallbacksTest < MiniTest::Test
4
4
  def test_added_to_index
5
5
  widget = Widget.new id: '10', color: 'green'
6
6
  refute Widget.elastic_index.record_exists?(widget.id)
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::ConfigTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::ConfigTest < MiniTest::Test
4
4
  def test_defaults
5
5
  assert_equal '5m', ElasticRecord::Config.scroll_keep_alive
6
6
  end
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::ConnectionTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::ConnectionTest < MiniTest::Test
4
4
  def test_servers
5
5
  assert_equal ['foo'], ElasticRecord::Connection.new('foo').servers
6
6
  assert_equal ['foo', 'bar'], ElasticRecord::Connection.new(['foo', 'bar']).servers
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Index::ConfiguratorTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Index::ConfiguratorTest < MiniTest::Test
4
4
  def test_property
5
5
  configurator.property :am_i_cool, type: "boolean"
6
6
 
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Index::DocumentsTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Index::DocumentsTest < MiniTest::Test
4
4
  class InheritedWidget < Widget
5
5
  def self.base_class
6
6
  Widget
@@ -41,7 +41,7 @@ class ElasticRecord::Index::DocumentsTest < MiniTest::Unit::TestCase
41
41
  index.index_document('bob', name: 'bob')
42
42
  index.index_document('joe', name: 'joe')
43
43
 
44
- index.delete_by_query(query_string: {query: 'name.analyzed:bob'})
44
+ index.delete_by_query('query' => {query_string: {query: 'name.analyzed:bob'}})
45
45
 
46
46
  refute index.record_exists?('bob')
47
47
  assert index.record_exists?('joe')
@@ -64,9 +64,9 @@ class ElasticRecord::Index::DocumentsTest < MiniTest::Unit::TestCase
64
64
  index.delete_document '3'
65
65
 
66
66
  expected = [
67
- {index: {_index: "widgets", _type: "widget", _id: "5"}},
67
+ {index: {_index: index.alias_name, _type: "widget", _id: "5"}},
68
68
  {color: "green"},
69
- {delete: {_index: "widgets", _type: "widget", _id: "3"}}
69
+ {delete: {_index: index.alias_name, _type: "widget", _id: "3"}}
70
70
  ]
71
71
  assert_equal expected, index.current_bulk_batch
72
72
  end
@@ -89,7 +89,7 @@ class ElasticRecord::Index::DocumentsTest < MiniTest::Unit::TestCase
89
89
  InheritedWidget.elastic_index.index_document '5', color: 'green'
90
90
 
91
91
  expected = [
92
- {index: {_index: "widgets", _type: "widget", _id: "5"}},
92
+ {index: {_index: index.alias_name, _type: "widget", _id: "5"}},
93
93
  {color: "green"}
94
94
  ]
95
95
  assert_equal expected, index.current_bulk_batch
@@ -101,4 +101,4 @@ class ElasticRecord::Index::DocumentsTest < MiniTest::Unit::TestCase
101
101
  def index
102
102
  @index ||= Widget.elastic_index
103
103
  end
104
- end
104
+ end
@@ -1,13 +1,13 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Index::ManageTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Index::ManageTest < MiniTest::Test
4
4
  class Felon
5
5
  include TestModel
6
6
  end
7
7
 
8
8
  def setup
9
9
  super
10
- Felon.elastic_index.delete_all
10
+ Felon.elastic_index.delete('felons*')
11
11
  end
12
12
 
13
13
  def test_create
@@ -55,4 +55,4 @@ class ElasticRecord::Index::ManageTest < MiniTest::Unit::TestCase
55
55
  def index
56
56
  @index ||= Felon.elastic_index
57
57
  end
58
- end
58
+ end
@@ -1,15 +1,13 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Index::MappingTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Index::MappingTest < MiniTest::Test
4
4
  def test_delete_mapping
5
5
  index_name = index.create
6
- index.get_mapping(index_name)
6
+ refute_nil index.get_mapping(index_name)['widget']
7
7
 
8
8
  index.delete_mapping(index_name)
9
9
 
10
- assert_raises ElasticRecord::ConnectionError do
11
- index.get_mapping(index_name)
12
- end
10
+ assert_nil index.get_mapping(index_name)
13
11
  end
14
12
 
15
13
  def test_default_mapping
@@ -33,4 +31,4 @@ class ElasticRecord::Index::MappingTest < MiniTest::Unit::TestCase
33
31
  def index
34
32
  @index ||= ElasticRecord::Index.new(Widget)
35
33
  end
36
- end
34
+ end
@@ -1,13 +1,13 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Index::PercolatorTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Index::PercolatorTest < MiniTest::Test
4
4
  def test_create_percolator
5
5
  index.disable_deferring!
6
6
 
7
7
  index.delete_percolator('green') if index.percolator_exists?('green')
8
8
  index.delete_percolator('blue') if index.percolator_exists?('blue')
9
9
 
10
- index.create_percolator('green', 'color' => 'green')
10
+ index.create_percolator('green', 'query' => {'match' => {'color' => 'green'}})
11
11
 
12
12
  assert index.percolator_exists?('green')
13
13
  refute index.percolator_exists?('blue')
@@ -16,13 +16,24 @@ class ElasticRecord::Index::PercolatorTest < MiniTest::Unit::TestCase
16
16
  def test_delete_percolator
17
17
  index.disable_deferring!
18
18
 
19
- index.create_percolator('green', 'color' => 'green')
19
+ index.create_percolator('green', 'query' => {'match' => {'color' => 'green'}})
20
20
  assert index.percolator_exists?('green')
21
21
 
22
22
  index.delete_percolator('green')
23
23
  refute index.percolator_exists?('green')
24
24
  end
25
25
 
26
+ def test_reset_percolators
27
+ index.disable_deferring!
28
+
29
+ index.create_percolator('green', 'query' => {'match' => {'color' => 'green'}})
30
+ assert index.percolator_exists?('green')
31
+
32
+ index.reset_percolators
33
+
34
+ refute index.percolator_exists?('green')
35
+ end
36
+
26
37
  def test_percolate
27
38
  end
28
39
 
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Index::SettingsTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Index::SettingsTest < MiniTest::Test
4
4
  def test_default_settings
5
5
  expected = {}
6
6
  assert_equal expected, ElasticRecord::Index.new(Widget).settings
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Index::WarmerTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Index::WarmerTest < MiniTest::Test
4
4
  def test_create_warmer
5
5
  index.delete_warmer('green') if index.warmer_exists?('green')
6
6
  refute index.warmer_exists?('green')
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::IndexTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::IndexTest < MiniTest::Test
4
4
  def test_copy
5
5
  copied = index.dup
6
6
 
@@ -49,7 +49,7 @@ module ElasticRecord
49
49
  end
50
50
  end
51
51
 
52
- class ElasticRecord::Mysql2Test < MiniTest::Unit::TestCase
52
+ class ElasticRecord::Mysql2Test < MiniTest::Test
53
53
  include ElasticRecord::ActiveRecordIntegration
54
54
 
55
55
  def setup
@@ -64,7 +64,7 @@ class ElasticRecord::Mysql2Test < MiniTest::Unit::TestCase
64
64
  end
65
65
  end
66
66
 
67
- class ElasticRecord::PostgresqlTest < MiniTest::Unit::TestCase
67
+ class ElasticRecord::PostgresqlTest < MiniTest::Test
68
68
  include ElasticRecord::ActiveRecordIntegration
69
69
 
70
70
  def setup
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::LuceneTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::LuceneTest < MiniTest::Test
4
4
  def test_escape
5
5
  assert_equal "\\\\", ElasticRecord::Lucene.escape("\\")
6
6
  assert_equal "Matt \\&& Joe", ElasticRecord::Lucene.escape("Matt && Joe")
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::ModelTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::ModelTest < MiniTest::Test
4
4
  class InheritedModel < Widget
5
5
  end
6
6
 
@@ -1,7 +1,7 @@
1
1
  require 'helper'
2
2
  require "elastic_record/railties/controller_runtime"
3
3
 
4
- class ElasticRecord::Railties::ControllerRuntimeTest < MiniTest::Unit::TestCase
4
+ class ElasticRecord::Railties::ControllerRuntimeTest < MiniTest::Test
5
5
  class TestRuntime
6
6
  def self.log_process_action(payload)
7
7
  ['sweet']
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Relation::AdminTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Relation::AdminTest < MiniTest::Test
4
4
  def test_create_percolator
5
5
  index.delete_percolator('green') if index.percolator_exists?('green')
6
6
 
@@ -25,4 +25,4 @@ class ElasticRecord::Relation::AdminTest < MiniTest::Unit::TestCase
25
25
  Widget.elastic_index
26
26
  end
27
27
 
28
- end
28
+ end
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Relation::BatchesTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Relation::BatchesTest < MiniTest::Test
4
4
  def setup
5
5
  super
6
6
  create_widgets
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Relation::DelegationTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Relation::DelegationTest < MiniTest::Test
4
4
  def test_delegate_to_array
5
5
  Widget.elastic_index.index_document('5', color: 'red')
6
6
 
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Test
4
4
  def setup
5
5
  super
6
6
  create_widgets
@@ -24,7 +24,7 @@ class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Unit::TestCase
24
24
 
25
25
  def test_find_passed_an_empty_args
26
26
  assert_raises ActiveRecord::RecordNotFound do
27
- Widget.elastic_relation.find()
27
+ Widget.elastic_relation.find
28
28
  end
29
29
  end
30
30
 
@@ -53,6 +53,19 @@ class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Unit::TestCase
53
53
  assert_equal 1, Widget.elastic_relation.filter('color' => 'red').all.size
54
54
  end
55
55
 
56
+ def test_find_by
57
+ Widget.elastic_index.bulk_add [
58
+ Widget.new(color: 'red', id: '05'),
59
+ Widget.new(color: 'blue', id: '10'),
60
+ ]
61
+
62
+ assert_equal '05', Widget.elastic_relation.find_by(color: 'red').id
63
+ assert_equal '05', Widget.elastic_relation.find_by!(color: 'red').id
64
+ assert_raises ActiveRecord::RecordNotFound do
65
+ Widget.elastic_relation.find_by!(color: 'green')
66
+ end
67
+ end
68
+
56
69
  private
57
70
 
58
71
  def create_widgets
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Relation::MergingTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Relation::MergingTest < MiniTest::Test
4
4
  def test_merge_single_values
5
5
  relation = Widget.elastic_relation.limit(5)
6
6
  other = Widget.elastic_relation.limit(10)
@@ -1,14 +1,15 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Relation::NoneTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Relation::NoneTest < MiniTest::Test
4
4
  def test_none
5
5
  none = Widget.elastic_relation.none
6
6
 
7
7
  assert_equal 0, none.size
8
8
  assert_equal 0, none.count
9
9
  assert_equal [], none.to_a
10
+ assert_equal({}, none.aggregations)
10
11
 
11
12
  expected_elastic = {"not" => {"match_all" => {}}}
12
13
  assert_equal expected_elastic, none.as_elastic
13
14
  end
14
- end
15
+ end
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::Relation::SearchMethodsTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::Relation::SearchMethodsTest < MiniTest::Test
4
4
  def test_query_with_no_queries
5
5
  expected = {"match_all" => {}}
6
6
 
@@ -57,19 +57,6 @@ class ElasticRecord::Relation::SearchMethodsTest < MiniTest::Unit::TestCase
57
57
  assert_equal expected, relation.as_elastic['query']
58
58
  end
59
59
 
60
- def test_find_by
61
- Widget.elastic_index.bulk_add [
62
- Widget.new(color: 'red', id: '05'),
63
- Widget.new(color: 'blue', id: '10'),
64
- ]
65
-
66
- assert_equal '05', relation.find_by(color: 'red').id
67
- assert_equal '05', relation.find_by!(color: 'red').id
68
- assert_raises ActiveRecord::RecordNotFound do
69
- relation.find_by!(color: 'green')
70
- end
71
- end
72
-
73
60
  def test_query_with_only_query
74
61
  relation.query!('foo')
75
62
 
@@ -146,6 +133,18 @@ class ElasticRecord::Relation::SearchMethodsTest < MiniTest::Unit::TestCase
146
133
  assert_equal expected, faceted.as_elastic['facets']
147
134
  end
148
135
 
136
+ def test_aggregation_with_bang
137
+ relation.aggregate!("tags" => {"terms" => {"field" => "tags"}})
138
+
139
+ expected = {
140
+ "tags" => {
141
+ "terms" => {"field" => "tags"}
142
+ }
143
+ }
144
+
145
+ assert_equal expected, relation.as_elastic['aggs']
146
+ end
147
+
149
148
  def test_limit
150
149
  relation.limit!(5)
151
150
 
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::RelationTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::RelationTest < MiniTest::Test
4
4
  def test_count
5
5
  create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
6
6
 
@@ -15,6 +15,15 @@ class ElasticRecord::RelationTest < MiniTest::Unit::TestCase
15
15
  assert_equal 2, facets['popular_colors']['total']
16
16
  end
17
17
 
18
+ def test_aggregations
19
+ create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
20
+
21
+ aggregations = Widget.elastic_relation.aggregate('popular_colors' => {'terms' => {'field' => 'color'}}).aggregations # Widget.arelastic.facet['popular_colors'].terms('color')).facets
22
+
23
+ assert_equal 2, aggregations['popular_colors']['buckets'].size
24
+ assert_equal %w(red blue).to_set, aggregations['popular_colors']['buckets'].map { |bucket| bucket['key'] }.to_set
25
+ end
26
+
18
27
  def test_explain
19
28
  create_widgets [Widget.new(id: 10, color: 'blue')]
20
29
 
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class ElasticRecord::SearchingTest < MiniTest::Unit::TestCase
3
+ class ElasticRecord::SearchingTest < MiniTest::Test
4
4
  def test_elastic_search
5
5
 
6
6
  end
data/test/helper.rb CHANGED
@@ -13,21 +13,19 @@ ElasticRecord::Config.model_names = %w(Warehouse Widget)
13
13
  FakeWeb.allow_net_connect = %r[^https?://127.0.0.1]
14
14
 
15
15
  module MiniTest
16
- class Unit
17
- class TestCase
18
- def setup
19
- FakeWeb.clean_registry
16
+ class Test
17
+ def setup
18
+ FakeWeb.clean_registry
20
19
 
21
- ElasticRecord::Config.models.each do |model|
22
- model.elastic_index.create_and_deploy if model.elastic_index.all_names.empty?
23
- model.elastic_index.enable_deferring!
24
- end
20
+ ElasticRecord::Config.models.each do |model|
21
+ model.elastic_index.create_and_deploy if model.elastic_index.all_names.empty?
22
+ model.elastic_index.enable_deferring!
25
23
  end
24
+ end
26
25
 
27
- def teardown
28
- ElasticRecord::Config.models.each do |model|
29
- model.elastic_index.reset_deferring!
30
- end
26
+ def teardown
27
+ ElasticRecord::Config.models.each do |model|
28
+ model.elastic_index.reset_deferring!
31
29
  end
32
30
  end
33
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Infogroup
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-01 00:00:00.000000000 Z
12
+ date: 2014-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: 0.4.0
20
+ version: 0.6.0
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: 0.4.0
27
+ version: 0.6.0
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activemodel
30
30
  requirement: !ruby/object:Gem::Requirement