1

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.

Ekaterina
  • 117
  • 1
  • 9
  • Possible duplicate of [ElasticSearch and NEST: How do you purge all documents from an index?](http://stackoverflow.com/questions/26917221/elasticsearch-and-nest-how-do-you-purge-all-documents-from-an-index) – Jim G. Oct 23 '15 at 15:24
  • @Jim G. it's easy to delete all documents from the index. My question was about the type. – Ekaterina Nov 09 '15 at 14:55

1 Answers1

2

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()));
Rob
  • 9,664
  • 3
  • 41
  • 43
  • Thanks for the reply. But I have documents of the same C# type in two different Elastic Search types, so if I follow your suggestion, I will delete both of my types when I need to delete only one. – Ekaterina May 22 '15 at 08:30