Skip to content

Commit 4c783bc

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 5dec33d commit 4c783bc

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
@@ -57,6 +57,12 @@ public interface IReindexSource
5757
/// </remarks>
5858
[JsonProperty("slice")]
5959
ISlicedScroll Slice { get; set; }
60+
61+
/// <summary>
62+
/// Individual fields from _source to reindex
63+
/// </summary>
64+
[JsonProperty("_source")]
65+
Fields Source { get; set; }
6066
}
6167

6268
/// <inheritdoc />
@@ -82,6 +88,9 @@ public class ReindexSource : IReindexSource
8288

8389
/// <inheritdoc />
8490
public ISlicedScroll Slice { get; set; }
91+
92+
/// <inheritdoc />
93+
public Fields Source { get; set; }
8594
}
8695

8796
/// <inheritdoc cref="IReindexSource"/>
@@ -94,6 +103,7 @@ public class ReindexSourceDescriptor : DescriptorBase<ReindexSourceDescriptor, I
94103
int? IReindexSource.Size { get; set; }
95104
IRemoteSource IReindexSource.Remote { get; set; }
96105
ISlicedScroll IReindexSource.Slice { get; set; }
106+
Fields IReindexSource.Source { get; set; }
97107

98108
/// <inheritdoc cref="IReindexSource.Query"/>
99109
public ReindexSourceDescriptor Query<T>(Func<QueryContainerDescriptor<T>, QueryContainer> querySelector) where T : class =>
@@ -119,5 +129,9 @@ public ReindexSourceDescriptor Remote(Func<RemoteSourceDescriptor, IRemoteSource
119129
/// <inheritdoc cref="IReindexSource.Slice"/>
120130
public ReindexSourceDescriptor Slice<T>(Func<SlicedScrollDescriptor<T>, ISlicedScroll> selector) where T : class =>
121131
Assign(a => a.Slice = selector?.Invoke(new SlicedScrollDescriptor<T>()));
132+
133+
/// <inheritdoc cref="IReindexSource.Source"/>
134+
public ReindexSourceDescriptor Source<T>(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) where T : class =>
135+
Assign(a => a.Source = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
122136
}
123137
}
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)