Class: Typesense::Documents

Inherits:
Object
  • Object
show all
Defined in:
lib/typesense/documents.rb

Constant Summary collapse

RESOURCE_PATH =
'/documents'

Instance Method Summary collapse

Constructor Details

#initialize(collection_name, api_call) ⇒ Documents

Returns a new instance of Documents.



9
10
11
12
13
# File 'lib/typesense/documents.rb', line 9

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

Instance Method Details

#[](document_id) ⇒ Object



76
77
78
# File 'lib/typesense/documents.rb', line 76

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

#create(document, options = {}) ⇒ Object



15
16
17
# File 'lib/typesense/documents.rb', line 15

def create(document, options = {})
  @api_call.post(endpoint_path, document, options)
end

#create_many(documents, options = {}) ⇒ Object



31
32
33
34
# File 'lib/typesense/documents.rb', line 31

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 = {}) ⇒ Object



80
81
82
# File 'lib/typesense/documents.rb', line 80

def delete(query_parameters = {})
  @api_call.delete(endpoint_path, query_parameters)
end

#export(options = {}) ⇒ Object



68
69
70
# File 'lib/typesense/documents.rb', line 68

def export(options = {})
  @api_call.get(endpoint_path('export'), options)
end

#import(documents, options = {}) ⇒ Object

Parameters:

  • documents (Array, String)

    An array of document hashes or a JSONL string of documents.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/typesense/documents.rb', line 37

def import(documents, options = {})
  documents_in_jsonl_format = if documents.is_a?(Array)
                                documents.map { |document| JSON.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 do |r|
      JSON.parse(r)
    rescue JSON::ParserError => e
      {
        'success' => false,
        'exception' => e.class.name,
        'error' => e.message,
        'json' => r
      }
    end
  else
    results_in_jsonl_format
  end
end

#search(search_parameters) ⇒ Object



72
73
74
# File 'lib/typesense/documents.rb', line 72

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

#truncateObject



84
85
86
# File 'lib/typesense/documents.rb', line 84

def truncate
  @api_call.delete(endpoint_path, { truncate: true })
end

#update(document, options = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/typesense/documents.rb', line 23

def update(document, options = {})
  if options['filter_by'] || options[:filter_by]
    @api_call.patch(endpoint_path, document, options)
  else
    @api_call.post(endpoint_path, document, options.merge(action: :update))
  end
end

#upsert(document, options = {}) ⇒ Object



19
20
21
# File 'lib/typesense/documents.rb', line 19

def upsert(document, options = {})
  @api_call.post(endpoint_path, document, options.merge(action: :upsert))
end