|
| 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.Diagnostics; |
| 7 | +using System.Text.Json.Serialization; |
| 8 | + |
| 9 | +using Elastic.Clients.Elasticsearch; |
| 10 | +using Elastic.Clients.Elasticsearch.Serialization; |
| 11 | +using Elastic.Transport; |
| 12 | +using Elastic.Transport.Extensions; |
| 13 | + |
| 14 | +namespace AOT; |
| 15 | + |
| 16 | +public static class Program |
| 17 | +{ |
| 18 | + public static void Main(string[] args) |
| 19 | + { |
| 20 | + var nodePool = new SingleNodePool(new Uri("http://localhost:9200")); |
| 21 | + var settings = new ElasticsearchClientSettings( |
| 22 | + nodePool, |
| 23 | + sourceSerializer: (_, settings) => |
| 24 | + new DefaultSourceSerializer(settings, UserTypeSerializerContext.Default) |
| 25 | + ) |
| 26 | + .DefaultMappingFor<Person>(x => x.IndexName("index")); |
| 27 | + |
| 28 | + var client = new ElasticsearchClient(settings); |
| 29 | + |
| 30 | + var person = new Person |
| 31 | + { |
| 32 | + Id = 1234, |
| 33 | + FirstName = "Florian", |
| 34 | + LastName = "Bernd" |
| 35 | + }; |
| 36 | + |
| 37 | + Trace.Assert(client.Infer.Id(person) == "1234"); |
| 38 | + |
| 39 | + var indexRequest = new IndexRequest<Person>(person); |
| 40 | + var indexRequestBody = client.ElasticsearchClientSettings.RequestResponseSerializer.SerializeToString(indexRequest); |
| 41 | + var indexRequest2 = client.ElasticsearchClientSettings.RequestResponseSerializer.Deserialize<IndexRequest<Person>>(indexRequestBody)!; |
| 42 | + |
| 43 | + Trace.Assert(indexRequest.Document == indexRequest2.Document); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +internal sealed record Person |
| 48 | +{ |
| 49 | + public long? Id { get; init; } |
| 50 | + public required string FirstName { get; init; } |
| 51 | + public required string LastName { get; init; } |
| 52 | + public DateTimeOffset? BirthDate { get; init; } |
| 53 | +} |
| 54 | + |
| 55 | +[JsonSerializable(typeof(Person), GenerationMode = JsonSourceGenerationMode.Default)] |
| 56 | +internal sealed partial class UserTypeSerializerContext : |
| 57 | + JsonSerializerContext |
| 58 | +{ |
| 59 | +} |
0 commit comments