class Typesense::Documents

Constants

RESOURCE_PATH

Public Class Methods

new(collection_name, api_call) click to toggle source
# File lib/typesense/documents.rb, line 9
def initialize(collection_name, api_call)
  @collection_name = collection_name
  @api_call        = api_call
  @documents       = {}
end

Public Instance Methods

[](document_id) click to toggle source
# File lib/typesense/documents.rb, line 63
def [](document_id)
  @documents[document_id] ||= Document.new(@collection_name, document_id, @api_call)
end
create(document, options = {}) click to toggle source
# File lib/typesense/documents.rb, line 15
def create(document, options = {})
  @api_call.post(endpoint_path, document, options)
end
create_many(documents, options = {}) click to toggle source
# File lib/typesense/documents.rb, line 27
def create_many(documents, options = {})
  @api_call.logger.warn('#create_many is deprecated and will be removed in a future version. Use #import instead, which now takes both an array of documents or a JSONL string of documents')
  import(documents, options)
end
delete(query_parameters = {}) click to toggle source
# File lib/typesense/documents.rb, line 67
def delete(query_parameters = {})
  @api_call.delete(endpoint_path, query_parameters)
end
export(options = {}) click to toggle source
# File lib/typesense/documents.rb, line 55
def export(options = {})
  @api_call.get(endpoint_path('export'), options)
end
import(documents, options = {}) click to toggle source

@param [Array,String] documents An array of document hashes or a JSONL string of documents.

# File lib/typesense/documents.rb, line 33
def import(documents, options = {})
  documents_in_jsonl_format = if documents.is_a?(Array)
                                documents.map { |document| Oj.dump(document) }.join("\n")
                              else
                                documents
                              end

  results_in_jsonl_format = @api_call.perform_request(
    'post',
    endpoint_path('import'),
    query_parameters: options,
    body_parameters: documents_in_jsonl_format,
    additional_headers: { 'Content-Type' => 'text/plain' }
  )

  if documents.is_a?(Array)
    results_in_jsonl_format.split("\n").map { |r| Oj.load(r) }
  else
    results_in_jsonl_format
  end
end
update(document, options = {}) click to toggle source
# File lib/typesense/documents.rb, line 23
def update(document, options = {})
  @api_call.post(endpoint_path, document, options.merge(action: :update))
end
upsert(document, options = {}) click to toggle source
# File lib/typesense/documents.rb, line 19
def upsert(document, options = {})
  @api_call.post(endpoint_path, document, options.merge(action: :upsert))
end

Private Instance Methods

endpoint_path(operation = nil) click to toggle source
# File lib/typesense/documents.rb, line 73
def endpoint_path(operation = nil)
  "#{Collections::RESOURCE_PATH}/#{@collection_name}#{Documents::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}"
end