Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/collections_and_documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@
# "num_documents" => 0
# }

###
# Truncate a collection
# Deletion returns the number of documents deleted
collection = @typesense.collections['companies'].truncate
ap collection

# {
# "num_deleted": 125
# }

# Let's create the collection again for use in our remaining examples
@typesense.collections.create(schema)

Expand Down
2 changes: 1 addition & 1 deletion lib/typesense/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(name, api_call)
@api_call = api_call
@documents = Documents.new(@name, @api_call)
@overrides = Overrides.new(@name, @api_call)
@synonyms = Synonyms.new(@name, @api_call)
@synonyms = Synonyms.new(@name, @api_call)
end

def retrieve
Expand Down
4 changes: 4 additions & 0 deletions lib/typesense/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def delete(query_parameters = {})
@api_call.delete(endpoint_path, query_parameters)
end

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

private

def endpoint_path(operation = nil)
Expand Down
18 changes: 18 additions & 0 deletions spec/typesense/documents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@
end
end

describe '#truncate' do
it 'truncate documents in a collection' do
stub_request(:delete, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents', typesense.configuration.nodes[0]))
.with(headers: {
'X-Typesense-Api-Key' => typesense.configuration.api_key,
'Content-Type' => 'application/json'
},
query: {
truncate: true
})
.to_return(status: 200, body: '{ "num_deleted": 1 }', headers: { 'Content-Type': 'application/json' })

result = companies_documents.truncate

expect(result['num_deleted']).to eq(1)
end
end

describe '#search' do
let(:search_parameters) do
{
Expand Down