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



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

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



303
304
305
# File 'lib/swiftype/client.rb', line 303

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



307
308
309
# File 'lib/swiftype/client.rb', line 307

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



319
320
321
# File 'lib/swiftype/client.rb', line 319

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



323
324
325
# File 'lib/swiftype/client.rb', line 323

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



327
328
329
# File 'lib/swiftype/client.rb', line 327

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



311
312
313
# File 'lib/swiftype/client.rb', line 311

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



315
316
317
# File 'lib/swiftype/client.rb', line 315

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



299
300
301
# File 'lib/swiftype/client.rb', line 299

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

Parameters:

  • receipt_ids (Array<String>)

    an Array of Document Receipt IDs

Returns:

  • (Array<Hash>)

    an Array of Document Receipt hashes



348
349
350
# File 'lib/swiftype/client.rb', line 348

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



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

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.

Parameters:

  • engine_id (String)

    the Engine slug or ID

  • document_type_id (String)

    the Document Type slug or ID

  • documents (Array) (defaults to: [])

    an Array of Document Hashes

  • options (Hash) (defaults to: {})

    additional options

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

Returns:

  • (Array<Hash>)

    an Array of newly-created Document Receipt hashes if used in :async => true mode

  • (Array<Hash>)

    an Array of processed Document Receipt hashes if used in :async => false mode

Raises:

  • (Timeout::Error)

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



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/swiftype/client.rb', line 366

def index_documents(engine_id, document_type_id, documents = [], options = {})
  documents = wrap(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



331
332
333
# File 'lib/swiftype/client.rb', line 331

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



335
336
337
# File 'lib/swiftype/client.rb', line 335

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