Skip to content

Commit 522115b

Browse files
committed
Merge branch 'feature/reindex-types'
2 parents 57d7273 + b4f45d1 commit 522115b

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

Diff for: src/Nest/Document/Multiple/Reindex/ReindexObservable.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private void Reindex(IObserver<IReindexResponse<T>> observer)
5454
var searchResult = this._client.Search<T>(
5555
s => s
5656
.Index(fromIndex)
57-
.Type(Types.All)
57+
.Type(this._reindexRequest.Type)
5858
.From(0)
5959
.Size(size)
6060
.Query(q=>this._reindexRequest.Query)

Diff for: src/Nest/Document/Multiple/Reindex/ReindexRequest.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public interface IReindexRequest
66
{
77
IndexName To { get; set; }
88
IndexName From { get; set; }
9+
Types Type { get; set; }
910
Time Scroll { get; set; }
1011
int? Size { get; set; }
1112

@@ -15,26 +16,30 @@ public interface IReindexRequest
1516

1617
IPutMappingRequest PutMappingRequest { get; set; }
1718
}
19+
1820
public class ReindexRequest : IReindexRequest
1921
{
2022
public IndexName To { get; set; }
2123
public IndexName From { get; set; }
24+
public Types Type { get; set; }
2225
public Time Scroll { get; set; }
2326
public int? Size { get; set; }
2427
public QueryContainer Query { get; set; }
2528
public ICreateIndexRequest CreateIndexRequest { get; set; }
2629
public IPutMappingRequest PutMappingRequest { get; set; }
27-
public ReindexRequest(IndexName from, IndexName to)
30+
public ReindexRequest(IndexName from, IndexName to, Types type)
2831
{
2932
this.To = to;
3033
this.From = from;
34+
this.Type = type;
3135
}
3236
}
3337

3438
public class ReindexDescriptor<T> : DescriptorBase<ReindexDescriptor<T>, IReindexRequest>, IReindexRequest where T : class
3539
{
3640
IndexName IReindexRequest.To { get; set; }
3741
IndexName IReindexRequest.From { get; set; }
42+
Types IReindexRequest.Type { get; set; }
3843
Time IReindexRequest.Scroll { get; set; }
3944
int? IReindexRequest.Size { get; set; }
4045
QueryContainer IReindexRequest.Query { get; set; }
@@ -43,7 +48,9 @@ public class ReindexDescriptor<T> : DescriptorBase<ReindexDescriptor<T>, IReinde
4348

4449
public ReindexDescriptor(IndexName from, IndexName to)
4550
{
46-
Assign(a => a.From = from).Assign(a => a.To = to);
51+
Assign(a => a.From = from)
52+
.Assign(a => a.To = to)
53+
.Assign(a => a.Type = typeof(T));
4754
}
4855

4956
/// <summary>
@@ -75,6 +82,16 @@ public ReindexDescriptor<T> Query(Func<QueryContainerDescriptor<T>, QueryContain
7582
/// </summary>
7683
public ReindexDescriptor<T> Query(QueryContainer query) => Assign(a => a.Query = query);
7784

85+
/// <summary>
86+
/// Specify the document types to reindex. By default, will be <typeparamref name="T"/>
87+
/// </summary>
88+
public ReindexDescriptor<T> Type(Types type) => Assign(a => a.Type = type);
89+
90+
/// <summary>
91+
/// Reindex all document types.
92+
/// </summary>
93+
public ReindexDescriptor<T> AllTypes() => this.Type(Types.AllTypes);
94+
7895
/// <summary>
7996
/// CreateIndex selector, will be passed the a descriptor initialized with the settings from
8097
/// the index we're reindexing from

Diff for: src/Nest/Document/Multiple/Reindex/ReindexResponse.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@ public class ReindexResponse<T> : IReindexResponse<T> where T : class
3939

4040
public int Scroll { get; internal set; }
4141

42-
public bool IsValid
43-
{
44-
get
45-
{
46-
return (this.BulkResponse != null && this.BulkResponse.IsValid
47-
&& this.SearchResponse != null && this.SearchResponse.IsValid);
48-
}
49-
}
42+
public bool IsValid =>
43+
this.BulkResponse != null && this.BulkResponse.IsValid
44+
&& this.SearchResponse != null && this.SearchResponse.IsValid;
5045
}
5146
}

Diff for: src/Tests/Document/Multiple/Reindex/ReindexApiTests.cs

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System;
22
using System.Linq;
33
using System.Threading;
4+
using Elasticsearch.Net;
45
using FluentAssertions;
56
using Nest;
67
using Tests.Framework;
78
using Tests.Framework.Integration;
89
using Tests.Framework.MockData;
910
using Xunit;
1011
using static Nest.Infer;
11-
using Elasticsearch.Net;
1212

1313
namespace Tests.Document.Multiple.Reindex
1414
{
@@ -26,10 +26,13 @@ public override void Boostrap()
2626
[Collection(IntegrationContext.Reindex)]
2727
public class ReindexApiTests : SerializationTestBase
2828
{
29-
private readonly IObservable<IReindexResponse<ILazyDocument>> _reindexResult;
29+
private readonly IObservable<IReindexResponse<ILazyDocument>> _reindexManyTypesResult;
30+
private readonly IObservable<IReindexResponse<Project>> _reindexSingleTypeResult;
3031
private readonly IElasticClient _client;
3132

32-
private static string NewIndexName { get; } = $"project-copy-{Guid.NewGuid().ToString("N").Substring(8)}";
33+
private static string NewManyTypesIndexName { get; } = $"project-copy-{Guid.NewGuid().ToString("N").Substring(8)}";
34+
35+
private static string NewSingleTypeIndexName { get; } = $"project-copy-{Guid.NewGuid().ToString("N").Substring(8)}";
3336

3437
private static string IndexName { get; } = "project";
3538

@@ -64,27 +67,33 @@ public ReindexApiTests(ReindexCluster cluster, EndpointUsage usage)
6467
bulkResult.IsValid.Should().BeTrue();
6568

6669
this._client.Refresh(IndexName);
67-
this._reindexResult = this._client.Reindex<ILazyDocument>(IndexName, NewIndexName, r=>r);
70+
71+
this._reindexManyTypesResult = this._client.Reindex<ILazyDocument>(IndexName, NewManyTypesIndexName, r => r.AllTypes());
72+
this._reindexSingleTypeResult = this._client.Reindex<Project>(IndexName, NewSingleTypeIndexName);
6873
}
6974

7075
[I] public void ReturnsExpectedResponse()
7176
{
72-
var handle = new ManualResetEvent(false);
73-
var observer = new ReindexObserver<ILazyDocument>(
77+
var handles = new[]
78+
{
79+
new ManualResetEvent(false),
80+
new ManualResetEvent(false)
81+
};
82+
83+
var manyTypesObserver = new ReindexObserver<ILazyDocument>(
7484
onError: (e) => { throw e; },
7585
completed: () =>
7686
{
77-
var refresh = this._client.Refresh(NewIndexName);
78-
87+
var refresh = this._client.Refresh(NewManyTypesIndexName);
7988
var originalIndexCount = this._client.Count<CommitActivity>(c => c.Index(IndexName));
80-
var newIndexCount = this._client.Count<CommitActivity>(c => c.Index(NewIndexName));
89+
var newIndexCount = this._client.Count<CommitActivity>(c => c.Index(NewManyTypesIndexName));
8190

8291
originalIndexCount.Count.Should().BeGreaterThan(0).And.Be(newIndexCount.Count);
8392

8493
var scroll = "20s";
8594

8695
var searchResult = this._client.Search<CommitActivity>(s => s
87-
.Index(NewIndexName)
96+
.Index(NewManyTypesIndexName)
8897
.From(0)
8998
.Size(100)
9099
.Query(q => q.MatchAll())
@@ -103,12 +112,31 @@ [I] public void ReturnsExpectedResponse()
103112
hit.Routing.Should().NotBeNullOrEmpty();
104113
}
105114
} while (searchResult.IsValid && searchResult.Documents.Any());
106-
handle.Set();
115+
handles[0].Set();
107116
}
108117
);
109118

110-
this._reindexResult.Subscribe(observer);
111-
handle.WaitOne(TimeSpan.FromMinutes(3));
119+
this._reindexManyTypesResult.Subscribe(manyTypesObserver);
120+
121+
var singleTypeObserver = new ReindexObserver<Project>(
122+
onError: (e) => { throw e; },
123+
completed: () =>
124+
{
125+
var refresh = this._client.Refresh(NewSingleTypeIndexName);
126+
var originalIndexCount = this._client.Count<Project>(c => c.Index(IndexName));
127+
128+
// new index should only contain project document types
129+
var newIndexCount = this._client.Count<Project>(c => c.Index(NewSingleTypeIndexName).AllTypes());
130+
131+
originalIndexCount.Count.Should().BeGreaterThan(0).And.Be(newIndexCount.Count);
132+
133+
handles[1].Set();
134+
}
135+
);
136+
137+
this._reindexSingleTypeResult.Subscribe(singleTypeObserver);
138+
139+
WaitHandle.WaitAll(handles, TimeSpan.FromMinutes(3));
112140
}
113141
}
114142
}

0 commit comments

Comments
 (0)