Skip to content

minhsangdotcom/elasticsearch-fluent-configuration

Repository files navigation

Els Fluent configurations

Fluent Configurations for elasticsearch in c#.

Example

Usage Example

public class AuditLogConfiguration : IElasticsearchDocumentConfigure<AuditLog>
{
    public void Configure(ref ElasticsearchConfigBuilder<AuditLog> builder, string? prefix = null)
    {
        // declare the name of index
        builder.ToIndex(prefix);

        // set key
        builder.HasKey(key => key.Id);

        // add settings
        builder.Settings(setting =>
            setting.Analysis(x =>
                x.Analyzers(an =>
                        an.Custom(
                                "myTokenizer",
                                ca => ca.Filter(["lowercase"]).Tokenizer("myTokenizer")
                            )
                            .Custom(
                                "standardAnalyzer",
                                ca => ca.Filter(["lowercase"]).Tokenizer("standard")
                            )
                    )
                    .Tokenizers(tz =>
                        tz.NGram(
                            "myTokenizer",
                            config =>
                                config
                                    .MinGram(3)
                                    .MaxGram(4)
                                    .TokenChars([TokenChar.Digit, TokenChar.Letter])
                        )
                    )
            )
        );

        // Map properties Manually
        builder.Properties(config =>
            config
                .Text(
                    t => t.Id,
                    config =>
                        config
                            .Fields(f =>
                                f.Keyword("raw")
                            )
                            .Analyzer("myTokenizer")
                            .SearchAnalyzer("standardAnalyzer")
                )
                .Text(
                    txt => txt.Entity,
                    config =>
                        config
                            .Fields(f =>
                                f.Keyword("raw")
                            )
                            .Analyzer("myTokenizer")
                            .SearchAnalyzer("standardAnalyzer")
                )
                .ByteNumber(b => b.Type)
                .Object(o => o.OldValue!)
                .Object(o => o.NewValue!)
                .Text(txt => txt.ActionPerformBy!)
                .Keyword(d => d.CreatedAt)
        );

        // Ignore properties
        builder.Ignores(x => x.NewValue!, x => x.Type);
    }
}

Register

ElasticsearchSettings elasticsearch =
    configuration.GetSection(nameof(ElasticsearchSettings)).Get<ElasticsearchSettings>()
            ?? new();

    if (elasticsearch.IsEnabled)
    {
        IEnumerable<Uri> nodes = elasticsearch!.Nodes.Select(x => new Uri(x));
        var pool = new StaticNodePool(nodes);
        string? userName = elasticsearch.Username;
        string? password = elasticsearch.Password;

        var settings = new ElasticsearchClientSettings(pool).DefaultIndex(
            elasticsearch.DefaultIndex!
        );

        if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password))
        {
            settings
                .Authentication(new BasicAuthentication(userName, password))
                // without ssl trust
                .ServerCertificateValidationCallback((o, certificate, chain, errors) => true)
                .ServerCertificateValidationCallback(CertificateValidations.AllowAll);
        }

        List<ElasticConfigureResult> elkConfigBuilder =
        [
            .. ElasticsearchRegisterHelper.GetElasticsearchConfigBuilder(
                Assembly.GetExecutingAssembly(),
                settings.PrefixIndex.ToKebabCase()
            ),
        ]

        // add configurations of id, ignore properties
        ElasticsearchRegisterHelper.ConfigureConnectionSettings(ref settings, elkConfigBuilder);

        var client = new ElasticsearchClient(settings);

        // add configuration of properties
        await client.ElasticFluentConfigAsync(configuration.Configurations);
        await DataSeeding.SeedingAsync(client, elasticsearch.PrefixIndex);

        services
            .AddSingleton(client)
            .AddSingleton<IElasticsearchServiceFactory, ElasticsearchServiceFactory>();
    }
dotnet add package minhsangdotcom.TheTemplate.ElasticsearchFluentConfig

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages