diff --git a/src/Nest/Document/Multiple/Reindex/ReindexObservable.cs b/src/Nest/Document/Multiple/Reindex/ReindexObservable.cs index 5a100cc9b7e..2f5ff4a42ea 100644 --- a/src/Nest/Document/Multiple/Reindex/ReindexObservable.cs +++ b/src/Nest/Document/Multiple/Reindex/ReindexObservable.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Elasticsearch.Net; @@ -10,6 +11,7 @@ public class ReindexObservable : IDisposable, IObservable private readonly IReindexRequest _reindexRequest; private readonly IConnectionSettingsValues _connectionSettings; private readonly IElasticClient _client; + private static IList DocOrderSort = new ReadOnlyCollection(new List { new SortField { Field = "_doc" } }); private Action, T, IBulkIndexOperation> Alter { get; set; } @@ -19,6 +21,7 @@ public ReindexObservable(IElasticClient client, IConnectionSettingsValues connec this._reindexRequest = reindexRequest; this._client = client; } + public IDisposable Subscribe(ReindexObserver observer) { this.Alter = observer.Alter; @@ -46,7 +49,8 @@ private void Reindex(IObserver> observer) var toIndex = this._reindexRequest.To.Resolve(this._connectionSettings); toIndex.ThrowIfNullOrEmpty(nameof(toIndex)); - this.CreateIndex(fromIndex, toIndex); + if (!this._reindexRequest.OmitCreateIndex) + this.CreateIndex(fromIndex, toIndex); var scroll = this._reindexRequest.Scroll ?? TimeSpan.FromMinutes(2); @@ -87,19 +91,19 @@ private void ScrollToCompletion(IObserver> observer, string private ISearchResponse InitiateSearch(string fromIndex, string toIndex, Time scroll) { var size = this._reindexRequest.Size ?? 100; - var searchResult = this._client.Search(new SearchRequest(fromIndex, this._reindexRequest.Type) + var searchResult = this._client.Search(new SearchRequest(fromIndex, this._reindexRequest.Types) { From = 0, Size = size, Query = this._reindexRequest.Query, - Scroll = scroll + Scroll = scroll, + Sort = DocOrderSort }); if (searchResult.Total <= 0) throw Throw($"Source index {fromIndex} doesn't contain any documents.", searchResult.ApiCall); return searchResult; } - private void CreateIndex(string resolvedFrom, string resolvedTo) { var originalIndexSettings = this._client.GetIndex(resolvedFrom); diff --git a/src/Nest/Document/Multiple/Reindex/ReindexRequest.cs b/src/Nest/Document/Multiple/Reindex/ReindexRequest.cs index 72a12d19682..77c96c018c3 100644 --- a/src/Nest/Document/Multiple/Reindex/ReindexRequest.cs +++ b/src/Nest/Document/Multiple/Reindex/ReindexRequest.cs @@ -6,86 +6,104 @@ public interface IReindexRequest { IndexName To { get; set; } IndexName From { get; set; } - Types Type { get; set; } + Types Types { get; set; } + /// + /// 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. + /// Time Scroll { get; set; } + + /// + /// The number of hits to return. Defaults to 100. When using scroll search type, + /// size is actually multiplied by the number of shards! + /// int? Size { get; set; } + /// + /// A query to optionally limit the documents to use for the reindex operation. + /// QueryContainer Query { get; set; } - ICreateIndexRequest CreateIndexRequest { get; set; } + /// + /// Do not send a create index call on , assume the index has been created outside of the reindex. + /// + bool OmitIndexCreation { get; set; } - IPutMappingRequest PutMappingRequest { get; set; } + /// + /// Describe how the newly created index should be created. Remember you can also register Index Templates for more dynamic usecases. + /// + ICreateIndexRequest CreateIndexRequest { get; set; } } public class ReindexRequest : IReindexRequest { + /// + public bool OmitCreateIndex { get; set; } + /// public IndexName To { get; set; } + /// public IndexName From { get; set; } - public Types Type { get; set; } + /// + public Types Types { get; set; } + /// public Time Scroll { get; set; } + /// public int? Size { get; set; } + /// public QueryContainer Query { get; set; } + /// public ICreateIndexRequest CreateIndexRequest { get; set; } - public IPutMappingRequest PutMappingRequest { get; set; } - public ReindexRequest(IndexName from, IndexName to, Types type) + + public ReindexRequest(IndexName from, IndexName to, Types types) { this.To = to; this.From = from; - this.Type = type; + this.Types = types; } } public class ReindexDescriptor : DescriptorBase, IReindexRequest>, IReindexRequest where T : class { + bool IReindexRequest.OmitIndexCreation { get; set; } IndexName IReindexRequest.To { get; set; } IndexName IReindexRequest.From { get; set; } - Types IReindexRequest.Type { get; set; } + Types IReindexRequest.Types { get; set; } Time IReindexRequest.Scroll { get; set; } int? IReindexRequest.Size { get; set; } QueryContainer IReindexRequest.Query { get; set; } ICreateIndexRequest IReindexRequest.CreateIndexRequest { get; set; } - IPutMappingRequest IReindexRequest.PutMappingRequest { get; set; } public ReindexDescriptor(IndexName from, IndexName to) { Assign(a => a.From = from) .Assign(a => a.To = to) - .Assign(a => a.Type = typeof(T)); + .Assign(a => a.Types = typeof(T)); } - /// - /// 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. - /// - /// The scroll parameter is a time value parameter (for example: scroll=5m) - /// + /// public ReindexDescriptor Scroll(Time scrollTime) => Assign(a => a.Scroll = scrollTime); - /// - /// The number of hits to return. Defaults to 100. When using scroll search type, - /// size is actually multiplied by the number of shards! - /// + /// public ReindexDescriptor Size(int? size) => Assign(a => a.Size = size); - /// - /// The number of hits to return. Defaults to 100. - /// + /// public ReindexDescriptor Take(int? take) => Assign(a => a.Size = take); - /// - /// A query to optionally limit the documents to use for the reindex operation. - /// + /// + public ReindexDescriptor OmitIndexCreation(bool omit = true) => Assign(a => a.OmitIndexCreation = true); + + /// public ReindexDescriptor Query(Func, QueryContainer> querySelector) => Assign(a => a.Query = querySelector?.Invoke(new QueryContainerDescriptor())); - /// - /// A query to optionally limit the documents to use for the reindex operation. - /// + /// public ReindexDescriptor Query(QueryContainer query) => Assign(a => a.Query = query); /// /// Specify the document types to reindex. By default, will be /// - public ReindexDescriptor Type(Types type) => Assign(a => a.Type = type); + public ReindexDescriptor Type(Types type) => Assign(a => a.Types = type); /// /// Reindex all document types.