Skip to content

Commit b13d2dd

Browse files
committed
Support for update by query
1 parent f6ffed1 commit b13d2dd

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/typesense/documents.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def upsert(document, options = {})
2121
end
2222

2323
def update(document, options = {})
24-
@api_call.post(endpoint_path, document, options.merge(action: :update))
24+
if options['filter_by'] || options[:filter_by]
25+
@api_call.patch(endpoint_path, document, options)
26+
else
27+
@api_call.post(endpoint_path, document, options.merge(action: :update))
28+
end
2529
end
2630

2731
def create_many(documents, options = {})

spec/typesense/documents_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@
6262
end
6363

6464
describe '#update' do
65+
context 'update by query' do
66+
it 'updates the document and returns it' do
67+
stub_request(:patch, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents', typesense.configuration.nodes[0]))
68+
.with(body: document,
69+
headers: {
70+
'X-Typesense-Api-Key' => typesense.configuration.api_key,
71+
'Content-Type' => 'application/json'
72+
},
73+
query: {
74+
'filter_by' => 'field:=value',
75+
'dirty_values' => 'coerce_or_reject'
76+
})
77+
.to_return(status: 200, body: JSON.dump(document), headers: { 'Content-Type': 'application/json' })
78+
79+
result = companies_documents.update(document, dirty_values: 'coerce_or_reject', filter_by: 'field:=value')
80+
81+
expect(result).to eq(document)
82+
end
83+
end
84+
6585
it 'updates the document and returns it' do
6686
stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents', typesense.configuration.nodes[0]))
6787
.with(body: document,

0 commit comments

Comments
 (0)