search_flip 2.0.0.beta4 → 2.0.0.beta5

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: c626b4c449c70b071fee204d164b226271d2f098ae48f4c54568e07b6ad8d9d8
4
- data.tar.gz: f35599e1cd57a27fe3c008fa20538be08fa9f7251a5c421d25fd98113d2a6cc7
3
+ metadata.gz: b0d1ed2d2431d99cbe9949cb24154bfda3c2188713eb7432a6a8f1c6d5e45fe8
4
+ data.tar.gz: fbb8f6f7989552d6944a3d238ab3ca22c5ff2ca2c2c3ca7dddf1a3d8c4825ecc
5
5
  SHA512:
6
- metadata.gz: 95df5e882633308f0dcbcac35d155901db7bfee39f0f37d1ee84c055fdde821a22b697fd0df5ea4ab82204896c3adb483769cae85b1489ec2eca28c4fc0e2de5
7
- data.tar.gz: e97fceeacf0186b99829fb70d9a8eb396455c2f2cf3dec58e2f357dd5b8bd542025e7ee33201757af02c45b90752da70e6d28e6cbff2605fc36d0fd7ac9638d2
6
+ metadata.gz: 2f72caff2b7806d19d88d31ea8900fa739d10975d121d83245e7111552cbfc95fda81dde504f8b235ed4fdc6a9a6a3056029c404a638045d86a6c9fc606a23fd
7
+ data.tar.gz: ea91ff174b46ad96bb29a94a9173ba9295ec503e731abc6e0956b1788dd73197aebc75c43eb302f59c6554822c5ebf2c7dee6006878462137ad1e57b1c512dad
data/README.md CHANGED
@@ -705,7 +705,7 @@ you can e.g. use docker-compose:
705
705
  ```
706
706
  $ cd search_flip
707
707
  $ sudo ES_IMAGE=elasticsearch:5.4 docker-compose up
708
- $ rake test
708
+ $ rspec
709
709
  ```
710
710
 
711
711
  That's it.
@@ -14,7 +14,7 @@ module SearchFlip
14
14
  class Bulk
15
15
  class Error < StandardError; end
16
16
 
17
- attr_accessor :url, :count, :options, :http_client, :ignore_errors
17
+ attr_reader :url, :options, :ignore_errors
18
18
 
19
19
  # Builds and yields a new Bulk object, ie initiates the buffer, yields,
20
20
  # sends batches of records each time the buffer is full, and sends a final
@@ -32,8 +32,9 @@ module SearchFlip
32
32
  # end
33
33
  #
34
34
  # @param url [String] The endpoint to send bulk requests to
35
- # @param count [Fixnum] The maximum number of documents per bulk request
36
35
  # @param options [Hash] Options for the bulk requests
36
+ # @option options batch_size [Fixnum] The maximum number of documents per bulk
37
+ # request
37
38
  # @option options ignore_errors [Array, Fixnum] Errors that should be
38
39
  # ignored. If you eg want to ignore errors resulting from conflicts,
39
40
  # you can specify to ignore 409 here.
@@ -43,12 +44,16 @@ module SearchFlip
43
44
  # @option options http_client [SearchFlip::HTTPClient] An optional http
44
45
  # client instance
45
46
 
46
- def initialize(url, count = 1_000, options = {})
47
- self.url = url
48
- self.count = count
49
- self.options = options
50
- self.http_client = options[:http_client] || SearchFlip::HTTPClient.new
51
- self.ignore_errors = Array(options[:ignore_errors]).to_set if options[:ignore_errors]
47
+ def initialize(url, options = {})
48
+ @url = url
49
+ @options = options
50
+ @http_client = options[:http_client] || SearchFlip::HTTPClient.new
51
+ @ignore_errors = Array(options[:ignore_errors]).to_set if options[:ignore_errors]
52
+
53
+ @bulk_limit = options[:bulk_limit] || SearchFlip::Config[:bulk_limit]
54
+ @bulk_max_mb = options[:bulk_max_mb] || SearchFlip::Config[:bulk_max_mb]
55
+
56
+ @bulk_max_bytes = @bulk_max_mb * 1024 * 1024
52
57
 
53
58
  init
54
59
 
@@ -117,7 +122,7 @@ module SearchFlip
117
122
 
118
123
  def upload
119
124
  response =
120
- http_client
125
+ @http_client
121
126
  .headers(accept: "application/json", content_type: "application/x-ndjson")
122
127
  .put(url, body: @payload, params: ignore_errors ? {} : { filter_path: "errors" })
123
128
 
@@ -141,17 +146,21 @@ module SearchFlip
141
146
  end
142
147
 
143
148
  def perform(action, id, json = nil, options = {})
144
- @payload << SearchFlip::JSON.generate(action => options.merge(_id: id))
145
- @payload << "\n"
149
+ new_payload = SearchFlip::JSON.generate(action => options.merge(_id: id))
150
+ new_payload << "\n"
146
151
 
147
152
  if json
148
- @payload << json
149
- @payload << "\n"
153
+ new_payload << json
154
+ new_payload << "\n"
150
155
  end
151
156
 
157
+ upload if @num > 0 && @payload.bytesize + new_payload.bytesize >= @bulk_max_bytes
158
+
159
+ @payload << new_payload
160
+
152
161
  @num += 1
153
162
 
154
- upload if @num >= count
163
+ upload if @num >= @bulk_limit || @payload.bytesize >= @bulk_max_bytes
155
164
  end
156
165
  end
157
166
  end
@@ -4,6 +4,7 @@ module SearchFlip
4
4
  index_prefix: nil,
5
5
  base_url: "http://127.0.0.1:9200",
6
6
  bulk_limit: 1_000,
7
+ bulk_max_mb: 100,
7
8
  auto_refresh: false
8
9
  }
9
10
  end
@@ -1,19 +1,22 @@
1
1
 
2
2
  module SearchFlip
3
3
  class Connection
4
- attr_reader :base_url, :http_client
4
+ attr_reader :base_url, :http_client, :bulk_limit, :bulk_max_mb
5
5
 
6
6
  # Creates a new connection.
7
7
  #
8
8
  # @example
9
9
  # SearchFlip::Connection.new(base_url: "http://elasticsearch.host:9200")
10
10
  #
11
- # @param base_url [String] The base url for the connection
12
- # @param http_client [SearchFlip::HTTPClient] An optional http client instance
11
+ # @param options [Hash] A hash containing the config options
12
+ # @option options base_url [String] The base url for the connection
13
+ # @option options http_client [SearchFlip::HTTPClient] An optional http client instance
14
+ # @option options bulk_max_mb [Fixnum] An optional MB limit for bulk requests
13
15
 
14
- def initialize(base_url: SearchFlip::Config[:base_url], http_client: SearchFlip::HTTPClient.new)
15
- @base_url = base_url
16
- @http_client = http_client
16
+ def initialize(options = {})
17
+ @base_url = options[:base_url] || SearchFlip::Config[:base_url]
18
+ @http_client = options[:http_client] || SearchFlip::HTTPClient.new
19
+ @bulk_limit = options[:bulk_limit] || SearchFlip::Config[:bulk_limit]
17
20
  end
18
21
 
19
22
  # Queries and returns the ElasticSearch version used.
@@ -538,9 +538,13 @@ module SearchFlip
538
538
  # raise.
539
539
 
540
540
  def bulk(options = {})
541
- opts = { http_client: connection.http_client }.merge(options)
541
+ default_options = {
542
+ http_client: connection.http_client,
543
+ bulk_limit: connection.bulk_limit,
544
+ bulk_max_mb: connection.bulk_max_mb
545
+ }
542
546
 
543
- SearchFlip::Bulk.new("#{type_url}/_bulk", SearchFlip::Config[:bulk_limit], opts) do |indexer|
547
+ SearchFlip::Bulk.new("#{type_url}/_bulk", default_options.merge(options)) do |indexer|
544
548
  yield indexer
545
549
  end
546
550
 
@@ -1,5 +1,5 @@
1
1
 
2
2
  module SearchFlip
3
- VERSION = "2.0.0.beta4"
3
+ VERSION = "2.0.0.beta5"
4
4
  end
5
5
 
@@ -74,5 +74,31 @@ RSpec.describe SearchFlip::Bulk do
74
74
 
75
75
  expect(&block).to raise_error(SearchFlip::ResponseError)
76
76
  end
77
+
78
+ it "handles overly long payloads" do
79
+ product = create(:product)
80
+
81
+ allow(product).to receive(:description).and_return("x" * 1024 * 1024 * 10)
82
+
83
+ block = lambda do
84
+ ProductIndex.bulk bulk_max_mb: 1_000 do |bulk|
85
+ 100.times do
86
+ bulk.index product.id, ProductIndex.serialize(product)
87
+ end
88
+ end
89
+ end
90
+
91
+ expect(&block).to raise_error(SearchFlip::ResponseError)
92
+
93
+ block = lambda do
94
+ ProductIndex.bulk bulk_max_mb: 100 do |bulk|
95
+ 100.times do
96
+ bulk.index product.id, ProductIndex.serialize(product)
97
+ end
98
+ end
99
+ end
100
+
101
+ expect(&block).not_to raise_error #(SearchFlip::ResponseError)
102
+ end
77
103
  end
78
104
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_flip
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta4
4
+ version: 2.0.0.beta5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Vetter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-15 00:00:00.000000000 Z
11
+ date: 2019-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord