Skip to content

Commit 9319f42

Browse files
committed
When reindexing, use the _parent, _routing and _timestamp from the existing document
Updated Reindex integration test to assert these are copied across Fixes #985 Fixes #1763
1 parent 57bb293 commit 9319f42

File tree

9 files changed

+143
-60
lines changed

9 files changed

+143
-60
lines changed

Diff for: src/Benchmarking/project.lock.json

+3
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@
508508
"frameworkAssemblies": [
509509
"System.Diagnostics.Tools",
510510
"System.IO.Compression.FileSystem",
511+
"System.Linq",
511512
"System.Reflection",
512513
"System.Runtime",
513514
"System.Threading.Tasks"
@@ -2716,6 +2717,7 @@
27162717
"frameworkAssemblies": [
27172718
"System.Diagnostics.Tools",
27182719
"System.IO.Compression.FileSystem",
2720+
"System.Linq",
27192721
"System.Reflection",
27202722
"System.Runtime",
27212723
"System.Threading.Tasks"
@@ -3337,6 +3339,7 @@
33373339
"frameworkAssemblies": [
33383340
"System.Diagnostics.Tools",
33393341
"System.IO.Compression.FileSystem",
3342+
"System.Linq",
33403343
"System.Reflection",
33413344
"System.Runtime",
33423345
"System.Threading.Tasks"

Diff for: src/Elasticsearch.Net/project.lock.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3109,9 +3109,9 @@
31093109
"projectFileDependencyGroups": {
31103110
"": [],
31113111
".NETFramework,Version=v4.5": [
3112+
"fx/System ",
31123113
"fx/System.Runtime ",
3113-
"fx/System.Runtime.Serialization ",
3114-
"fx/System "
3114+
"fx/System.Runtime.Serialization "
31153115
],
31163116
".NETPlatform,Version=v5.1": [
31173117
"Microsoft.CSharp >= 4.0.1-beta-23225",

Diff for: src/Nest/Document/Multiple/Bulk/BulkOperation/BulkOperationBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public abstract class BulkOperationDescriptorBase<TDescriptor, TInterface>
8989

9090
public TDescriptor Parent(Id parent) => Assign(a => a.Parent = parent);
9191

92-
public TDescriptor Timestamp(long timestamp) => Assign(a => a.Timestamp = timestamp);
92+
public TDescriptor Timestamp(long? timestamp) => Assign(a => a.Timestamp = timestamp);
9393

9494
public TDescriptor Ttl(Time ttl) => Assign(a => a.Ttl = ttl);
9595
}

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

+17-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public IDisposable Subscribe(IObserver<IReindexResponse<T>> observer)
2929
observer.OnError(e);
3030
}
3131
return this;
32-
3332
}
3433

3534
private void Reindex(IObserver<IReindexResponse<T>> observer)
@@ -44,7 +43,7 @@ private void Reindex(IObserver<IReindexResponse<T>> observer)
4443
var resolvedTo = toIndex.Resolve(this._connectionSettings);
4544
resolvedTo.ThrowIfNullOrEmpty(nameof(toIndex));
4645

47-
var indexSettings = this._client.GetIndexSettings(i=>i.Index(fromIndex));
46+
var indexSettings = this._client.GetIndex(fromIndex);
4847
Func<CreateIndexDescriptor, ICreateIndexRequest> settings = (ci) => this._reindexRequest.CreateIndexRequest ?? ci;
4948
var createIndexResponse = this._client.CreateIndex(
5049
toIndex, (c) => settings(c.InitializeUsing(indexSettings.Indices[resolvedFrom])));
@@ -88,7 +87,22 @@ public IBulkResponse IndexSearchResults(ISearchResponse<T> searchResult,IObserve
8887
foreach (var d in searchResult.Hits)
8988
{
9089
IHit<T> d1 = d;
91-
bb.Index<T>(bi => bi.Document(d1.Source).Type(d1.Type).Index(toIndex).Id(d.Id));
90+
bb.Index<T>(bi =>
91+
{
92+
var bid = bi
93+
.Document(d1.Source)
94+
.Type(d1.Type)
95+
.Index(toIndex)
96+
.Id(d1.Id)
97+
.Routing(d1.Routing)
98+
.Timestamp(d1.Timestamp);
99+
100+
if (d1.Parent != null)
101+
{
102+
bid.Parent(d1.Parent);
103+
}
104+
return bid;
105+
});
92106
}
93107

94108
var indexResult = this._client.Bulk(b=>bb);
@@ -104,7 +118,6 @@ public IBulkResponse IndexSearchResults(ISearchResponse<T> searchResult,IObserve
104118
return indexResult;
105119
}
106120

107-
108121
public void Dispose()
109122
{
110123

Diff for: src/Nest/Search/Search/Hits/Hit.cs

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public interface IHit<out T> where T : class
1616
string Id { get; }
1717
string Parent { get; }
1818
string Routing { get; }
19+
long? Timestamp { get; }
1920
IEnumerable<object> Sorts { get; }
2021
HighlightFieldDictionary Highlights { get; }
2122
Explanation Explanation { get; }
@@ -58,6 +59,9 @@ public class Hit<T> : IHit<T>
5859
[JsonProperty(PropertyName = "_routing")]
5960
public string Routing { get; internal set; }
6061

62+
[JsonProperty(PropertyName = "_timestamp")]
63+
public long? Timestamp { get; internal set; }
64+
6165
[JsonProperty(PropertyName = "sort")]
6266
public IEnumerable<object> Sorts { get; internal set; }
6367

Diff for: src/Nest/project.lock.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -3441,20 +3441,20 @@
34413441
"Newtonsoft.Json >= 8.0.2"
34423442
],
34433443
".NETFramework,Version=v4.5": [
3444+
"fx/System.Dynamic.Runtime ",
34443445
"fx/System.Runtime ",
34453446
"fx/System.Runtime.Serialization ",
3446-
"fx/System.ServiceModel ",
3447-
"fx/System.Dynamic.Runtime "
3447+
"fx/System.ServiceModel "
34483448
],
34493449
".NETPlatform,Version=v5.1": [
3450-
"System.Runtime >= 4.0.21-beta-23225",
3451-
"System.Linq >= 4.0.0-beta-23109",
3452-
"System.Linq.Queryable >= 4.0.0-beta-23109",
34533450
"System.Collections >= 4.0.11-beta-23225",
3451+
"System.Diagnostics.Tools >= 4.0.1-beta-23225",
34543452
"System.Dynamic.Runtime >= 4.0.11-beta-23516",
3453+
"System.Linq >= 4.0.0-beta-23109",
3454+
"System.Linq.Queryable >= 4.0.0-beta-23109",
3455+
"System.Runtime >= 4.0.21-beta-23225",
34553456
"System.Threading >= 4.0.11-beta-23225",
3456-
"System.Threading.Timer >= 4.0.0-beta-23109",
3457-
"System.Diagnostics.Tools >= 4.0.1-beta-23225"
3457+
"System.Threading.Timer >= 4.0.0-beta-23109"
34583458
]
34593459
}
34603460
}
+68-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Threading;
34
using FluentAssertions;
45
using Nest;
@@ -7,26 +8,63 @@
78
using Tests.Framework.MockData;
89
using Xunit;
910
using static Nest.Infer;
11+
using Elasticsearch.Net;
1012

1113
namespace Tests.Document.Multiple.Reindex
1214
{
1315
[CollectionDefinition(IntegrationContext.Reindex)]
14-
public class ReindexCluster : ClusterBase, ICollectionFixture<ReindexCluster>, IClassFixture<EndpointUsage> { }
16+
public class ReindexCluster : ClusterBase, ICollectionFixture<ReindexCluster>, IClassFixture<EndpointUsage>
17+
{
18+
public override void Boostrap()
19+
{
20+
var seeder = new Seeder(this.Node);
21+
seeder.DeleteIndicesAndTemplates();
22+
seeder.CreateIndices();
23+
}
24+
}
1525

1626
[Collection(IntegrationContext.Reindex)]
1727
public class ReindexApiTests : SerializationTestBase
1828
{
1929
private readonly IObservable<IReindexResponse<ILazyDocument>> _reindexResult;
2030
private readonly IElasticClient _client;
2131

22-
private static string NewIndexName { get; } = $"dev-copy-{Guid.NewGuid().ToString("N").Substring(8)}";
32+
private static string NewIndexName { get; } = $"project-copy-{Guid.NewGuid().ToString("N").Substring(8)}";
33+
34+
private static string IndexName { get; } = "project";
2335

2436
public ReindexApiTests(ReindexCluster cluster, EndpointUsage usage)
2537
{
2638
this._client = cluster.Client();
27-
var indexResult = this._client.IndexMany(Developer.Developers);
28-
this._client.Refresh(Index<Developer>());
29-
this._reindexResult = this._client.Reindex<ILazyDocument>(Index<Developer>(), NewIndexName, r=>r);
39+
40+
// create a couple of projects
41+
var projects = Project.Generator.Generate(2).ToList();
42+
var indexProjectsResponse = this._client.IndexMany(projects, IndexName);
43+
44+
// create a thousand commits and associate with the projects
45+
var commits = CommitActivity.Generator.Generate(1000).ToList();
46+
var bb = new BulkDescriptor();
47+
for (int index = 0; index < commits.Count; index++)
48+
{
49+
var commit = commits[index];
50+
var project = index%2 == 0
51+
? projects[0].Name
52+
: projects[1].Name;
53+
54+
bb.Index<CommitActivity>(bi => bi
55+
.Document(commit)
56+
.Index(IndexName)
57+
.Id(commit.Id)
58+
.Routing(project)
59+
.Parent(project)
60+
);
61+
}
62+
63+
var bulkResult = this._client.Bulk(b => bb);
64+
bulkResult.IsValid.Should().BeTrue();
65+
66+
this._client.Refresh(IndexName);
67+
this._reindexResult = this._client.Reindex<ILazyDocument>(IndexName, NewIndexName, r=>r);
3068
}
3169

3270
[I] public void ReturnsExpectedResponse()
@@ -37,18 +75,40 @@ [I] public void ReturnsExpectedResponse()
3775
completed: () =>
3876
{
3977
var refresh = this._client.Refresh(NewIndexName);
40-
var originalIndexCount = this._client.Count<Developer>();
41-
var newIndexCount = this._client.Count<Developer>(c => c.Index(NewIndexName));
78+
79+
var originalIndexCount = this._client.Count<CommitActivity>(c => c.Index(IndexName));
80+
var newIndexCount = this._client.Count<CommitActivity>(c => c.Index(NewIndexName));
4281

4382
originalIndexCount.Count.Should().BeGreaterThan(0).And.Be(newIndexCount.Count);
4483

84+
var scroll = "20s";
85+
86+
var searchResult = this._client.Search<CommitActivity>(s => s
87+
.Index(NewIndexName)
88+
.From(0)
89+
.Size(100)
90+
.Query(q => q.MatchAll())
91+
.SearchType(SearchType.Scan)
92+
.Scroll(scroll)
93+
);
94+
95+
do
96+
{
97+
var result = searchResult;
98+
searchResult = this._client.Scroll<CommitActivity>(scroll, result.ScrollId);
99+
foreach (var hit in searchResult.Hits)
100+
{
101+
hit.Timestamp.Should().HaveValue();
102+
hit.Parent.Should().NotBeNullOrEmpty();
103+
hit.Routing.Should().NotBeNullOrEmpty();
104+
}
105+
} while (searchResult.IsValid && searchResult.Documents.Any());
45106
handle.Set();
46107
}
47108
);
48109

49110
this._reindexResult.Subscribe(observer);
50111
handle.WaitOne(TimeSpan.FromMinutes(3));
51-
//await this._reindexResult;
52112
}
53113
}
54114
}

Diff for: src/Tests/Framework/Integration/Bootstrappers/Seeder.cs

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ private void CreateProjectIndex()
113113
.Map<Project>(MapProject)
114114
.Map<CommitActivity>(m => m
115115
.Parent<Project>()
116+
.TimestampField(t => t
117+
.Enabled()
118+
)
116119
.Properties(props => props
117120
.Object<Developer>(o => o
118121
.Name(p => p.Committer)

0 commit comments

Comments
 (0)