Skip to content

Commit 64b4d72

Browse files
Various improvements and clean up (#6371) (#6372)
* Add deserialisation test for DataStreamNames * Type cleanup and sealing * Update FieldValues * Descriptor cleanup * Document path * Initial Fields cleanup * TaskId cleanup * Lots of cleanup * Lots of sealing * Code generator seals more types * Extract out types from future generated * Reorg serialization types * Skip v7 folder * Fix BOM Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 9d7101e commit 64b4d72

File tree

702 files changed

+3557
-5619
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

702 files changed

+3557
-5619
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Elastic.Clients.Elasticsearch.QueryDsl;
7+
8+
namespace Elastic.Clients.Elasticsearch.AsyncSearch
9+
{
10+
public partial class AsyncSearchSubmitRequest
11+
{
12+
// Any request may contain aggregations so we force typed_keys in order to successfully deserialise them.
13+
internal override void BeforeRequest() => TypedKeys = true;
14+
}
15+
16+
public sealed partial class AsyncSearchSubmitRequestDescriptor
17+
{
18+
public AsyncSearchSubmitRequestDescriptor MatchAll(Action<MatchAllQueryDescriptor>? selector = null) => selector is null ? Query(q => q.MatchAll()) : Query(q => q.MatchAll(selector));
19+
20+
internal override void BeforeRequest() => TypedKeys(true);
21+
}
22+
23+
public sealed partial class AsyncSearchSubmitRequestDescriptor<TDocument>
24+
{
25+
public AsyncSearchSubmitRequestDescriptor<TDocument> MatchAll()
26+
{
27+
Query(new MatchAllQuery());
28+
return Self;
29+
}
30+
31+
internal override void BeforeRequest() => TypedKeys(true);
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Elastic.Clients.Elasticsearch.AsyncSearch
6+
{
7+
public partial class GetAsyncSearchRequest
8+
{
9+
// Any request may contain aggregations so we force typed_keys in order to successfully deserialise them.
10+
internal override void BeforeRequest() => TypedKeys = true;
11+
}
12+
13+
public sealed partial class GetAsyncSearchRequestDescriptor<TDocument>
14+
{
15+
// Any request may contain aggregations so we force typed_keys in order to successfully deserialise them.
16+
internal override void BeforeRequest() => TypedKeys(true);
17+
}
18+
19+
public sealed partial class GetAsyncSearchRequestDescriptor
20+
{
21+
// Any request may contain aggregations so we force typed_keys in order to successfully deserialise them.
22+
internal override void BeforeRequest() => TypedKeys(true);
23+
}
24+
}

src/Elastic.Clients.Elasticsearch/Api/AsyncSearchSubmitRequestDescriptor.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Threading.Tasks;
7+
using Elastic.Transport;
8+
using System.IO;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
12+
namespace Elastic.Clients.Elasticsearch
13+
{
14+
public partial class BulkRequest : IStreamSerializable
15+
{
16+
protected IRequest Self => this;
17+
18+
public BulkOperationsCollection Operations { get; set; }
19+
20+
protected override string ContentType => "application/x-ndjson";
21+
22+
protected override string Accept => "application/json";
23+
24+
public void Serialize(Stream stream, IElasticsearchClientSettings settings, SerializationFormatting formatting = SerializationFormatting.None)
25+
{
26+
if (Operations is null)
27+
return;
28+
29+
var index = Self.RouteValues.Get<IndexName>("index");
30+
31+
foreach (var op in Operations)
32+
{
33+
if (op is not IStreamSerializable serializable)
34+
throw new InvalidOperationException("");
35+
36+
op.PrepareIndex(index);
37+
38+
serializable.Serialize(stream, settings, formatting);
39+
stream.WriteByte((byte)'\n');
40+
}
41+
}
42+
43+
public async Task SerializeAsync(Stream stream, IElasticsearchClientSettings settings, SerializationFormatting formatting = SerializationFormatting.None)
44+
{
45+
if (Operations is null)
46+
return;
47+
48+
var index = Self.RouteValues.Get<IndexName>("index");
49+
50+
foreach (var op in Operations)
51+
{
52+
if (op is not IStreamSerializable serializable)
53+
throw new InvalidOperationException("");
54+
55+
op.PrepareIndex(index);
56+
57+
await serializable.SerializeAsync(stream, settings, formatting).ConfigureAwait(false);
58+
stream.WriteByte((byte)'\n');
59+
}
60+
}
61+
}
62+
63+
public sealed partial class BulkRequestDescriptor : IStreamSerializable
64+
{
65+
protected override string ContentType => "application/x-ndjson";
66+
67+
protected override string Accept => "application/json";
68+
69+
private readonly BulkOperationsCollection _operations = new();
70+
71+
public BulkRequestDescriptor Index(string index)
72+
{
73+
RouteValues.Optional("index", IndexName.Parse(index));
74+
return Self;
75+
}
76+
77+
public BulkRequestDescriptor Create<TSource>(TSource document, Action<BulkCreateOperationDescriptor<TSource>> configure = null)
78+
{
79+
var descriptor = new BulkCreateOperationDescriptor<TSource>(document);
80+
configure?.Invoke(descriptor);
81+
_operations.Add(descriptor);
82+
return this;
83+
}
84+
85+
public BulkRequestDescriptor Create<TSource>(TSource document, IndexName index, Action<BulkCreateOperationDescriptor<TSource>> configure = null)
86+
{
87+
var descriptor = new BulkCreateOperationDescriptor<TSource>(document, index);
88+
configure?.Invoke(descriptor);
89+
_operations.Add(descriptor);
90+
return this;
91+
}
92+
93+
public BulkRequestDescriptor Index<TSource>(TSource document, Action<BulkIndexOperationDescriptor<TSource>> configure = null)
94+
{
95+
var descriptor = new BulkIndexOperationDescriptor<TSource>(document);
96+
configure?.Invoke(descriptor);
97+
_operations.Add(descriptor);
98+
return this;
99+
}
100+
101+
public BulkRequestDescriptor Index<TSource>(TSource document, IndexName index, Action<BulkIndexOperationDescriptor<TSource>> configure = null)
102+
{
103+
var descriptor = new BulkIndexOperationDescriptor<TSource>(document, index);
104+
configure?.Invoke(descriptor);
105+
_operations.Add(descriptor);
106+
return this;
107+
}
108+
109+
public BulkRequestDescriptor Update(BulkUpdateOperationBase update)
110+
{
111+
_operations.Add(update);
112+
return this;
113+
}
114+
115+
public BulkRequestDescriptor Update<TSource, TPartialDocument>(Action<BulkUpdateOperationDescriptor<TSource, TPartialDocument>> configure)
116+
{
117+
var descriptor = new BulkUpdateOperationDescriptor<TSource, TPartialDocument>();
118+
configure?.Invoke(descriptor);
119+
_operations.Add(descriptor);
120+
return this;
121+
}
122+
123+
public BulkRequestDescriptor Update<T>(Action<BulkUpdateOperationDescriptor<T, T>> configure) =>
124+
Update<T, T>(configure);
125+
126+
public BulkRequestDescriptor Delete(Id id, Action<BulkDeleteOperationDescriptor> configure = null)
127+
{
128+
var descriptor = new BulkDeleteOperationDescriptor(id);
129+
configure?.Invoke(descriptor);
130+
_operations.Add(descriptor);
131+
return this;
132+
}
133+
134+
public BulkRequestDescriptor Delete(string id, Action<BulkDeleteOperationDescriptor> configure = null)
135+
{
136+
var descriptor = new BulkDeleteOperationDescriptor(id);
137+
configure?.Invoke(descriptor);
138+
_operations.Add(descriptor);
139+
return this;
140+
}
141+
142+
public BulkRequestDescriptor Delete(Action<BulkDeleteOperationDescriptor> configure)
143+
{
144+
var descriptor = new BulkDeleteOperationDescriptor();
145+
configure?.Invoke(descriptor);
146+
_operations.Add(descriptor);
147+
return this;
148+
}
149+
150+
public BulkRequestDescriptor Delete<TSource>(TSource documentToDelete, Action<BulkDeleteOperationDescriptor> configure = null)
151+
{
152+
var descriptor = new BulkDeleteOperationDescriptor(new Id(documentToDelete));
153+
configure?.Invoke(descriptor);
154+
_operations.Add(descriptor);
155+
return this;
156+
}
157+
158+
public BulkRequestDescriptor Delete<TSource>(Action<BulkDeleteOperationDescriptor> configure) => Delete(configure);
159+
160+
public BulkRequestDescriptor CreateMany<TSource>(IEnumerable<TSource> documents, Action<BulkCreateOperationDescriptor<TSource>, TSource> bulkCreateSelector) =>
161+
AddOperations(documents, bulkCreateSelector, o => new BulkCreateOperationDescriptor<TSource>(o));
162+
163+
public BulkRequestDescriptor CreateMany<TSource>(IEnumerable<TSource> documents) =>
164+
AddOperations(documents, null, o => new BulkCreateOperationDescriptor<TSource>(o));
165+
166+
public BulkRequestDescriptor IndexMany<TSource>(IEnumerable<TSource> documents, Action<BulkIndexOperationDescriptor<TSource>, TSource> bulkIndexSelector) =>
167+
AddOperations(documents, bulkIndexSelector, o => new BulkIndexOperationDescriptor<TSource>(o));
168+
169+
public BulkRequestDescriptor IndexMany<TSource>(IEnumerable<TSource> documents) =>
170+
AddOperations(documents, null, o => new BulkIndexOperationDescriptor<TSource>(o));
171+
172+
public BulkRequestDescriptor UpdateMany<TSource>(IEnumerable<TSource> objects, Action<BulkUpdateOperationDescriptor<TSource, TSource>, TSource> bulkIndexSelector) =>
173+
AddOperations(objects, bulkIndexSelector, o => new BulkUpdateOperationDescriptor<TSource, TSource>().IdFrom(o));
174+
175+
public BulkRequestDescriptor UpdateMany<TSource>(IEnumerable<TSource> objects) =>
176+
AddOperations(objects, null, o => new BulkUpdateOperationDescriptor<TSource, TSource>().IdFrom(o));
177+
178+
public BulkRequestDescriptor DeleteMany<T>(IEnumerable<T> objects, Action<BulkDeleteOperationDescriptor, T> bulkDeleteSelector) =>
179+
AddOperations(objects, bulkDeleteSelector, obj => new BulkDeleteOperationDescriptor(new Id(obj)));
180+
181+
public BulkRequestDescriptor DeleteMany(IEnumerable<Id> ids, Action<BulkDeleteOperationDescriptor, Id> bulkDeleteSelector) =>
182+
AddOperations(ids, bulkDeleteSelector, id => new BulkDeleteOperationDescriptor(id));
183+
184+
public BulkRequestDescriptor DeleteMany<T>(IEnumerable<T> objects) =>
185+
AddOperations(objects, null, obj => new BulkDeleteOperationDescriptor<T>(obj));
186+
187+
public BulkRequestDescriptor DeleteMany(IndexName index, IEnumerable<Id> ids) =>
188+
AddOperations(ids, null, id => new BulkDeleteOperationDescriptor(id).Index(index));
189+
190+
public void Serialize(Stream stream, IElasticsearchClientSettings settings, SerializationFormatting formatting = SerializationFormatting.None)
191+
{
192+
if (_operations is null)
193+
return;
194+
195+
var index = Self.RouteValues.Get<IndexName>("index");
196+
197+
foreach (var op in _operations)
198+
{
199+
if (op is not IStreamSerializable serializable)
200+
throw new InvalidOperationException("");
201+
202+
op.PrepareIndex(index);
203+
204+
serializable.Serialize(stream, settings, formatting);
205+
stream.WriteByte((byte)'\n');
206+
}
207+
}
208+
209+
public async Task SerializeAsync(Stream stream, IElasticsearchClientSettings settings, SerializationFormatting formatting = SerializationFormatting.None)
210+
{
211+
if (_operations is null)
212+
return;
213+
214+
var index = Self.RouteValues.Get<IndexName>("index");
215+
216+
foreach (var op in _operations)
217+
{
218+
if (op is not IStreamSerializable serializable)
219+
throw new InvalidOperationException("");
220+
221+
op.PrepareIndex(index);
222+
223+
await serializable.SerializeAsync(stream, settings, formatting).ConfigureAwait(false);
224+
stream.WriteByte((byte)'\n');
225+
}
226+
}
227+
228+
private BulkRequestDescriptor AddOperations<TSource, TDescriptor>(
229+
IEnumerable<TSource> objects,
230+
Action<TDescriptor, TSource> configureDescriptor,
231+
Func<TSource, TDescriptor> createDescriptor
232+
) where TDescriptor : IBulkOperation
233+
{
234+
if (@objects == null)
235+
return this;
236+
237+
var objectsList = @objects.ToList();
238+
var operations = new List<IBulkOperation>(objectsList.Count());
239+
240+
foreach (var o in objectsList)
241+
{
242+
var descriptor = createDescriptor(o);
243+
244+
if (configureDescriptor is not null)
245+
{
246+
configureDescriptor(descriptor, o);
247+
}
248+
249+
operations.Add(descriptor);
250+
}
251+
252+
_operations.AddRange(operations);
253+
return Self;
254+
}
255+
}
256+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using System.Text.Json.Serialization;
7+
using System.Text;
8+
using System.Linq;
9+
10+
namespace Elastic.Clients.Elasticsearch
11+
{
12+
public partial class BulkResponse
13+
{
14+
[JsonConverter(typeof(BulkResponseItemConverter)), JsonPropertyName("items")]
15+
public IReadOnlyList<BulkResponseItemBase> Items { get; init; }
16+
17+
[JsonIgnore]
18+
public IEnumerable<BulkResponseItemBase> ItemsWithErrors => !Items.HasAny()
19+
? Enumerable.Empty<BulkResponseItemBase>()
20+
: Items.Where(i => !i.IsValid);
21+
22+
public override bool IsValid => base.IsValid && !Errors && !ItemsWithErrors.HasAny();
23+
24+
protected override void DebugIsValid(StringBuilder sb)
25+
{
26+
if (Items == null)
27+
return;
28+
29+
sb.AppendLine($"# Invalid Bulk items:");
30+
foreach (var i in Items.Select((item, i) => new { item, i }).Where(i => !i.item.IsValid))
31+
sb.AppendLine($" operation[{i.i}]: {i.item}");
32+
}
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Elastic.Clients.Elasticsearch
6+
{
7+
public abstract partial class BulkResponseItemBase
8+
{
9+
public abstract string Operation { get; }
10+
11+
public bool IsValid
12+
{
13+
get
14+
{
15+
if (Error is not null)
16+
return false;
17+
18+
return Operation.ToLowerInvariant() switch
19+
{
20+
"delete" => Status == 200 || Status == 404,
21+
"update" or "index" or "create" => Status == 200 || Status == 201,
22+
_ => false,
23+
};
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)