Skip to content

Commit b7df510

Browse files
committed
Add ability to specify fields from _source to reindex (#3290)
This commit adds the ability to specify the fields from source to reindex when using the Reindex API (ReindexOnServer in NEST). Closes #3288 (cherry picked from commit 4753f77)
1 parent 6418efe commit b7df510

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

src/Nest/Document/Multiple/ReindexOnServer/ReindexSource.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public interface IReindexSource
2323

2424
[JsonProperty("remote")]
2525
IRemoteSource Remote { get; set; }
26+
27+
/// <summary>
28+
/// Individual fields from _source to reindex
29+
/// </summary>
30+
[JsonProperty("_source")]
31+
Fields Source { get; set; }
2632
}
2733

2834
public class ReindexSource : IReindexSource
@@ -38,6 +44,9 @@ public class ReindexSource : IReindexSource
3844
public int? Size { get; set; }
3945

4046
public IRemoteSource Remote { get; set; }
47+
48+
/// <inheritdoc />
49+
public Fields Source { get; set; }
4150
}
4251

4352
public class ReindexSourceDescriptor : DescriptorBase<ReindexSourceDescriptor, IReindexSource>, IReindexSource
@@ -48,6 +57,7 @@ public class ReindexSourceDescriptor : DescriptorBase<ReindexSourceDescriptor, I
4857
Types IReindexSource.Type { get; set; }
4958
int? IReindexSource.Size { get; set; }
5059
IRemoteSource IReindexSource.Remote { get; set; }
60+
Fields IReindexSource.Source { get; set; }
5161

5262
public ReindexSourceDescriptor Query<T>(Func<QueryContainerDescriptor<T>, QueryContainer> querySelector) where T : class =>
5363
Assign(a => a.Query = querySelector?.Invoke(new QueryContainerDescriptor<T>()));
@@ -63,5 +73,9 @@ public ReindexSourceDescriptor Remote(Func<RemoteSourceDescriptor, IRemoteSource
6373
public ReindexSourceDescriptor Type(Types types) => Assign(a => a.Type = types);
6474

6575
public ReindexSourceDescriptor Size(int? size) => Assign(a => a.Size = size);
76+
77+
/// <inheritdoc cref="IReindexSource.Source"/>
78+
public ReindexSourceDescriptor Source<T>(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) where T : class =>
79+
Assign(a => a.Source = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
6680
}
6781
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using Elasticsearch.Net;
3+
using Nest;
4+
using Tests.Framework;
5+
using Tests.Framework.Integration;
6+
using Tests.Framework.ManagedElasticsearch.Clusters;
7+
using static Nest.Infer;
8+
9+
namespace Tests.Document.Multiple.ReindexOnServer
10+
{
11+
public class ReindexOnServerSourceApiTests : ApiIntegrationTestBase<IntrusiveOperationCluster, IReindexOnServerResponse, IReindexOnServerRequest, ReindexOnServerDescriptor, ReindexOnServerRequest>
12+
{
13+
public class Test
14+
{
15+
public long Id { get; set; }
16+
public string Flag { get; set; }
17+
}
18+
19+
public ReindexOnServerSourceApiTests(IntrusiveOperationCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
20+
21+
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
22+
{
23+
foreach (var index in values.Values)
24+
{
25+
this.Client.Bulk(b => b
26+
.Index(index)
27+
.IndexMany(new []
28+
{
29+
new Test { Id = 1, Flag = "bar" },
30+
new Test { Id = 2, Flag = "bar" }
31+
})
32+
.Refresh(Refresh.WaitFor)
33+
);
34+
}
35+
}
36+
protected override LazyResponses ClientUsage() => Calls(
37+
fluent: (client, f) => client.ReindexOnServer(f),
38+
fluentAsync: (client, f) => client.ReindexOnServerAsync(f),
39+
request: (client, r) => client.ReindexOnServer(r),
40+
requestAsync: (client, r) => client.ReindexOnServerAsync(r)
41+
);
42+
43+
protected override bool ExpectIsValid => true;
44+
protected override int ExpectStatusCode => 200;
45+
protected override HttpMethod HttpMethod => HttpMethod.POST;
46+
47+
protected override string UrlPath => $"/_reindex?refresh=true";
48+
49+
protected override bool SupportsDeserialization => false;
50+
51+
protected override Func<ReindexOnServerDescriptor, IReindexOnServerRequest> Fluent => d => d
52+
.Source(s => s
53+
.Index(CallIsolatedValue)
54+
.Type("test")
55+
.Source<Test>(f => f
56+
.Field(ff => ff.Id)
57+
.Field(ff => ff.Flag)
58+
)
59+
)
60+
.Destination(s => s
61+
.Index(CallIsolatedValue + "-clone")
62+
.Type("test")
63+
)
64+
.Conflicts(Conflicts.Proceed)
65+
.Refresh();
66+
67+
protected override ReindexOnServerRequest Initializer => new ReindexOnServerRequest
68+
{
69+
Source = new ReindexSource
70+
{
71+
Index = CallIsolatedValue,
72+
Type = "test",
73+
Source = Infer.Fields<Test>(
74+
ff => ff.Id,
75+
ff => ff.Flag
76+
)
77+
},
78+
Destination = new ReindexDestination
79+
{
80+
Index = CallIsolatedValue + "-clone",
81+
Type = Type<Test>(),
82+
},
83+
Conflicts = Conflicts.Proceed,
84+
Refresh = true,
85+
};
86+
87+
protected override void ExpectResponse(IReindexOnServerResponse response)
88+
{
89+
response.ShouldBeValid();
90+
}
91+
92+
protected override object ExpectJson =>
93+
new
94+
{
95+
dest = new
96+
{
97+
index = $"{CallIsolatedValue}-clone",
98+
type = "test",
99+
},
100+
source = new
101+
{
102+
index = CallIsolatedValue,
103+
_source = new [] { "id", "flag" },
104+
type = new[] { "test" },
105+
},
106+
conflicts = "proceed"
107+
};
108+
}
109+
}

0 commit comments

Comments
 (0)