ElasticSearch NEST删除所有文件 [英] ElasticSearch NEST Delete all document

查看:378
本文介绍了ElasticSearch NEST删除所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ElastciSearch 2.3.0

I am using ElastciSearch 2.3.0

我正在尝试使用.net和NEST从特定索引中删除ElasticSearch中的文档.

I am trying to delete documents from the ElasticSearch using .net and NEST for specific index.

我只想删除所有文档,而不要删除_mapping

I only want to delete all documents and not the _mapping

DeleteByQueryRequest r = new DeleteByQueryRequest(new IndexName() { Name = indexName });
r.QueryOnQueryString = "*";                            
var response = client.DeleteByQuery(r);

我尝试通过使用上面的代码来执行此操作,但是它不起作用.

I am try to do this by using above code but it is not working.

请建议上述代码有什么问题或如何实现.

Please suggest on what's wrong with the above code or how this can be done.

谢谢您的帮助.

推荐答案

由于弹性2.0是有充分的理由,因此它不被查询删除,它已成为一个插件.您可以轻松摆脱内存异常. 您应该删除整个索引并重新创建映射

Don't use delete by query it was made a plugin since elastic 2.0 for a good reason. You can get out of memory exceptions easy. You should delete the whole index and recreate the mappings

static void Main(string[] args)
        {
            ElasticClient db = new ElasticClient(new Uri("http://localhost.fiddler:9200"));

            db.IndexMany(Enumerable.Range(0, 100).Select(i => new Data { Id = i, Name = "Name" + i }), "test_index");

            var mappings = db.GetMapping<Data>();

            var delete = db.DeleteIndex(new DeleteIndexRequest("test_index"));

            var indexMapping = mappings.IndexTypeMappings["test_index"].ToDictionary(k => k.Key, v => (ITypeMapping)v.Value);

            db.CreateIndex(new CreateIndexRequest("test_index")
            {
                Mappings = new Mappings(indexMapping)
            });

            Console.ReadLine();
        }

        class Data 
        {
            public int Id { get; set; }

            public string Name { get; set; }
        }

索引的原始副本

var res = db.LowLevel.IndicesGetMapping<JObject>("test_index"); 
var delete = db.DeleteIndex(new DeleteIndexRequest("test_index"));
var mappings = res.Body["test_index"].ToString(); 
var create = db.LowLevel.IndicesCreate<JObject>("test_index", mappings);

如果您确实需要安装插件 sudo bin/插件安装按查询删除

If you really need to install the plug-in sudo bin/plugin install delete-by-query

这篇关于ElasticSearch NEST删除所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆