Skip to content

Commit 49210bc

Browse files
committed
Add AOT example
1 parent 14c628d commit 49210bc

File tree

7 files changed

+89
-7
lines changed

7 files changed

+89
-7
lines changed

Elasticsearch.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests.ClusterLauncher", "te
5757
EndProject
5858
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "tests\Tests\Tests.csproj", "{6FD804B2-CE80-41CB-A411-2023F34C18FE}"
5959
EndProject
60+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aot", "examples\aot\aot.csproj", "{3FA9C99A-7DA0-4DF2-89C0-BDDFC97E2CB7}"
61+
EndProject
62+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
63+
EndProject
6064
Global
6165
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6266
Debug|Any CPU = Debug|Any CPU
@@ -103,6 +107,10 @@ Global
103107
{6FD804B2-CE80-41CB-A411-2023F34C18FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
104108
{6FD804B2-CE80-41CB-A411-2023F34C18FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
105109
{6FD804B2-CE80-41CB-A411-2023F34C18FE}.Release|Any CPU.Build.0 = Release|Any CPU
110+
{3FA9C99A-7DA0-4DF2-89C0-BDDFC97E2CB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111+
{3FA9C99A-7DA0-4DF2-89C0-BDDFC97E2CB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
112+
{3FA9C99A-7DA0-4DF2-89C0-BDDFC97E2CB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
113+
{3FA9C99A-7DA0-4DF2-89C0-BDDFC97E2CB7}.Release|Any CPU.Build.0 = Release|Any CPU
106114
EndGlobalSection
107115
GlobalSection(SolutionProperties) = preSolution
108116
HideSolutionNode = FALSE
@@ -118,6 +126,7 @@ Global
118126
{68D1BFDC-F447-4D2C-AF81-537807636610} = {1FE49D14-216A-41EE-A177-E42BFF53E0DC}
119127
{F6162603-D134-4121-8106-2BA4DAD7350B} = {362B2776-4B29-46AB-B237-56776B5372B6}
120128
{6FD804B2-CE80-41CB-A411-2023F34C18FE} = {362B2776-4B29-46AB-B237-56776B5372B6}
129+
{3FA9C99A-7DA0-4DF2-89C0-BDDFC97E2CB7} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
121130
EndGlobalSection
122131
GlobalSection(ExtensibilityGlobals) = postSolution
123132
SolutionGuid = {CE74F821-B001-4C69-A58D-CF81F8B0B632}

examples/aot/Program.cs

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

examples/aot/aot.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
7+
<Nullable>enable</Nullable>
8+
<PublishAot>true</PublishAot>
9+
<InvariantGlobalization>true</InvariantGlobalization>
10+
<TrimmerSingleWarn>false</TrimmerSingleWarn>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\src\Elastic.Clients.Elasticsearch\Elastic.Clients.Elasticsearch.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

src/Elastic.Clients.Elasticsearch/Elastic.Clients.Elasticsearch.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</PropertyGroup>
3232

3333
<ItemGroup>
34-
<PackageReference Include="Elastic.Transport" Version="0.9.2" />
34+
<PackageReference Include="Elastic.Transport" Version="0.10.0" />
3535
<PackageReference Include="PolySharp" Version="1.15.0">
3636
<PrivateAssets>all</PrivateAssets>
3737
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/DynamicPropertyAccessor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ internal static class DynamicPropertyAccessor
5050

5151
// Build compiled getter delegate.
5252

53-
#pragma warning disable IL3050
54-
#pragma warning disable IL2060
53+
#pragma warning disable IL3050, IL2060
5554
var getterDelegateFactory = MakeDelegateMethodInfo.MakeGenericMethod(type, getterMethod.ReturnType);
56-
#pragma warning restore IL3050
57-
#pragma warning restore IL2060
55+
#pragma warning restore IL3050, IL2060
5856
var genericGetterDelegate = (Func<object, object>)getterDelegateFactory.Invoke(null, [getterMethod])!;
5957

6058
return instance => retrieverFunc(genericGetterDelegate, instance);

src/Playground/Person.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Runtime.Serialization;
66
using Elastic.Clients.Elasticsearch;
7+
using Elastic.Clients.Elasticsearch.QueryDsl;
78

89
namespace Playground
910
{
@@ -30,6 +31,8 @@ public class Person
3031
public string Data { get; init; } = "NOTHING";
3132

3233
public DateTimeKind Enum { get; init; }
34+
35+
public Query? Q { get; init; }
3336
}
3437

3538
public class PersonV3

src/Playground/Playground.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Elastic.Transport" Version="0.9.2" />
17-
<PackageReference Include="System.Text.Json" Version="8.0.5" />
16+
<PackageReference Include="Elastic.Transport" Version="0.10.0" />
17+
<PackageReference Include="System.Text.Json" Version="9.0.8" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)