Skip to content

Commit ede8ed1

Browse files
committed
mode -> action
1 parent e306d9f commit ede8ed1

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

examples/collections_and_documents.rb

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@
168168
# }
169169

170170
# You can also upsert a document, which will update the document if it already exists or create a new one if it doesn't exist
171-
# document = @typesense.collections['companies'].documents.upsert(document)
172-
# ap document
171+
document = @typesense.collections['companies'].documents.upsert(document)
172+
ap document
173173

174174
##
175175
# Retrieve a document
@@ -187,20 +187,22 @@
187187
##
188188
# Update a document. Unlike upsert, update will error out if the doc doesn't already exist.
189189
document = @typesense.collections['companies'].documents['124'].update(
190+
'id' => 1,
190191
'num_employees' => 5500
191192
)
192193
ap document
193194

194-
# This should error out, since document 145 doesn't exist
195-
# document = @typesense.collections['companies'].documents['145'].update(
196-
# 'num_employees' => 5500
197-
# )
198-
199195
# {
200196
# "id" => "124",
201197
# "num_employees" => 5500
202198
# }
203199

200+
# This should error out, since document 145 doesn't exist
201+
# document = @typesense.collections['companies'].documents['145'].update(
202+
# 'num_employees' => 5500
203+
# )
204+
# ap document
205+
204206
##
205207
# Delete a document
206208
# Deleting a document, returns the document after deletion
@@ -234,12 +236,27 @@
234236
## If you already have documents in JSONL format, you can also pass it directly to #import, to avoid the JSON parsing overhead:
235237
# @typesense.collections['companies'].documents.import(documents_in_jsonl_format)
236238

237-
## You can bulk upsert documents, by adding an upsert mode option to #import
238-
ap @typesense.collections['companies'].documents.import(documents, mode: :upsert)
239+
## You can bulk upsert documents, by adding an upsert action option to #import
240+
documents << {
241+
'id' => '126',
242+
'company_name' => 'Stark Industries 2',
243+
'num_employees' => 200,
244+
'country' => 'USA'
245+
}
246+
ap @typesense.collections['companies'].documents.import(documents, action: :upsert)
239247

240-
## You can bulk upsert documents, by adding an update mode option to #import
241-
# `mode: update` will throw an error if the document doesn't already exist
242-
ap @typesense.collections['companies'].documents.import(documents, mode: :update)
248+
## You can bulk update documents, by adding an update action option to #import
249+
# `action: update` will throw an error if the document doesn't already exist
250+
# This document will error out, since id: 1200 doesn't exist
251+
documents << {
252+
'id' => '1200',
253+
'country' => 'USA'
254+
}
255+
documents << {
256+
'id' => '126',
257+
'num_employees' => 300
258+
}
259+
ap @typesense.collections['companies'].documents.import(documents, action: :update)
243260

244261
##
245262
# Export all documents in a collection in JSON Lines format

lib/typesense/documents.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ def create(document)
1717
end
1818

1919
def upsert(document)
20-
@api_call.post(endpoint_path, document, mode: :upsert)
20+
@api_call.post(endpoint_path, document, action: :upsert)
2121
end
2222

2323
def update(document)
24-
@api_call.post(endpoint_path, document, mode: :update)
24+
@api_call.post(endpoint_path, document, action: :update)
2525
end
2626

2727
def create_many(documents, options = {})

spec/typesense/documents_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
'Content-Type' => 'application/json'
6868
},
6969
query: {
70-
'mode' => 'update'
70+
'action' => 'update'
7171
})
7272
.to_return(status: 200, body: JSON.dump(document), headers: { 'Content-Type': 'application/json' })
7373

@@ -86,7 +86,7 @@
8686
'Content-Type' => 'application/json'
8787
},
8888
query: {
89-
'mode' => 'upsert'
89+
'action' => 'upsert'
9090
})
9191
.to_return(status: 200, body: JSON.dump(document), headers: { 'Content-Type': 'application/json' })
9292

@@ -140,11 +140,11 @@
140140
'X-Typesense-Api-Key' => typesense.configuration.api_key
141141
},
142142
query: {
143-
'mode' => 'upsert'
143+
'action' => 'upsert'
144144
})
145145
.to_return(status: 200, body: '{}', headers: { 'Content-Type': 'text/plain' })
146146

147-
result = companies_documents.import("#{JSON.dump(document)}\n#{JSON.dump(document)}", mode: :upsert)
147+
result = companies_documents.import("#{JSON.dump(document)}\n#{JSON.dump(document)}", action: :upsert)
148148

149149
expect(result).to eq('{}')
150150
end

0 commit comments

Comments
 (0)