swiftype 1.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec1df142277966dde94c63225c977056242a5d70
4
- data.tar.gz: e4decec856f184b9c8d1ad9a5774bde8e90cdb04
3
+ metadata.gz: 54e1e3d18b1ef0d4ac96736071a83904c332fa62
4
+ data.tar.gz: 7c387aedb4ccca99caef021296dc6f9f2adad14f
5
5
  SHA512:
6
- metadata.gz: 46250d369963f704a41c0911d228835e89696ffabdf3d0c60c4e81de1cb90e314202d844d58882f199aef34bd42cfed52cef1b4a904958891bbc0fec9bed2188
7
- data.tar.gz: d36c10b224eed2d6281ab2073bc6c849d98659952015d7da9c18cb0952dd937f74f26a8e00c06b305a25c3c87c75d869279fd6f630adad9b1a237b6c3172e5a3
6
+ metadata.gz: 380b9b2cd117f33f243386e1b5dac701ceb460d6ffd4f5f898bdedd5717d77585f1049f07b396613ce18dbc5add22487f6444d07f062f069cceedce02e717576
7
+ data.tar.gz: ccf234d1156a1338b9c11f12c2e68b06fb7b0b58b94118c8c60c9fb80d67f5e994c7f138fdbc914ced223351c73e3945aa8d07559917ec7d382dc938fd258318
data/README.md CHANGED
@@ -24,7 +24,7 @@ To install the gem, execute:
24
24
 
25
25
  gem install swiftype
26
26
 
27
- Or place `gem 'swiftype', '~> 1.0.1` in your `Gemfile` and run `bundle install`.
27
+ Or place `gem 'swiftype', '~> 1.2.0` in your `Gemfile` and run `bundle install`.
28
28
 
29
29
  ## Usage
30
30
 
@@ -36,6 +36,10 @@ Before issuing commands to the API, configure the client with your API key:
36
36
 
37
37
  You can find your API key in your [Account Settings](https://swiftype.com/settings/account).
38
38
 
39
+ If you're using Heroku, you can configure the client with the `SWIFTYPE_URL` configuration variable:
40
+
41
+ Swiftype.authenticated_url = ENV['SWIFTYPE_URL']
42
+
39
43
  ### Create a client
40
44
 
41
45
  client = Swiftype::Client.new
@@ -137,9 +141,11 @@ Retrieve a specific Document using its `id` or `external_id`:
137
141
 
138
142
  document = client.document('swiftype-api-example', 'videos', 'FHtvDA0W34I')
139
143
 
144
+ To create or update. single or multiple documents, we use the method `index_documents`.
145
+
140
146
  Create a new Document with mandatory `external_id` and user-defined fields:
141
147
 
142
- document = client.create_document('swiftype-api-example', 'videos', {
148
+ document = client.index_documents('swiftype-api-example', 'videos', {
143
149
  :external_id => 'FHtvDA0W34I',
144
150
  :fields => [
145
151
  {:name => 'title', :value => "Felix Baumgartner's supersonic freefall from 128k' - Mission Highlights", :type => 'string'},
@@ -149,7 +155,7 @@ Create a new Document with mandatory `external_id` and user-defined fields:
149
155
 
150
156
  Create multiple Documents at once and return status for each Document creation:
151
157
 
152
- response = client.create_documents('swiftype-api-example', 'videos', [{
158
+ response = client.index_documents('swiftype-api-example', 'videos', [{
153
159
  :external_id => 'FHtvDA0W34I',
154
160
  :fields => [
155
161
  {:name => 'title', :value => "Felix Baumgartner's supersonic freefall from 128k' - Mission Highlights", :type => 'string'},
@@ -162,35 +168,50 @@ Create multiple Documents at once and return status for each Document creation:
162
168
  {:name => 'url', :value => 'http://www.youtube.com/watch?v='dMH0bHeiRNg', :type => 'enum'},
163
169
  {:name => 'chanel_id', :value => UC5B9H4l2vtgo7cAoExcFh-w', :type => 'enum'}
164
170
  ]}])
165
- #=> [true, true]
166
171
 
167
172
  Update fields of an existing Document specified by `id` or `external_id`:
168
173
 
169
- client.update_document('swiftype-api-example', 'videos', 'FHtvDA0W34I', {:title =>'New Title'})
174
+ client.index_documents('swiftype-api-example', 'videos', 'FHtvDA0W34I', {:title =>'New Title'})
170
175
 
171
176
  **NOTE:** A field must already exist on a Document in order to update it.
172
177
 
173
178
  Update multiple Documents at once:
174
179
 
175
- response = client.update_documents('swiftype-api-example','videos', [
180
+ response = client.index_documents('swiftype-api-example','videos', [
176
181
  {:external_id => 'FHtvDA0W34I', :fields => {:view_count => 32874417}},
177
182
  {:external_id => 'dMH0bHeiRNg', :fields => {:view_count => 98323493}}
178
183
  ])
179
- #=> [true, true]
180
-
181
- Create or update a Document:
182
184
 
183
- document = client.create_or_update_document('swiftype-api-example', 'videos', {
184
- :external_id => 'FHtvDA0W34I',
185
- :fields => [
186
- {:name => 'title', :value => "Felix Baumgartner's supersonic freefall from 128k' - Mission Highlights", :type => 'string'},
187
- {:name => 'url', :value => 'http://www.youtube.com/watch?v=FHtvDA0W34I', :type => 'enum'},
188
- {:name => 'chanel_id', :value => 'UCblfuW_4rakIf2h6aqANefA', :type => 'enum'}
189
- ]})
190
-
191
- Create or update multiple `Documents` at once:
192
-
193
- response = client.create_or_update_documents('swiftype-api-example', 'videos', [{
185
+ All methods above will have a return in the following format:
186
+
187
+ [
188
+ {
189
+ "id": "5473d6142ed96065a9000001",
190
+ "external_id": "FHtvDA0W34I",
191
+ "status": "complete",
192
+ "errors": [],
193
+ "links": {
194
+ "receipt": "https://api.swiftype.com/api/v1/document_receipts/5473d6142ed96065a9000001.json",
195
+ "document": "https://api.swiftype.com/api/v1/engine/xyz/document_type/abc/document/5473d6142ed96065a9000001.json"
196
+ }
197
+ },
198
+ {
199
+ "id": "5473d6142ed96065a9000002",
200
+ "external_id": "dMH0bHeiRNg",
201
+ "status": "complete",
202
+ "errors": [],
203
+ "links": {
204
+ "receipt": "https://api.swiftype.com/api/v1/document_receipts/5473d6142ed96065a9000001.json",
205
+ "document": "https://api.swiftype.com/api/v1/engine/xyz/document_type/abc/document/5473d6142ed96065a9000002.json"
206
+ }
207
+ }
208
+ ]
209
+
210
+ **NOTE:** If you'd like to create or update documents asynchronously, simply pass the option `:async => true` as the last argument.
211
+
212
+ For instance, to Create multiple Documents at once:
213
+
214
+ response = client.index_documents('swiftype-api-example', 'videos', [{
194
215
  :external_id => 'FHtvDA0W34I',
195
216
  :fields => [
196
217
  {:name => 'title', :value => "Felix Baumgartner's supersonic freefall from 128k' - Mission Highlights", :type => 'string'},
@@ -202,7 +223,59 @@ Create or update multiple `Documents` at once:
202
223
  {:name => 'title', :value => 'Evolution of Dance - By Judson Laipply', :type => 'string'},
203
224
  {:name => 'url', :value => 'http://www.youtube.com/watch?v='dMH0bHeiRNg', :type => 'enum'},
204
225
  {:name => 'chanel_id', :value => UC5B9H4l2vtgo7cAoExcFh-w', :type => 'enum'}
205
- ]}])
226
+ ]}],
227
+ :async => true )
228
+ #=> {
229
+ "batch_link": "https://api.swiftype.com/api/v1/document_receipts.json?ids=5473d6142ed96065a9000001,5473d6142ed96065a9000002",
230
+ "document_receipts": [
231
+ {
232
+ "id": "5473d6142ed96065a9000001",
233
+ "external_id": "FHtvDA0W34I",
234
+ "status": "pending",
235
+ "errors": [],
236
+ "links": {
237
+ "receipt": "https://api.swiftype.com/api/v1/document_receipts/5473d6142ed96065a9000001.json",
238
+ "document": null
239
+ }
240
+ },
241
+ {
242
+ "id": "5473d6342ed96065a9000002",
243
+ "external_id": "dMH0bHeiRNg",
244
+ "status": "pending",
245
+ "errors": [],
246
+ "links": {
247
+ "receipt": "https://api.swiftype.com/api/v1/document_receipts/5473d6142ed96065a9000002.json",
248
+ "document": null
249
+ }
250
+ }
251
+ ]
252
+ }
253
+
254
+ To check the status of documents with their document_receipt ids:
255
+
256
+ response = client.document_receipts(["5473d6142ed96065a9000001", "5473d6342ed96065a9000002"])
257
+ #=> [
258
+ {
259
+ "id": "5473d6142ed96065a9000001",
260
+ "external_id": "FHtvDA0W34I",
261
+ "status": "complete",
262
+ "errors": [],
263
+ "links": {
264
+ "receipt": "https://api.swiftype.com/api/v1/document_receipts/5473d6142ed96065a9000001.json",
265
+ "document": "https://api.swiftype.com/api/v1/engine/xyz/document_type/abc/document/5473d6142ed96065a9000001.json"
266
+ }
267
+ },
268
+ {
269
+ "id": "5473d6142ed96065a9000002",
270
+ "external_id": "dMH0bHeiRNg",
271
+ "status": "complete",
272
+ "errors": [],
273
+ "links": {
274
+ "receipt": "https://api.swiftype.com/api/v1/document_receipts/5473d6142ed96065a9000001.json",
275
+ "document": "https://api.swiftype.com/api/v1/engine/xyz/document_type/abc/document/5473d6142ed96065a9000002.json"
276
+ }
277
+ }
278
+ ]
206
279
 
207
280
  Destroy a Document by external_id:
208
281
 
@@ -291,7 +364,7 @@ swiftype-rb 1.0 has been rewritten to be simpler and easier to use. However, it
291
364
 
292
365
  To upgrade from the old version of swiftype-rb:
293
366
 
294
- * If you previously used the `Swiftype` client, migrate your API calls to the `Swiftype::Client` format.
367
+ * If you previously used the `Swiftype` client, migrate your API calls to the `Swiftype::Client` format.
295
368
  * If you previously used the `Swiftype::Easy` client, change `Swiftype::Easy` to `Swiftype::Client`. Almost all method calls should be the same (there are a few minor changes).
296
369
 
297
370
  Additionally, the result object returned by search methods (for example, the old Swiftype gem's `Engine#search` or `Swiftype::Easy#search` methods) returns results in a different way. Instead of being an array of `Swiftype::Document` objects, it will be an array of Hashes.
@@ -14,9 +14,12 @@ module Swiftype
14
14
 
15
15
  # Create a new Swiftype::Client client
16
16
  #
17
- # @param options [Hash] a hash of configuration options that will overrided what is set on the Swiftype class.
17
+ # @param options [Hash] a hash of configuration options that will override what is set on the Swiftype class.
18
18
  # @option options [String] :api_key an API Key to use for this client
19
19
  # @option options [String] :platform_access_token a user's access token, will be used instead of API key for authenticating requests
20
+ # @option options [Fixnum] :open_timeout the number of seconds Net::HTTP
21
+ # will wait while opening a connection before raising a Timeout::Error
22
+
20
23
  def initialize(options={})
21
24
  @options = options
22
25
  end
@@ -29,6 +32,10 @@ module Swiftype
29
32
  @options[:platform_access_token]
30
33
  end
31
34
 
35
+ def open_timeout
36
+ @options[:open_timeout]
37
+ end
38
+
32
39
  # Methods wrapping the Swiftype private search and API endpoints. Using these methods, you can perform full-text
33
40
  # and prefix searches over the Documents in your Engine, in a specific DocumentType, or any subset of DocumentTypes.
34
41
  # You can also filter results and get faceted counts for results.
@@ -312,6 +319,51 @@ module Swiftype
312
319
  def update_documents(engine_id, document_type_id, documents={})
313
320
  put("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_update.json", { :documents => documents })
314
321
  end
322
+
323
+ def async_create_or_update_documents(engine_id, document_type_id, documents=[])
324
+ post("engines/#{engine_id}/document_types/#{document_type_id}/documents/async_bulk_create_or_update.json", :documents => documents)
325
+ end
326
+
327
+ # Retrieve Document Receipts from the API by ID
328
+ #
329
+ # @param [Array<String>] receipt_ids an Array of Document Receipt IDs
330
+ #
331
+ # @return [Array<Hash>] an Array of Document Receipt hashes
332
+ def document_receipts(receipt_ids)
333
+ post("document_receipts.json", :ids => receipt_ids)
334
+ end
335
+
336
+ # Index a batch of documents using the {asynchronous API}[https://swiftype.com/documentation/asynchronous_indexing].
337
+ # This is a good choice if you have a large number of documents.
338
+ #
339
+ # @param [String] engine_id the Engine slug or ID
340
+ # @param [String] document_type_id the Document Type slug or ID
341
+ # @param [Array] documents an Array of Document Hashes
342
+ # @param [Hash] options additional options
343
+ # @option options [Boolean] :async (false) When true, output is document receipts created. When false, poll until all receipts are no longer pending or timeout is reached.
344
+ # @option options [Numeric] :timeout (10) Number of seconds to wait before raising an exception
345
+ #
346
+ # @return [Array<Hash>] an Array of newly-created Document Receipt hashes if used in :async => true mode
347
+ # @return [Array<Hash>] an Array of processed Document Receipt hashes if used in :async => false mode
348
+ #
349
+ # @raise [Timeout::Error] when used in :async => false mode and the timeout expires
350
+ def index_documents(engine_id, document_type_id, documents = [], options = {})
351
+ documents = Array(documents)
352
+
353
+ res = async_create_or_update_documents(engine_id, document_type_id, documents)
354
+
355
+ if options[:async]
356
+ res
357
+ else
358
+ receipt_ids = res["document_receipts"].map { |a| a["id"] }
359
+
360
+ poll(options) do
361
+ receipts = document_receipts(receipt_ids)
362
+ flag = receipts.all? { |a| a["status"] != "pending" }
363
+ flag ? receipts : false
364
+ end
365
+ end
366
+ end
315
367
  end
316
368
 
317
369
  # The analytics API provides a way to export analytics data similar to what is found in the Swiftype Dashboard.
@@ -26,11 +26,35 @@ module Swiftype
26
26
  request(:delete, path, params)
27
27
  end
28
28
 
29
+ # Poll a block with backoff until a timeout is reached.
30
+ #
31
+ # @param [Hash] options optional arguments
32
+ # @option options [Numeric] :timeout (10) Number of seconds to wait before timing out
33
+ #
34
+ # @yieldreturn a truthy value to return from poll
35
+ # @yieldreturn [false] to continue polling.
36
+ #
37
+ # @return the truthy value returned from the block.
38
+ #
39
+ # @raise [Timeout::Error] when the timeout expires
40
+ def poll(options={})
41
+ timeout = options[:timeout] || 10
42
+ delay = 0.05
43
+ Timeout.timeout(timeout) do
44
+ while true
45
+ res = yield
46
+ return res if res
47
+ sleep delay *= 2
48
+ end
49
+ end
50
+ end
51
+
29
52
  def request(method, path, params={})
30
53
  uri = URI.parse("#{Swiftype.endpoint}#{path}")
31
54
 
32
55
  request = build_request(method, uri, params)
33
56
  http = Net::HTTP.new(uri.host, uri.port)
57
+ http.open_timeout = @options[:open_timeout]
34
58
 
35
59
  if uri.scheme == 'https'
36
60
  http.use_ssl = true
@@ -1,3 +1,3 @@
1
1
  module Swiftype
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end