Skip to content

Commit 54a6ab9

Browse files
authored
fix #2164 you can now skip the create index step if you use our Reindex() helper, also made sure we use a _doc sort on the scroll (#2434)
1 parent 827b9b6 commit 54a6ab9

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.Linq;
45
using Elasticsearch.Net;
56

@@ -10,6 +11,7 @@ public class ReindexObservable<T> : IDisposable, IObservable<IReindexResponse<T>
1011
private readonly IReindexRequest _reindexRequest;
1112
private readonly IConnectionSettingsValues _connectionSettings;
1213
private readonly IElasticClient _client;
14+
private static IList<ISort> DocOrderSort = new ReadOnlyCollection<ISort>(new List<ISort> { new SortField { Field = "_doc" } });
1315

1416
private Action<IHit<T>, T, IBulkIndexOperation<T>> Alter { get; set; }
1517

@@ -19,6 +21,7 @@ public ReindexObservable(IElasticClient client, IConnectionSettingsValues connec
1921
this._reindexRequest = reindexRequest;
2022
this._client = client;
2123
}
24+
2225
public IDisposable Subscribe(ReindexObserver<T> observer)
2326
{
2427
this.Alter = observer.Alter;
@@ -46,7 +49,8 @@ private void Reindex(IObserver<IReindexResponse<T>> observer)
4649
var toIndex = this._reindexRequest.To.Resolve(this._connectionSettings);
4750
toIndex.ThrowIfNullOrEmpty(nameof(toIndex));
4851

49-
this.CreateIndex(fromIndex, toIndex);
52+
if (!this._reindexRequest.OmitCreateIndex)
53+
this.CreateIndex(fromIndex, toIndex);
5054

5155
var scroll = this._reindexRequest.Scroll ?? TimeSpan.FromMinutes(2);
5256

@@ -87,19 +91,19 @@ private void ScrollToCompletion(IObserver<IReindexResponse<T>> observer, string
8791
private ISearchResponse<T> InitiateSearch(string fromIndex, string toIndex, Time scroll)
8892
{
8993
var size = this._reindexRequest.Size ?? 100;
90-
var searchResult = this._client.Search<T>(new SearchRequest<T>(fromIndex, this._reindexRequest.Type)
94+
var searchResult = this._client.Search<T>(new SearchRequest<T>(fromIndex, this._reindexRequest.Types)
9195
{
9296
From = 0,
9397
Size = size,
9498
Query = this._reindexRequest.Query,
95-
Scroll = scroll
99+
Scroll = scroll,
100+
Sort = DocOrderSort
96101
});
97102
if (searchResult.Total <= 0)
98103
throw Throw($"Source index {fromIndex} doesn't contain any documents.", searchResult.ApiCall);
99104
return searchResult;
100105
}
101106

102-
103107
private void CreateIndex(string resolvedFrom, string resolvedTo)
104108
{
105109
var originalIndexSettings = this._client.GetIndex(resolvedFrom);

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

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,104 @@ public interface IReindexRequest
66
{
77
IndexName To { get; set; }
88
IndexName From { get; set; }
9-
Types Type { get; set; }
9+
Types Types { get; set; }
10+
/// <summary>
11+
/// A search request can be scrolled by specifying the scroll parameter. The scroll parameter is a time value
12+
/// parameter (for example: scroll=5m), indicating for how long the nodes that participate in the search
13+
/// will maintain relevant resources in order to continue and support it. This is very similar in its idea to opening a cursor against a database.
14+
/// </summary>
1015
Time Scroll { get; set; }
16+
17+
/// <summary>
18+
/// The number of hits to return. Defaults to 100. When using scroll search type,
19+
/// size is actually multiplied by the number of shards!
20+
/// </summary>
1121
int? Size { get; set; }
1222

23+
/// <summary>
24+
/// A query to optionally limit the documents to use for the reindex operation.
25+
/// </summary>
1326
QueryContainer Query { get; set; }
1427

15-
ICreateIndexRequest CreateIndexRequest { get; set; }
28+
/// <summary>
29+
/// Do not send a create index call on <see cref="To"/>, assume the index has been created outside of the reindex.
30+
/// </summary>
31+
bool OmitIndexCreation { get; set; }
1632

17-
IPutMappingRequest PutMappingRequest { get; set; }
33+
/// <summary>
34+
/// Describe how the newly created index should be created. Remember you can also register Index Templates for more dynamic usecases.
35+
/// </summary>
36+
ICreateIndexRequest CreateIndexRequest { get; set; }
1837
}
1938

2039
public class ReindexRequest : IReindexRequest
2140
{
41+
/// <inheritdoc/>
42+
public bool OmitCreateIndex { get; set; }
43+
/// <inheritdoc/>
2244
public IndexName To { get; set; }
45+
/// <inheritdoc/>
2346
public IndexName From { get; set; }
24-
public Types Type { get; set; }
47+
/// <inheritdoc/>
48+
public Types Types { get; set; }
49+
/// <inheritdoc/>
2550
public Time Scroll { get; set; }
51+
/// <inheritdoc/>
2652
public int? Size { get; set; }
53+
/// <inheritdoc/>
2754
public QueryContainer Query { get; set; }
55+
/// <inheritdoc/>
2856
public ICreateIndexRequest CreateIndexRequest { get; set; }
29-
public IPutMappingRequest PutMappingRequest { get; set; }
30-
public ReindexRequest(IndexName from, IndexName to, Types type)
57+
58+
public ReindexRequest(IndexName from, IndexName to, Types types)
3159
{
3260
this.To = to;
3361
this.From = from;
34-
this.Type = type;
62+
this.Types = types;
3563
}
3664
}
3765

3866
public class ReindexDescriptor<T> : DescriptorBase<ReindexDescriptor<T>, IReindexRequest>, IReindexRequest where T : class
3967
{
68+
bool IReindexRequest.OmitIndexCreation { get; set; }
4069
IndexName IReindexRequest.To { get; set; }
4170
IndexName IReindexRequest.From { get; set; }
42-
Types IReindexRequest.Type { get; set; }
71+
Types IReindexRequest.Types { get; set; }
4372
Time IReindexRequest.Scroll { get; set; }
4473
int? IReindexRequest.Size { get; set; }
4574
QueryContainer IReindexRequest.Query { get; set; }
4675
ICreateIndexRequest IReindexRequest.CreateIndexRequest { get; set; }
47-
IPutMappingRequest IReindexRequest.PutMappingRequest { get; set; }
4876

4977
public ReindexDescriptor(IndexName from, IndexName to)
5078
{
5179
Assign(a => a.From = from)
5280
.Assign(a => a.To = to)
53-
.Assign(a => a.Type = typeof(T));
81+
.Assign(a => a.Types = typeof(T));
5482
}
5583

56-
/// <summary>
57-
/// A search request can be scrolled by specifying the scroll parameter. The scroll parameter is a time value parameter (for example: scroll=5m), indicating for how long the nodes that participate in the search will maintain relevant resources in order to continue and support it. This is very similar in its idea to opening a cursor against a database.
58-
/// </summary>
59-
/// <param name="scrollTime">The scroll parameter is a time value parameter (for example: scroll=5m)</param>
60-
/// <returns></returns>
84+
/// <inheritdoc/>
6185
public ReindexDescriptor<T> Scroll(Time scrollTime) => Assign(a => a.Scroll = scrollTime);
6286

63-
/// <summary>
64-
/// The number of hits to return. Defaults to 100. When using scroll search type,
65-
/// size is actually multiplied by the number of shards!
66-
/// </summary>
87+
/// <inheritdoc/>
6788
public ReindexDescriptor<T> Size(int? size) => Assign(a => a.Size = size);
6889

69-
/// <summary>
70-
/// The number of hits to return. Defaults to 100.
71-
/// </summary>
90+
/// <inheritdoc/>
7291
public ReindexDescriptor<T> Take(int? take) => Assign(a => a.Size = take);
7392

74-
/// <summary>
75-
/// A query to optionally limit the documents to use for the reindex operation.
76-
/// </summary>
93+
/// <inheritdoc/>
94+
public ReindexDescriptor<T> OmitIndexCreation(bool omit = true) => Assign(a => a.OmitIndexCreation = true);
95+
96+
/// <inheritdoc/>
7797
public ReindexDescriptor<T> Query(Func<QueryContainerDescriptor<T>, QueryContainer> querySelector) =>
7898
Assign(a => a.Query = querySelector?.Invoke(new QueryContainerDescriptor<T>()));
7999

80-
/// <summary>
81-
/// A query to optionally limit the documents to use for the reindex operation.
82-
/// </summary>
100+
/// <inheritdoc/>
83101
public ReindexDescriptor<T> Query(QueryContainer query) => Assign(a => a.Query = query);
84102

85103
/// <summary>
86104
/// Specify the document types to reindex. By default, will be <typeparamref name="T"/>
87105
/// </summary>
88-
public ReindexDescriptor<T> Type(Types type) => Assign(a => a.Type = type);
106+
public ReindexDescriptor<T> Type(Types type) => Assign(a => a.Types = type);
89107

90108
/// <summary>
91109
/// Reindex all document types.

0 commit comments

Comments
 (0)