Module: Swiftype::Client::Document

Included in:
Swiftype::Client
Defined in:
lib/swiftype/client.rb

Overview

Documents have fields that can be searched or filtered.

For more information on indexing documents, see the REST API indexing documentation.

Instance Method Summary collapse

Instance Method Details

#async_create_or_update_documents(engine_id, document_type_id, documents = []) ⇒ Object


329
330
331
# File 'lib/swiftype/client.rb', line 329

def async_create_or_update_documents(engine_id, document_type_id, documents=[])
  post("engines/#{engine_id}/document_types/#{document_type_id}/documents/async_bulk_create_or_update.json", :documents => documents)
end

#create_document(engine_id, document_type_id, document = {}) ⇒ Object


293
294
295
# File 'lib/swiftype/client.rb', line 293

def create_document(engine_id, document_type_id, document={})
  post("engines/#{engine_id}/document_types/#{document_type_id}/documents.json", :document => document)
end

#create_documents(engine_id, document_type_id, documents = []) ⇒ Object


297
298
299
# File 'lib/swiftype/client.rb', line 297

def create_documents(engine_id, document_type_id, documents=[])
  post("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_create.json", :documents => documents)
end

#create_or_update_document(engine_id, document_type_id, document = {}) ⇒ Object


309
310
311
# File 'lib/swiftype/client.rb', line 309

def create_or_update_document(engine_id, document_type_id, document={})
  post("engines/#{engine_id}/document_types/#{document_type_id}/documents/create_or_update.json", :document => document)
end

#create_or_update_documents(engine_id, document_type_id, documents = []) ⇒ Object


313
314
315
# File 'lib/swiftype/client.rb', line 313

def create_or_update_documents(engine_id, document_type_id, documents=[])
  post("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_create_or_update.json", :documents => documents)
end

#create_or_update_documents_verbose(engine_id, document_type_id, documents = []) ⇒ Object


317
318
319
# File 'lib/swiftype/client.rb', line 317

def create_or_update_documents_verbose(engine_id, document_type_id, documents=[])
  post("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_create_or_update_verbose.json", :documents => documents)
end

#destroy_document(engine_id, document_type_id, document_id) ⇒ Object


301
302
303
# File 'lib/swiftype/client.rb', line 301

def destroy_document(engine_id, document_type_id, document_id)
  delete("engines/#{engine_id}/document_types/#{document_type_id}/documents/#{document_id}.json")
end

#destroy_documents(engine_id, document_type_id, document_ids = []) ⇒ Object


305
306
307
# File 'lib/swiftype/client.rb', line 305

def destroy_documents(engine_id, document_type_id, document_ids=[])
  post("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_destroy.json", :documents => document_ids)
end

#document(engine_id, document_type_id, document_id) ⇒ Object


289
290
291
# File 'lib/swiftype/client.rb', line 289

def document(engine_id, document_type_id, document_id)
  get("engines/#{engine_id}/document_types/#{document_type_id}/documents/#{document_id}.json")
end

#document_receipts(receipt_ids) ⇒ Array<Hash>

Retrieve Document Receipts from the API by ID


338
339
340
# File 'lib/swiftype/client.rb', line 338

def document_receipts(receipt_ids)
  post("document_receipts.json", :ids => receipt_ids)
end

#documents(engine_id, document_type_id, page = nil, per_page = nil) ⇒ Object


282
283
284
285
286
287
# File 'lib/swiftype/client.rb', line 282

def documents(engine_id, document_type_id, page=nil, per_page=nil)
  options = {}
  options[:page] = page if page
  options[:per_page] = per_page if per_page
  get("engines/#{engine_id}/document_types/#{document_type_id}/documents.json", options)
end

#index_documents(engine_id, document_type_id, documents = [], options = {}) ⇒ Array<Hash>

Index a batch of documents using the asynchronous API. This is a good choice if you have a large number of documents.

Options Hash (options):

  • :async (Boolean) — default: false

    When true, output is document receipts created. When false, poll until all receipts are no longer pending or timeout is reached.

  • :timeout (Numeric) — default: 10

    Number of seconds to wait before raising an exception

Raises:

  • (Timeout::Error)

    when used in :async => false mode and the timeout expires


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/swiftype/client.rb', line 356

def index_documents(engine_id, document_type_id, documents = [], options = {})
  documents = Array(documents)

  res = async_create_or_update_documents(engine_id, document_type_id, documents)

  if options[:async]
    res
  else
    receipt_ids = res["document_receipts"].map { |a| a["id"] }

    poll(options) do
      receipts = document_receipts(receipt_ids)
      flag = receipts.all? { |a| a["status"] != "pending" }
      flag ? receipts : false
    end
  end
end

#update_document(engine_id, document_type_id, document_id, fields) ⇒ Object


321
322
323
# File 'lib/swiftype/client.rb', line 321

def update_document(engine_id, document_type_id, document_id, fields)
  put("engines/#{engine_id}/document_types/#{document_type_id}/documents/#{document_id}/update_fields.json", { :fields => fields })
end

#update_documents(engine_id, document_type_id, documents = {}) ⇒ Object


325
326
327
# File 'lib/swiftype/client.rb', line 325

def update_documents(engine_id, document_type_id, documents={})
  put("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_update.json", { :documents => documents })
end