Skip to content

Commit 50c9cf9

Browse files
authored
ported PR #2434 over manually because the reindex observable implementation deviates too much (#2435)
1 parent 4cf57b7 commit 50c9cf9

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/Nest/Document/Multiple/Reindex/ReindexObservable.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
24
using Elasticsearch.Net;
35

46
namespace Nest
@@ -43,12 +45,15 @@ private void Reindex(IObserver<IReindexResponse<T>> observer)
4345
var resolvedTo = toIndex.Resolve(this._connectionSettings);
4446
resolvedTo.ThrowIfNullOrEmpty(nameof(toIndex));
4547

46-
var indexSettings = this._client.GetIndex(fromIndex);
47-
Func<CreateIndexDescriptor, ICreateIndexRequest> settings = (ci) => this._reindexRequest.CreateIndexRequest ?? ci;
48-
var createIndexResponse = this._client.CreateIndex(
49-
toIndex, (c) => settings(c.InitializeUsing(indexSettings.Indices[resolvedFrom])));
50-
if (!createIndexResponse.IsValid)
51-
throw new ElasticsearchClientException(PipelineFailure.BadResponse, $"Failed to create destination index {toIndex}.", createIndexResponse.ApiCall);
48+
if (!this._reindexRequest.OmitIndexCreation)
49+
{
50+
var indexSettings = this._client.GetIndex(fromIndex);
51+
Func<CreateIndexDescriptor, ICreateIndexRequest> settings = (ci) => this._reindexRequest.CreateIndexRequest ?? ci;
52+
var createIndexResponse = this._client.CreateIndex(
53+
toIndex, (c) => settings(c.InitializeUsing(indexSettings.Indices[resolvedFrom])));
54+
if (!createIndexResponse.IsValid)
55+
throw new ElasticsearchClientException(PipelineFailure.BadResponse, $"Failed to create destination index {toIndex}.", createIndexResponse.ApiCall);
56+
}
5257

5358
var page = 0;
5459
var searchResult = this._client.Search<T>(
@@ -58,6 +63,7 @@ private void Reindex(IObserver<IReindexResponse<T>> observer)
5863
.From(0)
5964
.Size(size)
6065
.Query(q=>this._reindexRequest.Query)
66+
.Sort(sort=>sort.Field(f=>f.Field("_doc")))
6167
.SearchType(SearchType.Scan)
6268
.Scroll(scroll)
6369
);

src/Nest/Document/Multiple/Reindex/ReindexRequest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ public interface IReindexRequest
1414

1515
ICreateIndexRequest CreateIndexRequest { get; set; }
1616

17+
/// <summary>
18+
/// Do not send a create index call on <see cref="To"/>, assume the index has been created outside of the reindex.
19+
/// </summary>
20+
bool OmitIndexCreation { get; set; }
21+
22+
[Obsolete("Unused property that is removed in 5.0")]
1723
IPutMappingRequest PutMappingRequest { get; set; }
1824
}
1925

2026
public class ReindexRequest : IReindexRequest
2127
{
28+
public bool OmitIndexCreation { get; set; }
2229
public IndexName To { get; set; }
2330
public IndexName From { get; set; }
2431
public Types Type { get; set; }
@@ -37,6 +44,7 @@ public ReindexRequest(IndexName from, IndexName to, Types type)
3744

3845
public class ReindexDescriptor<T> : DescriptorBase<ReindexDescriptor<T>, IReindexRequest>, IReindexRequest where T : class
3946
{
47+
bool IReindexRequest.OmitIndexCreation { get; set; }
4048
IndexName IReindexRequest.To { get; set; }
4149
IndexName IReindexRequest.From { get; set; }
4250
Types IReindexRequest.Type { get; set; }
@@ -77,6 +85,9 @@ public ReindexDescriptor(IndexName from, IndexName to)
7785
public ReindexDescriptor<T> Query(Func<QueryContainerDescriptor<T>, QueryContainer> querySelector) =>
7886
Assign(a => a.Query = querySelector?.Invoke(new QueryContainerDescriptor<T>()));
7987

88+
/// <inheritdoc/>
89+
public ReindexDescriptor<T> OmitIndexCreation(bool omit = true) => Assign(a => a.OmitIndexCreation = true);
90+
8091
/// <summary>
8192
/// A query to optionally limit the documents to use for the reindex operation.
8293
/// </summary>

0 commit comments

Comments
 (0)