elastic_record 5.3.1 → 5.4.0
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 +4 -4
- data/.travis.yml +3 -4
- data/elastic_record.gemspec +1 -1
- data/lib/elastic_record.rb +1 -0
- data/lib/elastic_record/index/documents.rb +10 -10
- data/lib/elastic_record/relation.rb +1 -1
- data/lib/elastic_record/relation/batches.rb +3 -3
- data/lib/elastic_record/relation/hits.rb +3 -24
- data/lib/elastic_record/search_hits.rb +39 -0
- data/test/elastic_record/relation/hits_test.rb +1 -24
- data/test/elastic_record/relation_test.rb +23 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab3d32f450ac1c3c57de3216607cd0e98933ee13e009081556e40170db56c4e7
|
4
|
+
data.tar.gz: 3fe65fcfe4e06a56606aeb26e5dc0850433a146da77f3ceb4fcbf5fdff225813
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7afe879bc728ec2feecf6c616fa7b405d2df8172b468a052c307eee5ae02f97062e7f24d04efaecc9717591b489e3c01e4023d8f7034a4afd5ed0cf99bc106c
|
7
|
+
data.tar.gz: 79e3764907546cab0d8355f96ae32ca8460f8037a26009af46d77b3b8b107e6022832207227979a236d48507a2c70d069001c8b206b9c80960d920019084b4a8
|
data/.travis.yml
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
rvm: 2.
|
1
|
+
rvm: 2.5.3
|
2
2
|
cache: bundler
|
3
|
-
|
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.
|
18
|
+
- ES_VERSION=6.4.3
|
20
19
|
|
21
20
|
services:
|
22
21
|
- postgresql
|
data/elastic_record.gemspec
CHANGED
data/lib/elastic_record.rb
CHANGED
@@ -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 :
|
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
|
9
|
-
@search
|
10
|
-
@scroll_id
|
11
|
-
@keep_alive
|
12
|
-
@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.
|
22
|
+
request_more_hits.to_ids
|
23
23
|
end
|
24
24
|
|
25
25
|
def request_more_hits
|
26
|
-
request_next_scroll
|
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)
|
@@ -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
|
12
|
+
yield SearchHits.new(klass, hits).to_records
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def find_ids_in_batches(options = {}
|
16
|
+
def find_ids_in_batches(options = {})
|
17
17
|
build_scroll_enumerator(options).each_slice do |hits|
|
18
|
-
yield
|
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
|
-
|
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
|
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.
|
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-
|
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
|