elastic_record 5.3.1 → 5.4.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: e8877637e666a191a2b01fb967d998d36fe0a12023fdb8030a4c719bb77ae246
4
- data.tar.gz: 8f0136038c8d2b540c0e53e7c650a16ec7e5668274ae07631c2bc63ed3e4469b
3
+ metadata.gz: ab3d32f450ac1c3c57de3216607cd0e98933ee13e009081556e40170db56c4e7
4
+ data.tar.gz: 3fe65fcfe4e06a56606aeb26e5dc0850433a146da77f3ceb4fcbf5fdff225813
5
5
  SHA512:
6
- metadata.gz: 8e559697ea16610802ff1e7422cc5365f60098fbbda49565fc7227c1973c90bcacd0f893ee5a221316dde4e14a89338956134698dd5705a01581898f30a0bbb9
7
- data.tar.gz: ab182a767408f8843d854d79ac2f7f4f19d17ddbde11bbe52393d9eac12360fee38292a09c03a97ef726b76ddaea12ba4fb829a059c2ee505e6541f0cd4dd17c
6
+ metadata.gz: a7afe879bc728ec2feecf6c616fa7b405d2df8172b468a052c307eee5ae02f97062e7f24d04efaecc9717591b489e3c01e4023d8f7034a4afd5ed0cf99bc106c
7
+ data.tar.gz: 79e3764907546cab0d8355f96ae32ca8460f8037a26009af46d77b3b8b107e6022832207227979a236d48507a2c70d069001c8b206b9c80960d920019084b4a8
data/.travis.yml CHANGED
@@ -1,7 +1,6 @@
1
- rvm: 2.4.1
1
+ rvm: 2.5.3
2
2
  cache: bundler
3
- sudo: false
4
- dist: trusty
3
+ dist: xenial
5
4
 
6
5
  before_install:
7
6
  - wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
@@ -16,7 +15,7 @@ before_script:
16
15
 
17
16
  env:
18
17
  global:
19
- - ES_VERSION=6.2.3
18
+ - ES_VERSION=6.4.3
20
19
 
21
20
  services:
22
21
  - postgresql
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '5.3.1'
5
+ s.version = '5.4.0'
6
6
  s.summary = 'An Elasticsearch querying ORM'
7
7
  s.description = 'Find your records with Elasticsearch'
8
8
 
@@ -15,6 +15,7 @@ module ElasticRecord
15
15
  autoload :PercolatorModel
16
16
  autoload :Relation
17
17
  autoload :Searching
18
+ autoload :SearchHits
18
19
 
19
20
  module AggregationResponse
20
21
  extend ActiveSupport::Autoload
@@ -3,27 +3,27 @@ require 'active_support/core_ext/object/to_query'
3
3
  module ElasticRecord
4
4
  class Index
5
5
  class ScrollEnumerator
6
- attr_reader :keep_alive, :batch_size, :scroll_id
6
+ attr_reader :scroll_id, :keep_alive, :batch_size
7
7
  def initialize(elastic_index, search: nil, scroll_id: nil, keep_alive:, batch_size:)
8
- @elastic_index = elastic_index
9
- @search = search
10
- @scroll_id = scroll_id
11
- @keep_alive = keep_alive
12
- @batch_size = batch_size
8
+ @elastic_index = elastic_index
9
+ @search = search
10
+ @scroll_id = scroll_id
11
+ @keep_alive = keep_alive
12
+ @batch_size = batch_size
13
13
  end
14
14
 
15
15
  def each_slice(&block)
16
- while (hits = request_more_hits).any?
16
+ while (hits = request_more_hits.hits).any?
17
17
  hits.each_slice(batch_size, &block)
18
18
  end
19
19
  end
20
20
 
21
21
  def request_more_ids
22
- request_more_hits.map { |hit| hit['_id'] }
22
+ request_more_hits.to_ids
23
23
  end
24
24
 
25
25
  def request_more_hits
26
- request_next_scroll['hits']['hits']
26
+ SearchHits.from_response(@elastic_index.model, request_next_scroll)
27
27
  end
28
28
 
29
29
  def request_next_scroll
@@ -43,7 +43,7 @@ module ElasticRecord
43
43
 
44
44
  def initial_search_response
45
45
  @initial_search_response ||= begin
46
- search_options = {size: batch_size, scroll: keep_alive}
46
+ search_options = { size: batch_size, scroll: keep_alive }
47
47
  elastic_query = @search.reverse_merge('sort' => '_doc')
48
48
 
49
49
  @elastic_index.search(elastic_query, search_options)
@@ -39,7 +39,7 @@ module ElasticRecord
39
39
  end
40
40
 
41
41
  def to_a
42
- @records ||= load_hits(search_hits)
42
+ @records ||= search_hits.to_records
43
43
  end
44
44
 
45
45
  def delete_all
@@ -9,13 +9,13 @@ module ElasticRecord
9
9
 
10
10
  def find_in_batches(options = {})
11
11
  build_scroll_enumerator(options).each_slice do |hits|
12
- yield load_hits(hits)
12
+ yield SearchHits.new(klass, hits).to_records
13
13
  end
14
14
  end
15
15
 
16
- def find_ids_in_batches(options = {}, &block)
16
+ def find_ids_in_batches(options = {})
17
17
  build_scroll_enumerator(options).each_slice do |hits|
18
- yield map_hits_to_ids(hits)
18
+ yield SearchHits.new(klass, hits).to_ids
19
19
  end
20
20
  end
21
21
 
@@ -2,37 +2,16 @@ module ElasticRecord
2
2
  class Relation
3
3
  module Hits
4
4
  def to_ids
5
- map_hits_to_ids search_hits
6
- end
7
-
8
- def load_hits(search_hits)
9
- if klass.elastic_index.load_from_source
10
- search_hits.map { |hit| load_from_hit(hit) }
11
- else
12
- klass.find map_hits_to_ids(search_hits)
13
- end
14
- end
15
-
16
- def map_hits_to_ids(hits)
17
- hits.map { |hit| hit['_id'] }
5
+ search_hits.to_ids
18
6
  end
19
7
 
20
8
  def search_hits
21
- search_results['hits']['hits']
22
- end
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
9
+ SearchHits.from_response(klass, search_results)
31
10
  end
32
11
 
33
12
  def search_results
34
13
  @search_results ||= begin
35
- options = {typed_keys: true}
14
+ options = { typed_keys: true }
36
15
  options[:search_type] = search_type_value if search_type_value
37
16
 
38
17
  klass.elastic_index.search(as_elastic, options)
@@ -0,0 +1,39 @@
1
+ module ElasticRecord
2
+ class SearchHits
3
+ attr_accessor :hits, :model
4
+
5
+ class << self
6
+ def from_response(model, response)
7
+ new(model, response['hits']['hits'])
8
+ end
9
+ end
10
+
11
+ def initialize(model, hits)
12
+ @model = model
13
+ @hits = hits
14
+ end
15
+
16
+ def to_ids
17
+ hits.map { |hit| hit['_id'] }
18
+ end
19
+
20
+ def to_records
21
+ if model.elastic_index.load_from_source
22
+ hits.map { |hit| load_from_hit(hit) }
23
+ else
24
+ model.find to_ids
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def load_from_hit(hit)
31
+ model.new.tap do |record|
32
+ record.id = hit['_id']
33
+ hit['_source'].each do |k, v|
34
+ record.send("#{k}=", v) if record.respond_to?("#{k}=")
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -8,34 +8,11 @@ class ElasticRecord::Relation::HitsTest < MiniTest::Test
8
8
  assert_equal [red_widget.id.to_s, blue_widget.id.to_s].to_set, Widget.elastic_relation.to_ids.to_set
9
9
  end
10
10
 
11
- def test_to_a
12
- red_widget = Widget.create(color: 'red')
13
- blue_widget = Widget.create(color: 'red')
14
-
15
- array = Widget.elastic_relation.to_a
16
-
17
- assert_equal 2, array.size
18
- assert array.first.is_a?(Widget)
19
- end
20
-
21
- def test_to_a_from_source
22
- warehouses = [Project.new(name: 'Latte'), Project.new(name: 'Americano')]
23
- result = Project.elastic_index.bulk_add(warehouses)
24
-
25
- array = Project.elastic_relation.to_a
26
-
27
- assert_equal 2, array.size
28
- assert array.first.is_a?(Project)
29
- names = array.map(&:name)
30
- assert_includes names, 'Latte'
31
- assert_includes names, 'Americano'
32
- end
33
-
34
11
  def test_search_hits
35
12
  coffees = [Project.new(name: 'Latte'), Project.new(name: 'Americano')]
36
13
  Project.elastic_index.bulk_add(coffees)
37
14
 
38
- array = Project.elastic_relation.search_hits
15
+ array = Project.elastic_relation.search_hits.hits
39
16
  assert_equal %w(Latte Americano).to_set, array.map { |hit| hit["_source"]["name"] }.to_set
40
17
  end
41
18
 
@@ -28,6 +28,29 @@ class ElasticRecord::RelationTest < MiniTest::Test
28
28
  # explain = Widget.elastic_relation.filter(color: 'blue').explain('10')
29
29
  end
30
30
 
31
+ def test_to_a
32
+ Widget.create(color: 'red')
33
+ Widget.create(color: 'red')
34
+
35
+ array = Widget.elastic_relation.to_a
36
+
37
+ assert_equal 2, array.size
38
+ assert array.first.is_a?(Widget)
39
+ end
40
+
41
+ def test_to_a_from_source
42
+ warehouses = [Project.new(name: 'Latte'), Project.new(name: 'Americano')]
43
+ Project.elastic_index.bulk_add(warehouses)
44
+
45
+ array = Project.elastic_relation.to_a
46
+
47
+ assert_equal 2, array.size
48
+ assert array.first.is_a?(Project)
49
+ names = array.map(&:name)
50
+ assert_includes names, 'Latte'
51
+ assert_includes names, 'Americano'
52
+ end
53
+
31
54
  def test_delete_all
32
55
  project_red = Warehouse.create! name: 'Red'
33
56
  project_blue = Warehouse.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: 5.3.1
4
+ version: 5.4.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-12-04 00:00:00.000000000 Z
12
+ date: 2018-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic
@@ -91,6 +91,7 @@ files:
91
91
  - lib/elastic_record/relation/none.rb
92
92
  - lib/elastic_record/relation/search_methods.rb
93
93
  - lib/elastic_record/relation/value_methods.rb
94
+ - lib/elastic_record/search_hits.rb
94
95
  - lib/elastic_record/searching.rb
95
96
  - lib/elastic_record/tasks/index.rake
96
97
  - test/dummy/.env.example