Ekaterina
Ekaterina

Reputation: 117

NEST ElasticSearch Delete type

I will be very grateful if someone tells me the correct way to delete all data in specific type using NEST. I have one index in my elasticsearch and two types and I would like to be able to delete all data in one or the other type when I need it.

My current idea is

ElasticClient.DeleteByQuery<ISearchData>(q => q.Index(indexName).Type(type.ToString()).Query(qu => qu.Bool(b => b.Must(m => m.MatchAll()))));

Thanks in advance.

Upvotes: 1

Views: 3828

Answers (1)

Rob
Rob

Reputation: 9979

Try this one:

var deleteByQuery = client.DeleteByQuery<Document>(d => d.MatchAll());

UPDATE:

In case you are using one class to store documents in two types, you can use .Type() parameter to specify which one would you like to delete.

client.DeleteByQuery<Document>(descriptor => descriptor.Type("type1").Query(q => q.MatchAll()));

My example:

client.Index(new Document {Id = 2}, descriptor => descriptor.Type("type1"));
client.Index(new Document {Id = 1}, descriptor => descriptor.Type("type1"));
client.Index(new Document {Id = 2}, descriptor => descriptor.Type("type2"));

client.Refresh();

client.DeleteByQuery<Document>(descriptor => descriptor.Type("type1").Query(q => q.MatchAll()));

Upvotes: 2

Related Questions