Skip to content

Commit 42fbd6f

Browse files
[9.1] Add AOT example (#8569) (#8663)
Co-authored-by: Florian Bernd <[email protected]>
1 parent 7ffeadf commit 42fbd6f

File tree

9 files changed

+95
-7
lines changed

9 files changed

+95
-7
lines changed

.buildkite/DockerFile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ COPY ./tests/*.Build.props ./tests/
2424
COPY src/*/*.?sproj ./src/
2525
COPY tests/*/*.?sproj ./tests/
2626
COPY benchmarks/*/*.?sproj ./benchmarks/
27+
COPY examples/*/*.?sproj ./examples/
2728

2829
# this puts the project files back into original location since COPY flattens
2930
RUN for file in $(find . -name "*.?sproj"); do echo mkdir -p $(dirname $file)/$(basename ${file%.*})/ && echo mv $file $(dirname $file)/$(basename ${file%.*})/; done

.ci/DockerFile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ COPY ./tests/*.Build.props ./tests/
2424
COPY src/*/*.?sproj ./src/
2525
COPY tests/*/*.?sproj ./tests/
2626
COPY benchmarks/*/*.?sproj ./benchmarks/
27+
COPY examples/*/*.?sproj ./examples/
2728

2829
# this puts the project files back into original location since COPY flattens
2930
RUN for file in $(find . -name "*.?sproj"); do echo mkdir -p $(dirname $file)/$(basename ${file%.*})/ && echo mv $file $(dirname $file)/$(basename ${file%.*})/; done

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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

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)