|
| 1 | +Fluent Configurations for elasticsearch in c#. |
| 2 | + |
| 3 | +Let check the source code out at [Github](https://github.com/minhsangdotcom/elasticsearch-fluent-configuration) |
| 4 | + |
| 5 | +Check out My clean architecture solution template at [Github](https://github.com/minhsangdotcom/Clean-Architecture_The-Template) |
| 6 | + |
| 7 | +Work well with [Elastic.Clients.Elasticsearch](https://www.nuget.org/packages/Elastic.Clients.Elasticsearch) package |
| 8 | + |
| 9 | +# Usage Example |
| 10 | + |
| 11 | +```csharp |
| 12 | +public class AuditLogConfiguration : IElasticsearchDocumentConfigure<AuditLog> |
| 13 | +{ |
| 14 | + public void Configure(ref ElasticsearchConfigBuilder<AuditLog> buider, string? prefix = null) |
| 15 | + { |
| 16 | + // declare the name of index |
| 17 | + buider.ToIndex("audit_log"); |
| 18 | + |
| 19 | + // set key |
| 20 | + buider.HasKey(key => key.Id); |
| 21 | + |
| 22 | + // add settings |
| 23 | + buider.Settings(setting => |
| 24 | + setting.Analysis(x => |
| 25 | + x.Analyzers(an => |
| 26 | + an.Custom( |
| 27 | + "myTokenizer", |
| 28 | + ca => ca.Filter(["lowercase"]).Tokenizer("myTokenizer") |
| 29 | + ) |
| 30 | + .Custom( |
| 31 | + "standardAnalyzer", |
| 32 | + ca => ca.Filter(["lowercase"]).Tokenizer("standard") |
| 33 | + ) |
| 34 | + ) |
| 35 | + .Tokenizers(tz => |
| 36 | + tz.NGram( |
| 37 | + "myTokenizer", |
| 38 | + config => |
| 39 | + config |
| 40 | + .MinGram(3) |
| 41 | + .MaxGram(4) |
| 42 | + .TokenChars([TokenChar.Digit, TokenChar.Letter]) |
| 43 | + ) |
| 44 | + ) |
| 45 | + ) |
| 46 | + ); |
| 47 | + |
| 48 | + // Map properties Manually |
| 49 | + buider.Properties(config => |
| 50 | + config |
| 51 | + .Text( |
| 52 | + t => t.Id, |
| 53 | + config => |
| 54 | + config |
| 55 | + .Fields(f => |
| 56 | + f.Keyword("Id") |
| 57 | + ) |
| 58 | + .Analyzer("myTokenizer") |
| 59 | + .SearchAnalyzer("standardAnalyzer") |
| 60 | + ) |
| 61 | + .Text( |
| 62 | + txt => txt.Entity, |
| 63 | + config => |
| 64 | + config |
| 65 | + .Fields(f => |
| 66 | + f.Keyword("Entity") |
| 67 | + ) |
| 68 | + .Analyzer("myTokenizer") |
| 69 | + .SearchAnalyzer("standardAnalyzer") |
| 70 | + ) |
| 71 | + .ByteNumber(b => b.Type) |
| 72 | + .Object(o => o.OldValue!) |
| 73 | + .Object(o => o.NewValue!) |
| 74 | + .Text(txt => txt.ActionPerformBy!) |
| 75 | + .Keyword(d => d.CreatedAt) |
| 76 | + ); |
| 77 | + |
| 78 | + // Ignore properties |
| 79 | + buider.Ignores([x => x.NewValue!, x => x.Type]); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +# Register |
| 85 | + |
| 86 | +```csharp |
| 87 | +ElasticsearchSettings elasticsearch = |
| 88 | + configuration.GetSection(nameof(ElasticsearchSettings)).Get<ElasticsearchSettings>() |
| 89 | + ?? new(); |
| 90 | + |
| 91 | + if (elasticsearch.IsEnbaled) |
| 92 | + { |
| 93 | + IEnumerable<Uri> nodes = elasticsearch!.Nodes.Select(x => new Uri(x)); |
| 94 | + var pool = new StaticNodePool(nodes); |
| 95 | + string? userName = elasticsearch.Username; |
| 96 | + string? password = elasticsearch.Password; |
| 97 | + |
| 98 | + var settings = new ElasticsearchClientSettings(pool).DefaultIndex( |
| 99 | + elasticsearch.DefaultIndex! |
| 100 | + ); |
| 101 | + |
| 102 | + if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password)) |
| 103 | + { |
| 104 | + settings |
| 105 | + .Authentication(new BasicAuthentication(userName, password)) |
| 106 | + // without ssl trust |
| 107 | + .ServerCertificateValidationCallback((o, certificate, chain, errors) => true) |
| 108 | + .ServerCertificateValidationCallback(CertificateValidations.AllowAll); |
| 109 | + } |
| 110 | + |
| 111 | + IEnumerable<ElasticConfigureResult> elkConfigbuilder = |
| 112 | + ElasticsearchRegisterHelper.GetElasticsearchConfigBuilder( |
| 113 | + Assembly.GetExecutingAssembly(), |
| 114 | + elasticsearch.PrefixIndex! |
| 115 | + ); |
| 116 | + |
| 117 | + // add configurations of id, ignore properties |
| 118 | + ElasticsearchRegisterHelper.ConfigureConnectionSettings(ref settings, elkConfigbuilder); |
| 119 | + |
| 120 | + var client = new ElasticsearchClient(settings); |
| 121 | + |
| 122 | + // add configuration of properties |
| 123 | + await ElasticsearchRegisterHelper.ElasticFluentConfigAsync( |
| 124 | + elasticsearchClient, |
| 125 | + configures |
| 126 | + ); |
| 127 | + |
| 128 | + await DataSeeding.SeedingAsync(client, elasticsearch.PrefixIndex) |
| 129 | + |
| 130 | + services |
| 131 | + .AddSingleton(client) |
| 132 | + .AddSingleton<IElasticsearchServiceFactory, ElasticsearchServiceFactory>(); |
| 133 | + } |
| 134 | +``` |
0 commit comments