【问题标题】:ElasticSearch NEST Delete all documentElasticSearch NEST 删除所有文档
【发布时间】:2016-08-31 08:28:22
【问题描述】:

我正在使用 ElastciSearch 2.3.0

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

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

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

我尝试使用上面的代码来做到这一点,但它不起作用。

请就上述代码有什么问题或如何做到这一点提出建议。

提前感谢您的帮助。

【问题讨论】:

  • 您运行的是哪个版本的 Elasticsearch?
  • 我建议将映射复制到新索引并删除现有索引。删除索引中的所有个文档以保留映射是一项非常昂贵的操作!
  • 我使用的是2.3.0版本的elasticsearch

标签: c# elasticsearch nest


【解决方案1】:

不要使用查询删除,因为它是自弹性 2.0 以来的一个插件,这是有充分理由的。您可以轻松摆脱内存异常。 您应该删除整个索引并重新创建映射

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/plugin install delete-by-query

【讨论】:

  • 嗨 Filip,非常感谢。我还有一个问题。就我而言,我没有像“数据”这样的课程。当我尝试在 client.GetMapping 运行代码时出现错误。 'var MappingRequest = new GetMappingRequest(indexToCreate); var map = client.GetMapping(MappingRequest);'
  • 没问题就用 db.GetMapping(new GetMappingRequest(new Nest.IndexName() { Name = "test_index" }) { });
  • 这给出了以下错误。 '#FailureReason: Unrecoverable/Unexpected BadResponse while GET localhost:9200/contact/_mapping - [1] BadResponse: Node: localhost:9200 Exception: JsonSerializationException Take: 00:00:00.3850932 # AuditResponse in step 1 BadResponse: Newtonsoft.Json.JsonSerializationException: Error将值“分析”转换为类型“System.Nullable`1[Nest.NonStringIndexOption]”。路径“索引”,第 1 行,位置 128。---> System.ArgumentException:未找到请求的值“已分析”。'
  • 你得到删除前的映射了吗?
  • 试试这个var res = db.LowLevel.IndicesGetMapping&lt;JObject&gt;("test_index"); var delete = db.DeleteIndex(new DeleteIndexRequest("test_index")); var mappings = res.Body["test_index"].ToString(); var create = db.LowLevel.IndicesCreate&lt;JObject&gt;("test_index", mappings);
【解决方案2】:

成功了。 非常感谢。

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

【讨论】:

  • 如果有帮助,请为他们的答案投票,如果它提供了问题的解决方案,请将其标记为已接受。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多