elastic_record 5.2.0 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fbc183b9807acb14f1ba0b3d2f860c7be2eddbcf7360d542cbf9f1a9ea9df166
4
- data.tar.gz: cbcb08f9fff903a87f0fa35b4f5ceb1542870ec1b176605e559eb99111f53b72
3
+ metadata.gz: 3667cdd2ffc2ca7e744f74c570aca1de6217ab14f4ca196cacce22828ac09f55
4
+ data.tar.gz: 4099515af51acadaa10531d11c82078db1e239be9bb52f27d6211babd1b3774f
5
5
  SHA512:
6
- metadata.gz: da4b87dc02cb1bd4f0db4fdd02923584b96137ad1c8b115f94c906244f66f77aa8f021721724333a14f33b15f950d1df57a863c566ee96265b00f5586509a784
7
- data.tar.gz: ed8050784a85135ac6e3a9c650608e20d168221349bb84bc4b35588a474ac475b45f2e0605d358577f0b9147644c4254b303885db3153b8a169ef087b8ae4593
6
+ metadata.gz: e41404a6079677a0e0235c59217c0eebc42829bd07f0b57bff25b9da47cdd1585c29dfa83b614229f4be33da8e2335d58522a4e27a2c00cc3baad5c58bdd3838
7
+ data.tar.gz: 5ffbe7ee7525c9319d3b7db41e7ca4e0279f188aefe547b75bdccb1af1798c43564e83611366dcb5900b28c0bc0baf7d768a12da85a004e6060982e8dc086909
data/README.md CHANGED
@@ -224,14 +224,14 @@ end
224
224
 
225
225
  ```
226
226
 
227
- Use `elastic_index.load_from_source = true` to configure an index without ActiveRecord.
227
+ Call `load_from_source!` to configure an index without ActiveRecord. Finder methods will be
228
+ delegated to the ElasticRecord module.
228
229
 
229
230
  ```ruby
230
231
  class Product
231
232
  include ActiveModel::Model
232
233
  include ElasticRecord::Record
233
-
234
- self.elastic_index.load_from_source = true
234
+ elastic_index.load_from_source!
235
235
  end
236
236
  ```
237
237
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '5.2.0'
5
+ s.version = '5.3.0'
6
6
  s.summary = 'An Elasticsearch querying ORM'
7
7
  s.description = 'Find your records with Elasticsearch'
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', '>= 2.0.0'
21
+ s.add_dependency 'arelastic', '>= 2.5.0'
22
22
  s.add_dependency 'activemodel'
23
23
  end
@@ -12,7 +12,7 @@ module ElasticRecord
12
12
  "mappings" => {
13
13
  mapping_type => mapping
14
14
  },
15
- "settings" => setting_overrides.merge(settings)
15
+ "settings" => settings.merge(setting_overrides)
16
16
  }
17
17
  index_name
18
18
  end
@@ -42,7 +42,7 @@ module ElasticRecord
42
42
  def initialize(model)
43
43
  @model = model
44
44
  @disabled = false
45
- @load_from_source = false
45
+ self.load_from_source = false
46
46
  end
47
47
 
48
48
  def initialize_copy(other)
@@ -65,11 +65,16 @@ module ElasticRecord
65
65
  @disabled = false
66
66
  end
67
67
 
68
+ def load_from_source!
69
+ self.load_from_source = true
70
+ model.singleton_class.delegate :find, :find_by, :find_each, :find_in_batches, :first, to: :elastic_search
71
+ end
72
+
68
73
  def loading_from_source(&block)
69
- @load_from_source = true
74
+ self.load_from_source = true
70
75
  yield
71
76
  ensure
72
- @load_from_source = false
77
+ self.load_from_source = false
73
78
  end
74
79
 
75
80
  def real_connection
@@ -9,6 +9,8 @@ module ElasticRecord
9
9
 
10
10
  class_attribute :elastic_connection
11
11
  self.elastic_connection = ElasticRecord::Connection.new(ElasticRecord::Config.servers, ElasticRecord::Config.connection_options)
12
+
13
+ singleton_class.delegate :query, :filter, :aggregate, to: :elastic_search
12
14
  end
13
15
  end
14
16
 
@@ -2,15 +2,18 @@ module ElasticRecord
2
2
  class Relation
3
3
  module FinderMethods
4
4
  def find(*ids)
5
- return [] if ids.first.is_a?(Array) && ids.first.empty?
6
- ids = ids.flatten
7
- id_filter = filter(arelastic.filter.ids(ids))
8
- id_filter = id_filter.limit(ids.size) unless limit_value
9
-
10
- case ids.size
11
- when 0; raise ActiveRecord::RecordNotFound.new('empty argument')
12
- when 1; id_filter.first!
13
- else id_filter
5
+ flattened_ids = ids.flatten
6
+ id_filter = filter(arelastic.filter.ids(flattened_ids))
7
+ id_filter = id_filter.limit(flattened_ids.size) unless limit_value
8
+
9
+ if ids.first.is_a?(Array)
10
+ id_filter
11
+ else
12
+ case ids.size
13
+ when 0; raise ActiveRecord::RecordNotFound.new('empty argument')
14
+ when 1; id_filter.first!
15
+ else id_filter
16
+ end
14
17
  end
15
18
  end
16
19
 
@@ -7,7 +7,7 @@ module ElasticRecord
7
7
 
8
8
  def load_hits(search_hits)
9
9
  if klass.elastic_index.load_from_source
10
- search_hits.map { |hit| klass.new(hit['_source'].update('id' => hit['_id'])) }
10
+ search_hits.map { |hit| load_from_hit(hit) }
11
11
  else
12
12
  klass.find map_hits_to_ids(search_hits)
13
13
  end
@@ -21,6 +21,15 @@ module ElasticRecord
21
21
  search_results['hits']['hits']
22
22
  end
23
23
 
24
+ def load_from_hit(hit)
25
+ record = klass.new
26
+ record.id = hit['_id']
27
+ hit['_source'].each do |k, v|
28
+ record.send("#{k}=", v) if record.respond_to?("#{k}=")
29
+ end
30
+ record
31
+ end
32
+
24
33
  def search_results
25
34
  @search_results ||= begin
26
35
  options = {typed_keys: true}
@@ -196,13 +196,15 @@ module ElasticRecord
196
196
  query = build_query(query)
197
197
  filter = build_filter(filters)
198
198
 
199
- if filter
199
+ query_and_filter = if filter
200
200
  arelastic.query.bool(filter: filter, must: query)
201
201
  elsif query
202
- Arelastic::Searches::Query.new(query)
202
+ query
203
203
  else
204
204
  arelastic.query.match_all
205
205
  end
206
+
207
+ Arelastic::Searches::Query.new query_and_filter
206
208
  end
207
209
 
208
210
  def build_query(query)
@@ -11,6 +11,7 @@ module ElasticRecord
11
11
  elastic_relation
12
12
  end
13
13
  end
14
+ alias es elastic_search
14
15
 
15
16
  def elastic_scope(name, body, &block)
16
17
  extension = Module.new(&block) if block
@@ -7,12 +7,11 @@ class Project
7
7
 
8
8
  include ActiveModel::Model
9
9
  include ElasticRecord::Model
10
+ elastic_index.load_from_source!
10
11
 
11
12
  attr_accessor :id, :name
12
13
  alias_method :as_json, :as_search_document
13
14
 
14
- elastic_index.load_from_source = true
15
-
16
15
  def as_search_document
17
16
  { name: name }
18
17
  end
@@ -18,6 +18,21 @@ class ElasticRecord::Index::ManageTest < MiniTest::Test
18
18
  assert index.exists?('felons_foo')
19
19
  end
20
20
 
21
+ def test_create_with_overrides
22
+ old_settings = ElasticRecord::Config.default_index_settings
23
+ ElasticRecord::Config.default_index_settings = {
24
+ number_of_replicas: '2'
25
+ }
26
+ index.remove_instance_variable('@settings') if index.instance_variable_defined?('@settings')
27
+ index.create 'felons_default'
28
+ assert_equal '2', index_settings('felons_default')['index']['number_of_replicas']
29
+ index.create 'felons_override', setting_overrides: { number_of_replicas: 4 }
30
+ assert_equal '4', index_settings('felons_override')['index']['number_of_replicas']
31
+ ensure
32
+ ElasticRecord::Config.default_index_settings = old_settings
33
+ index.remove_instance_variable('@settings') if index.instance_variable_defined?('@settings')
34
+ end
35
+
21
36
  def test_exists
22
37
  index.create 'felons_foo'
23
38
 
@@ -48,4 +63,8 @@ class ElasticRecord::Index::ManageTest < MiniTest::Test
48
63
  def index
49
64
  @index ||= Felon.elastic_index
50
65
  end
66
+
67
+ def index_settings(index_name)
68
+ Felon.elastic_connection.json_get("/#{index_name}/_settings")[index_name]['settings']
69
+ end
51
70
  end
@@ -31,6 +31,15 @@ class ElasticRecord::IndexTest < MiniTest::Test
31
31
  refute index.load_from_source
32
32
  end
33
33
 
34
+ def test_delegations_when_loading_from_source
35
+ project = Project.new(name: 'Something')
36
+ Project.elastic_index.index_record(project)
37
+
38
+ found_project = Project.first
39
+ assert_equal 'Something', found_project.name
40
+ assert_equal 'Something', Project.find(found_project.id).name
41
+ end
42
+
34
43
  private
35
44
 
36
45
  def index
@@ -24,6 +24,7 @@ class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Test
24
24
 
25
25
  def test_find_passed_an_array
26
26
  assert_equal 2, Widget.elastic_relation.find([@red_widget.id, @blue_widget.id]).size
27
+ assert_equal 1, Widget.elastic_relation.find([@red_widget.id]).size
27
28
  assert_equal 2, Widget.elastic_relation.filter('color' => ['red', 'blue']).find([@red_widget.id, @blue_widget.id]).size
28
29
  assert_equal 0, Widget.elastic_relation.filter('color' => ['purple', 'gold']).find([@red_widget.id, @blue_widget.id]).size
29
30
  end
@@ -9,7 +9,9 @@ class ElasticRecord::SearchingTest < MiniTest::Test
9
9
  end
10
10
 
11
11
  def test_elastic_search
12
-
12
+ widget = Widget.create(color: 'red')
13
+ assert_equal widget, Widget.elastic_search.filter(color: 'red').first
14
+ assert_equal widget, Widget.es.filter(color: 'red').first
13
15
  end
14
16
 
15
17
  def test_elastic_scope
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: 5.2.0
4
+ version: 5.3.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: 2018-11-27 00:00:00.000000000 Z
12
+ date: 2018-12-04 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: 2.0.0
20
+ version: 2.5.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: 2.0.0
27
+ version: 2.5.0
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activemodel
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -196,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
196
  version: 1.8.11
197
197
  requirements: []
198
198
  rubyforge_project:
199
- rubygems_version: 2.7.8
199
+ rubygems_version: 2.7.6
200
200
  signing_key:
201
201
  specification_version: 4
202
202
  summary: An Elasticsearch querying ORM