elastic_record 4.1.3 → 4.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7965e969bb8efac9eaa7cbc7ca51904096738d48
4
- data.tar.gz: d8d402f88e931c733467579335fd953c2d817fd4
3
+ metadata.gz: 421498813d046dccefd30ad42c6f838aa9f9eb68
4
+ data.tar.gz: a86f63e2eac01170619fe51bf2531b856186e676
5
5
  SHA512:
6
- metadata.gz: cc4f71a68201edfa1ff67044bfa3dd2cd9ef3c68ecdd9eee7dbdc1411500fd1d7c0c0868454ec6c5fe01f5bdfb259393bf902dbb4976bad76b08ece9821f5d59
7
- data.tar.gz: '0075226959c8dac41a0f90ad92b1679440f3e17a580c3e218fe217dc8abb3f108c6c8b2c0c5cb291f262c38fbde6c0c052c7d5697854b1c004bb338f33087805'
6
+ metadata.gz: 84eec08e7e9e8dfea71136e8865e9a487f436674f04ec6de93c652e6193c184d3b072af6334157ca3f4176ef0532126c93b19e57f1e366248bb6171536785986
7
+ data.tar.gz: '05934bd3792f3b6a264aa7ec12cb93e9bcf9a50c518bc4f45780c8cb8fad46456cc45f87bfcc7d590eff9166145192e2c12a433753cf4c99b5e494ef9a2bc4b6'
data/README.md CHANGED
@@ -194,6 +194,28 @@ class Product
194
194
  end
195
195
  ```
196
196
 
197
+ ### Load Documents from Source
198
+
199
+ To fetch documents without an additional request to a backing ActiveRecord database you can load the documents from `_source`.
200
+
201
+ ```ruby
202
+ Product.elastic_index.loading_from_source do
203
+ Product.elastic_search.filter(name: "Pizza")
204
+ end
205
+
206
+ ```
207
+
208
+ Use `elastic_index.load_from_source = true` to configure an index without ActiveRecord.
209
+
210
+ ```ruby
211
+ class Product
212
+ include ActiveModel::Model
213
+ include ElasticRecord::Record
214
+
215
+ self.elastic_index.load_from_source = true
216
+ end
217
+ ```
218
+
197
219
  ### Index Management ###
198
220
 
199
221
  If you need to manage multiple indexes via the rake tasks, you will need to declare them explicitly:
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '4.1.3'
5
+ s.version = '4.1.4'
6
6
  s.summary = 'An Elasticsearch querying ORM'
7
7
  s.description = 'Find your records with Elasticsearch'
8
8
 
@@ -37,6 +37,7 @@ module ElasticRecord
37
37
  attr_accessor :disabled
38
38
  attr_accessor :model
39
39
  attr_accessor :partial_updates
40
+ attr_accessor :load_from_source
40
41
 
41
42
  def initialize(models)
42
43
  models = Array.wrap(models)
@@ -65,6 +66,13 @@ module ElasticRecord
65
66
  @disabled = false
66
67
  end
67
68
 
69
+ def loading_from_source(&block)
70
+ @load_from_source = true
71
+ yield
72
+ ensure
73
+ @load_from_source = false
74
+ end
75
+
68
76
  def real_connection
69
77
  model.elastic_connection
70
78
  end
@@ -35,7 +35,7 @@ module ElasticRecord
35
35
  end
36
36
 
37
37
  def to_a
38
- @records ||= load_hits(to_ids)
38
+ @records ||= load_hits
39
39
  end
40
40
 
41
41
  def to_ids
@@ -62,11 +62,12 @@ module ElasticRecord
62
62
  klass.current_elastic_search = previous
63
63
  end
64
64
 
65
- def search_hits
66
- search_results['hits']['hits']
67
- end
68
-
69
65
  private
66
+
67
+ def search_hits
68
+ search_results['hits']['hits']
69
+ end
70
+
70
71
  def reset
71
72
  @search_results = @records = nil
72
73
  end
@@ -75,13 +76,21 @@ module ElasticRecord
75
76
  @search_results ||= begin
76
77
  options = search_type_value ? {search_type: search_type_value} : {}
77
78
 
78
- klass.elastic_index.search(as_elastic.update('_source' => false), options)
79
+ unless klass.elastic_index.load_from_source
80
+ as_elastic.update('_source' => false)
81
+ end
82
+
83
+ klass.elastic_index.search(as_elastic, options)
79
84
  end
80
85
  end
81
86
 
82
- def load_hits(ids)
83
- scope = select_values.any? ? klass.select(select_values) : klass
84
- scope.find(ids)
87
+ def load_hits
88
+ if klass.elastic_index.load_from_source
89
+ search_hits.map { |hit| klass.new(hit['_source'].update('id' => hit['_id'])) }
90
+ else
91
+ scope = select_values.any? ? klass.select(select_values) : klass
92
+ scope.find(to_ids)
93
+ end
85
94
  end
86
95
  end
87
96
  end
@@ -51,9 +51,13 @@ namespace :index do
51
51
  index_name = model.elastic_index.create
52
52
  end
53
53
 
54
- puts " Reindexing into #{index_name}"
55
- model.find_in_batches(batch_size: 100) do |records|
56
- model.elastic_index.bulk_add(records, index_name: index_name)
54
+ if model.elastic_index.load_from_source
55
+ puts " Reindexing into #{index_name} [skipped]"
56
+ else
57
+ puts " Reindexing into #{index_name}"
58
+ model.find_in_batches(batch_size: 100) do |records|
59
+ model.elastic_index.bulk_add(records, index_name: index_name)
60
+ end
57
61
  end
58
62
 
59
63
  puts " Deploying index..."
data/test/dummy/.env.test CHANGED
@@ -1 +1 @@
1
- DATABASE_URL=postgres://localhost/elastic_record_test
1
+ DATABASE_URL=postgres://localhost/elastic_record_subtest
@@ -28,6 +28,13 @@ class ElasticRecord::IndexTest < MiniTest::Test
28
28
  refute index.disabled
29
29
  end
30
30
 
31
+ def test_loading_from_source
32
+ index.loading_from_source do
33
+ assert index.load_from_source
34
+ end
35
+ refute index.load_from_source
36
+ end
37
+
31
38
  private
32
39
 
33
40
  def index
@@ -42,6 +42,19 @@ class ElasticRecord::RelationTest < MiniTest::Test
42
42
  assert array.first.is_a?(Widget)
43
43
  end
44
44
 
45
+ def test_to_a_from_source
46
+ Widget.elastic_index.loading_from_source do
47
+ create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
48
+
49
+ array = Widget.elastic_relation.to_a
50
+
51
+ assert_equal 2, array.size
52
+ assert array.first.is_a?(Widget)
53
+ assert_equal 'red', array.first.color
54
+ assert_equal 'blue', array.last.color
55
+ end
56
+ end
57
+
45
58
  def test_delete_all
46
59
  project_red = Project.create! name: 'Red'
47
60
  project_blue = Project.create! name: 'Blue'
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: 4.1.3
4
+ version: 4.1.4
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: 2017-09-07 00:00:00.000000000 Z
12
+ date: 2017-09-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic