NEST ElasticSearch 删除类型

NEST ElasticSearch Delete type

如果有人告诉我使用 NEST 删除特定类型的所有数据的正确方法,我将不胜感激。 我的 elasticsearch 中有一个索引和两种类型,我希望能够在需要时删除一种或另一种类型的所有数据。

我目前的想法是

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

提前致谢。

试试这个:

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

更新:

如果您使用一个 class 来存储两种类型的文档,您可以使用 .Type() 参数来指定要删除哪一种。

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

我的例子:

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