【问题标题】:Elasticsearch NEST client ReindexOnServer type problemElasticsearch NEST 客户端 ReindexOnServer 类型问题
【发布时间】:2020-10-27 09:08:09
【问题描述】:

基本上我想重写这段代码:

  var values = "{\"source\":{\"index\": \"" + oldIndex.Index + "\",\"type\": \"" + MyType+ "\"},"
                                     + "\",\"size\": \"" + size + "\"},"
                                     + "\"dest\": {\"index\": \"" + tempIndex + "\",\"type\": \"_doc\"}}";

            var result = this.httpClient.PostAsync(ReIndexUrl, new StringContent(values, System.Text.Encoding.UTF8, "application/json")).Result;
            var response = result.Content.ReadAsStringAsync().Result;

到 NEST 客户端:

  var response = elasticClient.ReindexOnServer(
                r => r
                    .Source(
                        s => s
                            .Index(sourceIndex)
                    )
                    .Destination(
                        d => d
                            .Index(targetIndex)
                    ).Refresh(false).Size(maximumDocuments)
                    .WaitForCompletion()
            );

在使用 NEST 版本将文档从 V6 重新索引到 V7 时,由于目标上的重复类型而出现错误:

java.lang.IllegalArgumentException: Rejecting mapping update to [audit-trail-2020-04-temp] as the final mapping would have more than 1 type: [_doc, audittrailentry]

由于无法排除类型,我找不到使用 NEST 解决此问题的方法?

【问题讨论】:

  • 你好,你有没有找到答案?我遇到了几乎完全相同的问题,并且在任何地方都看不到 NEST API 允许覆盖目标索引映射类型。
  • 检查我的答案

标签: elasticsearch nest


【解决方案1】:

不幸的是,我没有找到使用 NEST 客户端的解决方案。 最后我写了自己的扩展:

  internal static long ReIndex(this IElasticClient elasticClient, ReindexModel reindexModel)
        {
            elasticClient.DisableRefreshing(reindexModel.SourceIndex);

            var result = HttpClient.PostAsync(ReIndexUrl, new StringContent(JsonConvert.SerializeObject(reindexModel.Value), Encoding.UTF8, "application/json")).Result;
            var response = result.Content.ReadAsStringAsync().Result;
            var responseObj = JsonConvert.DeserializeObject<ReindexResponse>(response);
            return responseObj.Total;
        }

 internal class ReindexModel
    {     

        public ReindexModel(string sourceIndex, string targetIndex, string docType, int maxBulkSize)
        {
            this.SourceIndex = sourceIndex;
            this.TargetIndex = targetIndex;
            this.DocType = docType;           

            this.Value = new { source = new { index = this.SourceIndex, type = this.DocType, size = maxBulkSize }, dest = new { index = this.TargetIndex, type = "_doc" } };
        }

        public object Value { get; }

        public string SourceIndex { get; }

        public string TargetIndex { get; }

        private string DocType { get; }
    }

 internal class ReindexResponse
    {
        [JsonProperty]
        public long Total { get; set; }
    }

【讨论】:

    猜你喜欢
    • 2017-12-22
    • 2020-12-22
    • 2016-08-27
    • 2015-02-19
    • 1970-01-01
    • 2022-12-14
    • 2013-03-31
    • 2016-07-27
    • 2012-09-23
    相关资源
    最近更新 更多