Sha256: 4d5e44e7e379e58d92175feae32c513561893c8ba949a65316b45c0b2b221448

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 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")
      import(documents_in_jsonl_format)
    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')) || '').split("\n")
    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

1 entries across 1 versions & 1 rubygems

Version Path
typesense-0.6.0 lib/typesense/documents.rb