|
168 | 168 | # } |
169 | 169 |
|
170 | 170 | # 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 |
173 | 173 |
|
174 | 174 | ## |
175 | 175 | # Retrieve a document |
|
187 | 187 | ## |
188 | 188 | # Update a document. Unlike upsert, update will error out if the doc doesn't already exist. |
189 | 189 | document = @typesense.collections['companies'].documents['124'].update( |
| 190 | + 'id' => 1, |
190 | 191 | 'num_employees' => 5500 |
191 | 192 | ) |
192 | 193 | ap document |
193 | 194 |
|
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 | | - |
199 | 195 | # { |
200 | 196 | # "id" => "124", |
201 | 197 | # "num_employees" => 5500 |
202 | 198 | # } |
203 | 199 |
|
| 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 | + |
204 | 206 | ## |
205 | 207 | # Delete a document |
206 | 208 | # Deleting a document, returns the document after deletion |
|
234 | 236 | ## If you already have documents in JSONL format, you can also pass it directly to #import, to avoid the JSON parsing overhead: |
235 | 237 | # @typesense.collections['companies'].documents.import(documents_in_jsonl_format) |
236 | 238 |
|
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) |
239 | 247 |
|
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) |
243 | 260 |
|
244 | 261 | ## |
245 | 262 | # Export all documents in a collection in JSON Lines format |
|
0 commit comments