Skip to content

Commit b138d67

Browse files
Complete JoinFieldConverter (#6396) (#6397)
* Complete JoinFieldConverter * Fix license headers Co-authored-by: Steve Gordon <[email protected]>
1 parent fae2153 commit b138d67

File tree

11 files changed

+798
-6
lines changed

11 files changed

+798
-6
lines changed

src/Elastic.Clients.Elasticsearch/Client/ElasticsearchClient-Manual.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public IndexResponse Index<TDocument>(TDocument document, Action<IndexRequestDes
1717
return DoRequest<IndexRequestDescriptor<TDocument>, IndexResponse>(descriptor);
1818
}
1919

20+
public IndexResponse Index<TDocument>(TDocument document)
21+
{
22+
var descriptor = new IndexRequestDescriptor<TDocument>(documentWithId: document);
23+
return DoRequest<IndexRequestDescriptor<TDocument>, IndexResponse>(descriptor);
24+
}
25+
2026
public Task<IndexResponse> IndexAsync<TDocument>(TDocument document, CancellationToken cancellationToken = default)
2127
{
2228
var descriptor = new IndexRequestDescriptor<TDocument>(documentWithId: document);

src/Elastic.Clients.Elasticsearch/Common/Infer/JoinFieldConverter.cs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,77 @@
55
using System;
66
using System.Text.Json.Serialization;
77
using System.Text.Json;
8+
using Elastic.Transport;
89

910
namespace Elastic.Clients.Elasticsearch
1011
{
1112
internal sealed class JoinFieldConverter : JsonConverter<JoinField>
1213
{
13-
public override JoinField? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();
14+
private readonly IElasticsearchClientSettings _elasticsearchClientSettings;
15+
16+
public JoinFieldConverter(IElasticsearchClientSettings elasticsearchClientSettings) => _elasticsearchClientSettings = elasticsearchClientSettings;
17+
18+
public override JoinField? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
19+
{
20+
if (reader.TokenType == JsonTokenType.String)
21+
{
22+
var parent = reader.GetString();
23+
return new JoinField(new JoinField.Parent(parent));
24+
}
25+
26+
Id parentId = null;
27+
string name = null;
28+
29+
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
30+
{
31+
if (reader.TokenType != JsonTokenType.PropertyName)
32+
throw new JsonException($"Unexpected token. Expected {JsonTokenType.PropertyName}, but read {reader.TokenType}.");
33+
34+
var propertyName = reader.GetString();
35+
reader.Read();
36+
37+
switch (propertyName)
38+
{
39+
case "parent":
40+
parentId = JsonSerializer.Deserialize<Id>(ref reader, options);
41+
break;
42+
case "name":
43+
name = reader.GetString();
44+
break;
45+
default:
46+
throw new JsonException($"Read an unexpected property name {propertyName}.");
47+
}
48+
}
49+
50+
return parentId != null
51+
? new JoinField(new JoinField.Child(name, parentId))
52+
: new JoinField(new JoinField.Parent(name));
53+
}
54+
1455
public override void Write(Utf8JsonWriter writer, JoinField value, JsonSerializerOptions options)
1556
{
57+
if (value is null)
58+
{
59+
writer.WriteNullValue();
60+
return;
61+
}
62+
1663
switch (value.Tag)
1764
{
1865
case 0:
1966
JsonSerializer.Serialize(writer, value.ParentOption.Name, options);
20-
break;
21-
}
67+
break;
2268

23-
// TODO - Finish this
69+
case 1:
70+
writer.WriteStartObject();
71+
writer.WritePropertyName("name");
72+
JsonSerializer.Serialize(writer, value.ChildOption.Name, options);
73+
writer.WritePropertyName("parent");
74+
var id = (value.ChildOption.ParentId as IUrlParameter)?.GetString(_elasticsearchClientSettings);
75+
writer.WriteStringValue(id);
76+
writer.WriteEndObject();
77+
break;
78+
}
2479
}
2580
}
2681
}

src/Elastic.Clients.Elasticsearch/Serialization/DefaultRequestResponseSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
4848
new SortCollectionConverter(settings),
4949
new LazyDocumentConverter(settings),
5050
new RelationNameConverter(settings),
51+
new JoinFieldConverter(settings),
5152
new CustomJsonWriterConverterFactory(settings),
5253
new RoutingConverter(settings),
5354
new SelfSerializableConverterFactory(settings),

src/Elastic.Clients.Elasticsearch/Serialization/DefaultSourceSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public DefaultSourceSerializer(IElasticsearchClientSettings settings, JsonSerial
2121
new IdConverter(settings),
2222
new RelationNameConverter(settings),
2323
new RoutingConverter(settings),
24+
new JoinFieldConverter(settings),
2425
new LazyDocumentConverter(settings),
2526
},
2627
PropertyNamingPolicy = JsonNamingPolicy.CamelCase

src/Elastic.Clients.Elasticsearch/Types/Mapping/Properties.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ public bool TryGetProperty<T>(PropertyName propertyName, out T property) where T
1818
return false;
1919
}
2020
}
21+
22+
public partial class TypeMappingDescriptor
23+
{
24+
25+
}
26+

src/PlaygroundV7x/Person.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
6+
using Nest;
7+
58
namespace PlaygroundV7x
69
{
710
public class Person
811
{
12+
public int Id { get; set; }
13+
14+
public Guid SecondaryId { get; set; } = Guid.NewGuid();
915
public string? FirstName { get; init; }
1016
public string? LastName { get; init; }
1117
public int? Age { get; init; }
18+
//public Routing? Routing { get; init; }
19+
20+
public Id Idv3 => "testing";
21+
//public Guid Routing { get; init; } = Guid.NewGuid();
22+
1223
public string? Email { get; init; }
24+
25+
public string Data { get; init; } = "NOTHING";
1326
}
1427
}

src/PlaygroundV7x/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Threading.Tasks;
8+
using Elasticsearch.Net;
89
using Nest;
910

1011
namespace PlaygroundV7x
@@ -36,7 +37,20 @@ private static async Task Main()
3637
})
3738
};
3839

39-
var client = new ElasticClient();
40+
var client = new ElasticClient(new ConnectionSettings(new InMemoryConnection())
41+
.DefaultIndex("default-index")
42+
.DefaultMappingFor<Person>(m => m
43+
.DisableIdInference()
44+
.IndexName("people")
45+
.IdProperty(id => id.SecondaryId)
46+
.RoutingProperty(id => id.SecondaryId)
47+
.RelationName("relation"))
48+
//.DefaultFieldNameInferrer(s => $"{s}_2")
49+
.EnableDebugMode());
50+
51+
var person = new Person { Id = 101, FirstName = "Steve", LastName = "Gordon", Age = 37, Email = "[email protected]" };
52+
53+
var routingResponse = await client.IndexAsync(person, r => r);
4054

4155
client.Update<Person>("a", d => d.Index("test").Script(s => s.Source("script").Params(new Dictionary<string, object?> { { "null", new Person { FirstName = null, LastName = "test-surname" } } })));
4256

tests/Tests.Configuration/tests.default.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ force_reseed: true
1717
# this is opt in during development in CI we never want to see our tests running against an already running node
1818
test_against_already_running_elasticsearch: true
1919

20-
#random_source_serializer: true
20+
random_source_serializer: false
2121
#random_old_transportClient: true
2222
#random_http_compresssion: true
2323
#seed: 74337

0 commit comments

Comments
 (0)