Sha256: af70bcf416378e0ea7fbd70a38e68646bd74e1352dc6c0d2e453b343921f9796

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'oj'

module Typesense
  class Documents
    RESOURCE_PATH = '/documents'

    def initialize(collection_name, api_call)
      @collection_name = collection_name
      @api_call        = api_call
      @documents       = {}
    end

    def create(document)
      @api_call.post(endpoint_path, document)
    end

    def create_many(documents)
      documents_in_jsonl_format = documents.map { |document| Oj.dump(document) }.join("\n")
      results_in_jsonl_format = import(documents_in_jsonl_format)
      results_in_jsonl_format.split("\n").map { |r| Oj.load(r) }
    end

    def import(documents_in_jsonl_format)
      @api_call.post(endpoint_path('import'), as_json: false, body: documents_in_jsonl_format)
    end

    def export
      @api_call.get(endpoint_path('export'))
    end

    def search(search_parameters)
      @api_call.get(endpoint_path('search'), search_parameters)
    end

    def [](document_id)
      @documents[document_id] ||= Document.new(@collection_name, document_id, @api_call)
    end

    private

    def endpoint_path(operation = nil)
      "#{Collections::RESOURCE_PATH}/#{@collection_name}#{Documents::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
typesense-0.7.0 lib/typesense/documents.rb
typesense-0.7.0.pre1 lib/typesense/documents.rb
typesense-0.7.0.pre0 lib/typesense/documents.rb