简体   繁体   中英

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.

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()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM