Skip to content

Commit 5c384a5

Browse files
author
Matteo Calisti
committed
first commit
0 parents  commit 5c384a5

5 files changed

+328
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
################################################################################
2+
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
3+
################################################################################
4+
5+
/.vs
6+
/elasticsearch-net-tsds-mode-changes-source-serialization/bin/Debug/net6.0
7+
/elasticsearch-net-tsds-mode-changes-source-serialization/obj
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34316.72
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "elasticsearch-net-tsds-mode-changes-source-serialization", "elasticsearch-net-tsds-mode-changes-source-serialization\elasticsearch-net-tsds-mode-changes-source-serialization.csproj", "{CE4DF877-7FF9-4BDD-A587-08C56C957EC5}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_", "_", "{69BD1196-DAD8-4DF2-9249-C91CB95CCD66}"
9+
ProjectSection(SolutionItems) = preProject
10+
elasticsearch-net-tsds-mode-changes-source-serialization\.gitignore = elasticsearch-net-tsds-mode-changes-source-serialization\.gitignore
11+
EndProjectSection
12+
EndProject
13+
Global
14+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
15+
Debug|Any CPU = Debug|Any CPU
16+
Release|Any CPU = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{CE4DF877-7FF9-4BDD-A587-08C56C957EC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{CE4DF877-7FF9-4BDD-A587-08C56C957EC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{CE4DF877-7FF9-4BDD-A587-08C56C957EC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{CE4DF877-7FF9-4BDD-A587-08C56C957EC5}.Release|Any CPU.Build.0 = Release|Any CPU
23+
EndGlobalSection
24+
GlobalSection(SolutionProperties) = preSolution
25+
HideSolutionNode = FALSE
26+
EndGlobalSection
27+
GlobalSection(ExtensibilityGlobals) = postSolution
28+
SolutionGuid = {405021B4-02CF-45F8-AE90-6F5C2AA2F8F5}
29+
EndGlobalSection
30+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<RootNamespace>elasticsearch_net_tsds_mode_changes_source_serialization</RootNamespace>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.13.12" />
11+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
12+
<PackageReference Include="Nunit" Version="3.13.3" />
13+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
15+
<PackageReference Include="Testcontainers" Version="3.8.0" />
16+
<PackageReference Include="Testcontainers.Elasticsearch" Version="3.8.0" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using Elastic.Clients.Elasticsearch;
2+
using Elastic.Clients.Elasticsearch.Cluster;
3+
using Elastic.Clients.Elasticsearch.IndexManagement;
4+
using Elastic.Clients.Elasticsearch.Mapping;
5+
using FluentAssertions;
6+
using NUnit.Framework;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Text.Json.Serialization;
11+
using System.Threading.Tasks;
12+
13+
namespace elasticsearch_net_tsds_mode_changes_source_serialization;
14+
15+
public class normal_index_tests
16+
{
17+
private static Guid id = Guid.NewGuid();
18+
private string componentMappingName = $"{id}-component-mapping";
19+
private string componentSettingName = $"{id}-component-setting";
20+
private string templateName = $"{id}-template";
21+
private string datastreamName = $"{id}-datastream";
22+
private const string elasticsearchAddress = "http://172.18.103.104:9201";
23+
24+
private ElasticsearchClient client = null!;
25+
private ElasticsearchClientSettings settings = null!;
26+
27+
public class SimpleDocument
28+
{
29+
public string Code { get; set; } = null!;
30+
[JsonPropertyName("@timestamp")]
31+
public DateTimeOffset Time { get; set; }
32+
public double Value { get; set; }
33+
public List<string> Tags { get; set; } = new List<string>();
34+
}
35+
36+
[SetUp]
37+
public async Task Setup()
38+
{
39+
id = Guid.NewGuid();
40+
componentMappingName = $"{id}-component-mapping";
41+
componentSettingName = $"{id}-component-setting";
42+
templateName = $"{id}-template";
43+
datastreamName = $"{id}-datastream";
44+
45+
settings = new ElasticsearchClientSettings(new Uri(elasticsearchAddress))
46+
.ThrowExceptions(true)
47+
.DisableDirectStreaming();
48+
49+
client = new ElasticsearchClient(settings);
50+
51+
PutComponentTemplateResponse putMappingReponse = await client.Cluster.PutComponentTemplateAsync<SimpleDocument>(
52+
componentMappingName,
53+
a => a
54+
.Template(t => t
55+
.Mappings(m => m
56+
.Properties(p => p
57+
.Keyword(k => k.Code, c => c.TimeSeriesDimension())
58+
.DoubleNumber(k => k.Value, c => c.TimeSeriesMetric(TimeSeriesMetricType.Gauge))
59+
.DateNanos(k => k.Time, c => c.Format("strict_date_optional_time_nanos"))
60+
.Wildcard(n => n.Tags)
61+
)
62+
.Dynamic(DynamicMapping.True)
63+
)
64+
)
65+
);
66+
67+
PutComponentTemplateResponse componentSettingsTemplateResponse
68+
= await client.Cluster.PutComponentTemplateAsync<SimpleDocument>(
69+
componentSettingName,
70+
d => d
71+
.Template(tc => tc
72+
.Settings(s => s
73+
.Codec("best_compression")
74+
.NumberOfShards(3)
75+
//.Index(i => i
76+
// .Mode("time_series")
77+
// .RoutingPath(new List<string> { "code" })
78+
//)
79+
)
80+
)
81+
);
82+
83+
PutIndexTemplateResponse putTemplateReponse = await client.Indices.PutIndexTemplateAsync(
84+
templateName, rd => rd
85+
.ComposedOf(new List<Name> { componentMappingName, componentSettingName })
86+
.IndexPatterns(datastreamName)
87+
.DataStream(new DataStreamVisibility())
88+
);
89+
90+
CreateDataStreamResponse createDatastreamResponse = await client.Indices.CreateDataStreamAsync(datastreamName);
91+
}
92+
93+
[TestCase]
94+
public async Task can_insert_and_read_list_of_strings()
95+
{
96+
SimpleDocument doc = new SimpleDocument
97+
{
98+
Code = "1",
99+
Time = DateTimeOffset.UtcNow,
100+
Value = 1,
101+
Tags = new List<string> { "one_tag", "second_tag" }
102+
};
103+
104+
await client.IndexAsync(doc, index: datastreamName);
105+
await client.Indices.RefreshAsync();
106+
107+
var response = await client.SearchAsync<SimpleDocument>(datastreamName, r => r.Index(datastreamName)
108+
.Size(2)
109+
.Sort(s => s.Field(f => f.Time)));
110+
111+
response.Documents.Should().HaveCount(1);
112+
response.Documents.First().Tags.Should().HaveCount(2);
113+
}
114+
115+
[TestCase]
116+
public async Task can_insert_and_read_list_of_strings_with_a_single_element()
117+
{
118+
SimpleDocument doc = new SimpleDocument
119+
{
120+
Code = "2",
121+
Time = DateTimeOffset.UtcNow,
122+
Value = 2,
123+
Tags = new List<string> { "one_tag" }
124+
};
125+
126+
await client.IndexAsync(doc, index: datastreamName);
127+
await client.Indices.RefreshAsync();
128+
129+
var response = await client.SearchAsync<SimpleDocument>(datastreamName, r => r.Index(datastreamName)
130+
.Size(2)
131+
.Sort(s => s.Field(f => f.Time)));
132+
133+
response.Documents.Should().HaveCount(1);
134+
response.Documents.First().Tags.Should().HaveCount(1);
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using Elastic.Clients.Elasticsearch;
2+
using Elastic.Clients.Elasticsearch.Cluster;
3+
using Elastic.Clients.Elasticsearch.IndexManagement;
4+
using Elastic.Clients.Elasticsearch.Mapping;
5+
using FluentAssertions;
6+
using NUnit.Framework;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Text.Json.Serialization;
11+
using System.Threading.Tasks;
12+
13+
namespace elasticsearch_net_tsds_mode_changes_source_serialization;
14+
15+
public class ts_mode_index_tests
16+
{
17+
private static Guid id = Guid.NewGuid();
18+
private string componentMappingName = $"{id}-component-mapping";
19+
private string componentSettingName = $"{id}-component-setting";
20+
private string templateName = $"{id}-template";
21+
private string datastreamName = $"{id}-datastream";
22+
private const string elasticsearchAddress = "http://172.18.103.104:9201";
23+
24+
private ElasticsearchClient client = null!;
25+
private ElasticsearchClientSettings settings = null!;
26+
27+
public class SimpleDocument
28+
{
29+
public string Code { get; set; } = null!;
30+
[JsonPropertyName("@timestamp")]
31+
public DateTimeOffset Time { get; set; }
32+
public double Value { get; set; }
33+
public List<string> Tags { get; set; } = new List<string>();
34+
}
35+
36+
[SetUp]
37+
public async Task Setup()
38+
{
39+
id = Guid.NewGuid();
40+
componentMappingName = $"{id}-component-mapping";
41+
componentSettingName = $"{id}-component-setting";
42+
templateName = $"{id}-template";
43+
datastreamName = $"{id}-datastream";
44+
45+
settings = new ElasticsearchClientSettings(new Uri(elasticsearchAddress))
46+
.ThrowExceptions(true)
47+
.DisableDirectStreaming();
48+
49+
client = new ElasticsearchClient(settings);
50+
51+
PutComponentTemplateResponse putMappingReponse = await client.Cluster.PutComponentTemplateAsync<SimpleDocument>(
52+
componentMappingName,
53+
a => a
54+
.Template(t => t
55+
.Mappings(m => m
56+
.Properties(p => p
57+
.Keyword(k => k.Code, c => c.TimeSeriesDimension())
58+
.DoubleNumber(k => k.Value, c => c.TimeSeriesMetric(TimeSeriesMetricType.Gauge))
59+
.DateNanos(k => k.Time, c => c.Format("strict_date_optional_time_nanos"))
60+
.Wildcard(n => n.Tags)
61+
)
62+
.Dynamic(DynamicMapping.True)
63+
)
64+
)
65+
);
66+
67+
PutComponentTemplateResponse componentSettingsTemplateResponse
68+
= await client.Cluster.PutComponentTemplateAsync<SimpleDocument>(
69+
componentSettingName,
70+
d => d
71+
.Template(tc => tc
72+
.Settings(s => s
73+
.Codec("best_compression")
74+
.NumberOfShards(3)
75+
.Index(i => i
76+
.Mode("time_series")
77+
.RoutingPath(new List<string> { "code" })
78+
)
79+
)
80+
)
81+
);
82+
83+
PutIndexTemplateResponse putTemplateReponse = await client.Indices.PutIndexTemplateAsync(
84+
templateName, rd => rd
85+
.ComposedOf(new List<Name> { componentMappingName, componentSettingName })
86+
.IndexPatterns(datastreamName)
87+
.DataStream(new DataStreamVisibility())
88+
);
89+
90+
CreateDataStreamResponse createDatastreamResponse = await client.Indices.CreateDataStreamAsync(datastreamName);
91+
}
92+
93+
[TestCase]
94+
public async Task can_insert_and_read_list_of_strings()
95+
{
96+
SimpleDocument doc = new SimpleDocument
97+
{
98+
Code = "1",
99+
Time = DateTimeOffset.UtcNow,
100+
Value = 1,
101+
Tags = new List<string> { "one_tag", "second_tag" }
102+
};
103+
104+
await client.IndexAsync(doc, index: datastreamName);
105+
await client.Indices.RefreshAsync();
106+
107+
var response = await client.SearchAsync<SimpleDocument>(datastreamName, r => r.Index(datastreamName)
108+
.Size(2)
109+
.Sort(s => s.Field(f => f.Time)));
110+
111+
response.Documents.Should().HaveCount(1);
112+
response.Documents.First().Tags.Should().HaveCount(2);
113+
}
114+
115+
[TestCase]
116+
public async Task can_insert_and_read_list_of_strings_with_a_single_element()
117+
{
118+
SimpleDocument doc = new SimpleDocument
119+
{
120+
Code = "2",
121+
Time = DateTimeOffset.UtcNow,
122+
Value = 2,
123+
Tags = new List<string> { "one_tag" }
124+
};
125+
126+
await client.IndexAsync(doc, index: datastreamName);
127+
await client.Indices.RefreshAsync();
128+
129+
var response = await client.SearchAsync<SimpleDocument>(datastreamName, r => r.Index(datastreamName)
130+
.Size(2)
131+
.Sort(s => s.Field(f => f.Time)));
132+
133+
response.Documents.Should().HaveCount(1);
134+
response.Documents.First().Tags.Should().HaveCount(1);
135+
}
136+
}

0 commit comments

Comments
 (0)