|
| 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 | +} |
0 commit comments