trample_search 0.16.0 → 0.17.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
  SHA1:
3
- metadata.gz: 20ffd69885c3eeda15f53ab9d4b6dd4bbc27f7be
4
- data.tar.gz: 71f0bc852acc52e6cd9e3f71edc74336e26b0c85
3
+ metadata.gz: d6aae72f53b824559e0bcf84a7c544737bc8b180
4
+ data.tar.gz: 50bca159319464c8cc994d0f87b7aec131b279c4
5
5
  SHA512:
6
- metadata.gz: 54309a47baec66899dc8e2481e37d3c4406394f8e8b86de457e084c468a352ce351b7988f0509daa6a46feab57f11d8975eea381efc5db53ed9642f8bfecaa63
7
- data.tar.gz: 9c25d165d404140577558b19e5a9b776c5c55091f12a51ddb95ade05ea96293f299c24b7225d3935d9eb7c4dcbc567aa233e14005d1bab5a961626925f9d6c70
6
+ metadata.gz: da90c4512a189ee0909d685eed2b4ea4e4f90408925efaa03dd78584055f07ae5c01f17b98052f11aa3207203e1320b885d419a99d59a7753f1ea23a8bf100b0
7
+ data.tar.gz: 5bc12aaff9e5665ec1b50c86272495d85622d016bee5c81cbb7fd3a03a0c8ce9ef3289fb4628d6ed44cdb519463855f92d2d35bf3e4123a887757bf21fea7eba
@@ -10,19 +10,55 @@ module Trample
10
10
  end
11
11
 
12
12
  def query!(conditions, aggregations)
13
- query = build_query(conditions, aggregations, @metadata, @_models)
14
- results = @_models.first.search(keywords(conditions), query)
15
- parse_response_aggs!(results.aggs, aggregations) if results.response.has_key?('aggregations')
13
+ results = resume_or_execute(conditions, aggregations)
16
14
 
17
15
  {
18
- total: results.total_count,
19
- took: results.response['took'],
20
- results: results.results
16
+ total: results.total_count,
17
+ took: results.response['took'],
18
+ results: results.results,
19
+ scroll_id: results.response['_scroll_id']
21
20
  }
22
21
  end
23
22
 
23
+ # There are 3 types of searches
24
+ #
25
+ # * A) Vanilla
26
+ # * B) Vanilla, but tell ES we are scrolling
27
+ # * C) A scroll search, using the scroll_id from B
28
+ def resume_or_execute(conditions, aggregations)
29
+ payload = build_payload(conditions, aggregations, @metadata, @_models)
30
+ query = ::Searchkick::Query.new(@_models.first, keywords(conditions), payload)
31
+
32
+ response = if @metadata.scroll_id
33
+ resume_search(@metadata.scroll_id)
34
+ else
35
+ execute_search(query)
36
+ end
37
+ results = query.handle_response(response)
38
+ parse_response_aggs!(results.aggs, aggregations) if results.response.has_key?('aggregations')
39
+ results
40
+ end
41
+
42
+ def execute_search(query)
43
+ search_params = raw_search_params(query, scroll: @metadata.scroll)
44
+ ::Searchkick.client.search(search_params)
45
+ end
46
+
24
47
  private
25
48
 
49
+ def resume_search(scroll_id)
50
+ search_params = { scroll_id: scroll_id, scroll: '1m' }
51
+ ::Searchkick.client.scroll(search_params)
52
+ end
53
+
54
+ def raw_search_params(query, scroll: false)
55
+ query.params.tap do |search_params|
56
+ if scroll
57
+ search_params[:scroll] = '5m'
58
+ end
59
+ end
60
+ end
61
+
26
62
  def keywords(conditions)
27
63
  if conditions[:keywords] and conditions[:keywords].values.first != ''
28
64
  conditions[:keywords].values.first
@@ -31,7 +67,7 @@ module Trample
31
67
  end
32
68
  end
33
69
 
34
- def build_query(conditions, aggregations, metadata, models)
70
+ def build_payload(conditions, aggregations, metadata, models)
35
71
  clauses = build_condition_clauses(conditions, aggregations)
36
72
  query = searchkick_payload(conditions[:keywords], clauses, metadata, aggregations)
37
73
  query.merge!(index_name: models.map { |m| m.searchkick_index.name }) if models.length > 1
@@ -33,6 +33,8 @@ module Trample
33
33
  attribute :pagination, Pagination, default: ->(_,_) { Pagination.new }
34
34
  attribute :took, Integer
35
35
  attribute :sort, Array[Sort]
36
+ attribute :scroll, Boolean, default: false
37
+ attribute :scroll_id, String
36
38
 
37
39
  def_delegators :pagination, :total, :current_page, :per_page
38
40
  def_delegator :sort, :att, :sort_att
@@ -137,6 +137,7 @@ module Trample
137
137
  @records = nil
138
138
  hash = backend.query!(conditions, aggregations)
139
139
  self.metadata.took = hash[:took]
140
+ self.metadata.scroll_id = hash[:scroll_id]
140
141
  self.metadata.pagination.total = hash[:total]
141
142
  self.results = hash[:results]
142
143
  if !!metadata.records[:load]
@@ -166,21 +167,22 @@ module Trample
166
167
  records
167
168
  end
168
169
 
169
- #This implementation is not using scroll and scan of elastic
170
- #https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
171
- #In future versions we hope to replace this with elastic in-built implementation
170
+ # Uses elasticsearch scroll, as ES will blow up > 10_000 results,
171
+ # even if you are paginating.
172
172
  #
173
+ # 1. Execute a search, telling ES we are scrolling
174
+ # 2. metadata.scroll_id is set on response
175
+ # 3. Use the scroll_id to fetch the next resultset
176
+ # 4. Repeat until results are exhausted
173
177
  def find_in_batches(batch_size: 10_000)
174
- page_number = 1
175
-
176
- loop do
177
- paginate(size: batch_size, number: page_number)
178
+ metadata.scroll = true
179
+ metadata.pagination.per_page = batch_size
180
+ query!
181
+ while !self.results.empty?
182
+ yield self.results
178
183
  query!
179
- yield results
180
- offset = metadata.pagination.current_page * metadata.pagination.per_page
181
- break unless metadata.pagination.next?
182
- page_number += 1
183
184
  end
185
+ self.metadata = Metadata.new
184
186
  end
185
187
 
186
188
  private
@@ -1,3 +1,3 @@
1
1
  module Trample
2
- VERSION = "0.16.0"
2
+ VERSION = "0.17.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trample_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - richmolj
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-27 00:00:00.000000000 Z
11
+ date: 2016-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus