Skip to content

Commit 40c96db

Browse files
committed
Several remain source serializer fixes
* JoinField Deserialize needs to be special case because we can not pass a string directly to JsonSerializer.Deserialize, it won't pick up Converters in that case. * Bulk and LazyDocument, did not play nicely together. * The reindex tests were not setting the routing correctly * I don't think CreateIndexRequest(name, state) should copy aliases from `state`. cc @codebrain @russcam
1 parent 643bab1 commit 40c96db

File tree

10 files changed

+30
-23
lines changed

10 files changed

+30
-23
lines changed

src/Nest/CommonAbstractions/Extensions/Extensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ internal static async Task ForEachAsync<TSource, TResult>(
244244
try
245245
{
246246
var tasks = new List<Task>();
247+
int i = 0;
247248
foreach (var item in lazyList)
248249
{
249250
tasks.Add(ProcessAsync(item, taskSelector, resultProcessor, semaphore, additionalRateLimitter, page++));
@@ -252,6 +253,7 @@ internal static async Task ForEachAsync<TSource, TResult>(
252253

253254
var task = await Task.WhenAny(tasks);
254255
tasks.Remove(task);
256+
i++;
255257
}
256258

257259
await Task.WhenAll(tasks);

src/Nest/CommonAbstractions/LazyDocument/LazyDocument.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
using System.IO;
33
using System.Text;
44
using Elasticsearch.Net;
5-
using Newtonsoft.Json;
65
using Newtonsoft.Json.Linq;
76

87
namespace Nest
98
{
10-
[JsonConverter(typeof(LazyDocumentJsonConverter))]
9+
[ContractJsonConverter(typeof(LazyDocumentJsonConverter))]
1110
public interface ILazyDocument
1211
{
1312
/// <summary>

src/Nest/CommonAbstractions/SerializationBehavior/JsonNetSerializer.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
2-
using System.Collections.Concurrent;
3-
using System.Collections.Generic;
42
using System.IO;
5-
using System.Reflection;
63
using System.Text;
74
using System.Threading;
85
using System.Threading.Tasks;

src/Nest/Document/Multiple/Bulk/BulkOperation/BulkIndex.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public interface IBulkIndexOperation<T> : IBulkOperation
1111
[JsonProperty("pipeline")]
1212
string Pipeline { get; set; }
1313

14+
[JsonConverter(typeof(SourceConverter))]
1415
T Document { get; set; }
1516
}
1617

src/Nest/Document/Multiple/Bulk/BulkRequestJsonConverter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
3030
writer.WriteRaw($"{{\"{op.Operation}\":" + opJson + "}\n");
3131
var body = op.GetBody();
3232
if (body == null) continue;
33-
var bodyJson = (op.Operation == "update" ? requestResponseSerializer : sourceSerializer)
33+
var bodyJson = (
34+
op.Operation == "update" || body is ILazyDocument
35+
? requestResponseSerializer
36+
: sourceSerializer
37+
)
3438
.SerializeToString(body, SerializationFormatting.None);
3539

3640
writer.WriteRaw(bodyJson + "\n");

src/Nest/Document/Multiple/Reindex/ReindexObservable.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ private void Reindex(IObserver<IBulkAllResponse> observer)
7777
.SelectMany(r => r.SearchResponse.Hits)
7878
.Select(r=> r.Copy(this._reindexRequest.Map));
7979

80-
8180
var observableBulk = this.BulkAll(scrollDocuments, backPressure, toIndex);
8281

8382
//by casting the observable can potentially store more meta data on the user provided observer
84-
var moreInfoObserver = observer as BulkAllObserver;
85-
if (moreInfoObserver != null)
83+
if (observer is BulkAllObserver moreInfoObserver)
8684
observableBulk.Subscribe(moreInfoObserver);
8785
else observableBulk.Subscribe(observer);
8886
}
@@ -93,6 +91,7 @@ private BulkAllObservable<IHitMetadata<TTarget>> BulkAll(IEnumerable<IHitMetadat
9391
if (bulkAllRequest == null)
9492
throw new Exception("BulkAll must set on ReindexRequest in order to get the target of a Reindex operation");
9593

94+
bulkAllRequest.Type = null;
9695
bulkAllRequest.BackPressure = backPressure;
9796
bulkAllRequest.BufferToBulk = (bulk, hits) =>
9897
{
@@ -219,12 +218,6 @@ private int CreateIndex(string toIndex, IScrollAllRequest scrollAll)
219218
originalIndexState = getIndexResponse.Indices[resolvedFrom];
220219
if (this._reindexRequest.OmitIndexCreation)
221220
return originalIndexState.Settings.NumberOfShards;
222-
223-
// Black list internal settings that cannot be copied over
224-
// See https://github.com/elastic/elasticsearch/issues/21096
225-
originalIndexState.Settings.Remove("index.provided_name");
226-
originalIndexState.Settings.Remove("index.creation_date");
227-
originalIndexState.Settings.Remove("index.version.created");
228221
}
229222

230223
var createIndexRequest = this._reindexRequest.CreateIndexRequest ??

src/Nest/Indices/IndexManagement/CreateIndex/CreateIndexRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ public partial class CreateIndexRequest
1313
//Only here for ReadAsType new() constraint needs to be updated
1414
internal CreateIndexRequest() { }
1515

16-
public CreateIndexRequest(IndexName index, IndexState state) : this(index)
16+
public CreateIndexRequest(IndexName index, IIndexState state) : this(index)
1717
{
1818
this.Settings = state.Settings;
1919
this.Mappings = state.Mappings;
20-
this.Aliases = state.Aliases;
2120
this.Similarity = state.Similarity;
2221
CreateIndexRequest.RemoveReadOnlySettings(this.Settings);
2322
}
@@ -27,6 +26,7 @@ public CreateIndexRequest(IndexName index, IndexState state) : this(index)
2726
"index.creation_date",
2827
"index.uuid",
2928
"index.version.created",
29+
"index.provided_name"
3030
};
3131

3232
internal static void RemoveReadOnlySettings (IIndexSettings settings)

src/Nest/Mapping/Types/Core/Join/JoinFieldJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal class JoinFieldJsonConverter :JsonConverter
1010
{
1111
public override bool CanRead => true;
1212
public override bool CanWrite => true;
13+
public override bool CanConvert(Type objectType) => true;
1314

1415
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
1516
{
@@ -47,6 +48,5 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
4748
);
4849
}
4950

50-
public override bool CanConvert(Type objectType) => true;
5151
}
5252
}

src/Serializers/Nest.JsonNetSerializer/Converters/RevertBackToBuiltinSerializer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
3030
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
3131
{
3232
var token = JToken.ReadFrom(reader);
33+
//in place because JsonConverter.Deserialize() only works on full json objects.
34+
//even though we pass type JSON.NET won't try the registered converter for that type
35+
//even if it can handle string tokens :(
36+
if (objectType == typeof(JoinField) && token.Type == JTokenType.String)
37+
return JoinField.Root(token.ToString());
38+
3339
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(token.ToString())))
3440
return _builtInSerializer.Deserialize(objectType, ms);
3541
}
@@ -38,7 +44,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
3844
typeof(JoinField),
3945
typeof(QueryContainer),
4046
typeof(CompletionField),
41-
typeof(Attachment)
47+
typeof(Attachment),
48+
typeof(ILazyDocument)
4249
};
4350

4451
public override bool CanConvert(Type objectType) => NestTypesThatCanAppearInSource.Contains(objectType);

src/Tests/Document/Multiple/Reindex/ReindexApiTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,25 @@ public ReindexApiTests(ManualReindexCluster cluster, EndpointUsage usage)
6262
// create a thousand commits and associate with the projects
6363
var commits = CommitActivity.Generator.Generate(5000).ToList();
6464
var bb = new BulkDescriptor();
65-
for (int i = 0; i < commits.Count; i++)
65+
for (var i = 0; i < commits.Count; i++)
6666
{
6767
var commit = commits[i];
6868
var project = i % 2 == 0
6969
? projects[0].Name
7070
: projects[1].Name;
7171

7272
bb.Index<CommitActivity>(bi => bi
73-
.Document(commit)
73+
.Document(UpdateJoin(commit, project))
7474
.Index(IndexName)
7575
.Id(commit.Id)
7676
.Routing(project)
7777
);
7878
}
79+
CommitActivity UpdateJoin(CommitActivity a, string p)
80+
{
81+
a.ProjectName = p;
82+
return a;
83+
}
7984

8085
var bulkResult = this._client.Bulk(b => bb);
8186
bulkResult.ShouldBeValid();
@@ -126,7 +131,7 @@ [I] public void ReturnsExpectedResponse()
126131
CountdownEvent observableWait = null;
127132
var reindexRoutines = new List<Action>
128133
{
129-
//() => ReindexMany(GetSignal, Signal),
134+
() => ReindexMany(GetSignal, Signal),
130135
() => ReindexSingleType(GetSignal, Signal),
131136
() => ReindexProjection(GetSignal, Signal)
132137
};
@@ -148,7 +153,6 @@ public void ReindexMany(Func<CountdownEvent> getCountDown, Action<Exception> sig
148153
);
149154

150155
this._reindexManyTypesResult.Subscribe(manyTypesObserver);
151-
this._reindexManyTypesResult.Wait(TimeSpan.FromMinutes(3), r => { });
152156
}
153157

154158
private void ReindexManyTypesCompleted(CountdownEvent handle)

0 commit comments

Comments
 (0)