示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            #region Elastic
            var settings = new ConnectionSettings(new Uri(Configuration["ElasticsearchSettings:uri"]));

            var defaultIndex = Configuration["ElasticsearchSettings:defaultIndex"];

            if (!string.IsNullOrEmpty(defaultIndex))
            {
                settings = settings.DefaultIndex(defaultIndex);
            }

            var basicAuthUser     = Configuration["ElasticsearchSettings:username"];
            var basicAuthPassword = Configuration["ElasticsearchSettings:password"];

            if (!string.IsNullOrEmpty(basicAuthUser) && !string.IsNullOrEmpty(basicAuthPassword))
            {
                settings = settings.BasicAuthentication(basicAuthUser, basicAuthPassword);
            }

            var client = new ElasticClient(settings);

            services.AddSingleton <IElasticClient>(client);
            #endregion

            services.AddTransient <ICatalogApplication, CatalogApplication>();
            services.AddControllers();
        }
示例#2
0
        private ElasticClient GetClient(string[] urls, string defaultIndex = "")
        {
            if (urls == null || urls.Length < 1)
            {
                throw new ArgumentNullException(nameof(urls));
            }

            var uris              = urls.Select(x => new Uri(x)).ToArray();
            var connectionPool    = new SniffingConnectionPool(uris);
            var connectionSetting = new ConnectionSettings(connectionPool);

            if (defaultIndex.IsNullOrWhiteSpace())
            {
                connectionSetting.DefaultIndex(defaultIndex);
            }

            var uname = config.Value.UserName;
            var upwd  = config.Value.Password;

            if (!uname.IsNullOrWhiteSpace() && !upwd.IsNullOrWhiteSpace())
            {
                connectionSetting.BasicAuthentication(uname, upwd);
            }
            return(new ElasticClient(connectionSetting));
        }
示例#3
0
        public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
        {
            var settings = new ConnectionSettings(new Uri(configuration["ElasticsearchSettings:uri"]));

            var defaultIndex = configuration["ElasticsearchSettings:defaultIndex"];

            if (!string.IsNullOrEmpty(defaultIndex))
            {
                settings = settings.DefaultIndex(defaultIndex);
            }

            // The authentication options below are set if you have non-null/empty
            // settings in the configuration.
            var apiKeyId = configuration["ElasticsearchSettings:apiKeyId"];
            var apiKey   = configuration["ElasticsearchSettings:apiKey"];

            if (!string.IsNullOrEmpty(apiKeyId) && !string.IsNullOrEmpty(apiKey))
            {
                settings = settings.ApiKeyAuthentication(apiKeyId, apiKey);
            }
            else
            {
                var basicAuthUser     = configuration["ElasticsearchSettings:basicAuthUser"];
                var basicAuthPassword = configuration["ElasticsearchSettings:basicAuthPassword"];

                if (!string.IsNullOrEmpty(basicAuthUser) && !string.IsNullOrEmpty(basicAuthPassword))
                {
                    settings = settings.BasicAuthentication(basicAuthUser, basicAuthPassword);
                }
            }

            var client = new ElasticClient(settings);

            services.AddSingleton <IElasticClient>(client); // It's Thread safe
        }
示例#4
0
        public IElasticClient GetInstance()
        {
            // Get the ElasticSearch credentials.
            string username = _config.Userid;
            string password = _config.Password;

            //Get the ElasticSearch servers that we will be connecting to.
            List <Uri> uris = GetServerUriList();

            // Create the connection pool, the SniffingConnectionPool will
            // keep tabs on the health of the servers in the cluster and
            // probe them to ensure they are healthy.  This is how we handle
            // redundancy and load balancing.
            var connectionPool = new SniffingConnectionPool(uris);

            //Return a new instance of an ElasticClient with our settings
            ConnectionSettings settings = new ConnectionSettings(connectionPool)
                                          .MaximumRetries(_config.MaximumRetries);


            //Let's only try and use credentials if the username is set.
            if (!string.IsNullOrWhiteSpace(username))
            {
                settings.BasicAuthentication(username, password);
            }

            return(new ElasticClient(settings));
        }
示例#5
0
        private ElasticClient GetClient()
        {
            if (_client == null)
            {
                _logger.LogDebug("Create elastic client");
                var settings = new ConnectionSettings(new Uri(_options.Url)).DisableDirectStreaming()
                               .OnRequestCompleted(details =>
                {
                    if (_options.EnableClientLogging)
                    {
                        _logger.LogDebug("### ES REQEUST ###");
                        if (details.RequestBodyInBytes != null)
                        {
                            _logger.LogDebug("{Request}", Encoding.UTF8.GetString(details.RequestBodyInBytes));
                        }
                        _logger.LogDebug("### ES RESPONSE ###");
                        if (details.ResponseBodyInBytes != null)
                        {
                            _logger.LogDebug("{Response}", Encoding.UTF8.GetString(details.ResponseBodyInBytes));
                        }
                    }
                })
                               .PrettyJson();
                if (!string.IsNullOrEmpty(_options.Login))
                {
                    settings.BasicAuthentication(_options.Login, _options.Password);
                }

                settings.ServerCertificateValidationCallback((_, _, _, _) => true);
                _client = new ElasticClient(settings);
            }

            return(_client);
        }
示例#6
0
        public IElasticClient EsClient()
        {
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Environment.ContentRootPath)
                                .AddJsonFile("appsettings.json")
                                .Build();
            var settings = new ConnectionSettings(new Uri(configuration["ElasticsearchSettings:uri"]));

            var defaultIndex = configuration["ElasticsearchSettings:defaultIndex"];

            if (!string.IsNullOrEmpty(defaultIndex))
            {
                settings = settings.DefaultIndex(defaultIndex);
            }

            var basicAuthUser     = configuration["ElasticsearchSettings:username"];
            var basicAuthPassword = configuration["ElasticsearchSettings:password"];

            if (!string.IsNullOrEmpty(basicAuthUser) && !string.IsNullOrEmpty(basicAuthPassword))
            {
                settings = settings.BasicAuthentication(basicAuthUser, basicAuthPassword);
            }

            var client = new ElasticClient(settings);

            return(client);
        }
    public static IServiceCollection AddIndexingServices(this IServiceCollection services, IConfiguration config, IConnectionMultiplexer connection)
    {
        services.Configure <SearchRedisOptions>(config);
        services.TryAddSingleton <IConnectionMultiplexer>(connection);
        services.Configure <SearchOptions>(config.GetSection("search"));
        services.AddSingleton <IIndexingClient, IndexingClient>();

        services.AddSingleton <SlowEntryIndexProvider>();
        services.AddSingleton <IIndexProvider <EntryItem> >(x => x.GetRequiredService <SlowEntryIndexProvider>());
        services.AddSingleton <ISingleEntryIndexProvider>(x => x.GetRequiredService <SlowEntryIndexProvider>());
        services.AddSingleton <IVerifiedEntryIndexProvider>(x => x.GetRequiredService <SlowEntryIndexProvider>());

        services.AddSingleton <IIndexProvider <LeagueItem>, LeagueIndexProvider>();
        services.AddSingleton <IIndexingService, IndexingService>();
        services.AddSingleton <ILeagueIndexBookmarkProvider, LeagueIndexRedisBookmarkProvider>();
        services.AddSingleton <IEntryIndexBookmarkProvider, EntryIndexRedisBookmarkProvider>();
        services.AddSingleton <IElasticClient>(provider =>
        {
            var searchOpts    = provider.GetService <IOptions <SearchOptions> >();
            var searchOptions = searchOpts.Value;
            searchOptions.Validate();
            var connectionSettings = new ConnectionSettings(new Uri(searchOptions.IndexUri));
            connectionSettings.BasicAuthentication(searchOptions.Username, searchOptions.Password);
            return(new ElasticClient(connectionSettings));
        });
        services.AddMediatR(typeof(IndexEntryCommandHandler));

        return(services);
    }
示例#8
0
        public EsClientProvider(IOptions <ElasticSearchOptions> options)
        {
            var pool = new SingleNodeConnectionPool(new Uri(options.Value.Uri));
            var connectionSettings = new ConnectionSettings(pool, sourceSerializer: (builtin, settings) => new JsonNetSerializer(builtin, settings, () => new JsonSerializerSettings
            {
                NullValueHandling     = NullValueHandling.Include,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatHandling    = DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling  = DateTimeZoneHandling.Utc
            }, resolver => resolver.NamingStrategy = new CamelCaseNamingStrategy()
                ));

            // .DefaultMappingFor<AccountDto>(m => m.IndexName(PrefixIndex + "-" + nameof(AccountDto).ToLower()))
            // .DefaultMappingFor<CategoryDto>(m => m.IndexName(PrefixIndex + "-" + nameof(CategoryDto).ToLower()))
            // .DefaultIndex(PrefixIndex);
            if (!string.IsNullOrEmpty(options.Value.UserName) && !string.IsNullOrEmpty(options.Value.Password))
            {
                connectionSettings.BasicAuthentication(options.Value.UserName, options.Value.Password);
            }

            connectionSettings.DisableDirectStreaming();
            Client = new ElasticClient(connectionSettings);

            PrefixIndex = options.Value.PrefixIndex;
            CreateIndex().Wait();
        }
示例#9
0
 private void SetDefaultSettings(ConnectionSettings settings)
 {
     if (_indexerSettings.Elk5Enabled)
     {
         settings.BasicAuthentication(_indexerSettings.ElasticUsername, _indexerSettings.ElasticPassword);
     }
 }
示例#10
0
        public static int Main()
        {
            ElasticsearchVersion v = null;


//			var clusterConfiguration = new EphemeralClusterConfiguration("6.0.0", numberOfNodes: 2);
//			var ephemeralCluster = new EphemeralCluster(clusterConfiguration);
//			var nodeConfiguration = new NodeConfiguration(clusterConfiguration, 9200);
//			var elasticsearchNode = new ElasticsearchNode(nodeConfiguration);
////

//			using (var node = new ElasticsearchNode("5.5.1"))
//			{
//				node.Subscribe(new ConsoleOutColorWriter());
//				node.WaitForStarted(TimeSpan.FromMinutes(2));
//			}
//
//			using (var node = new ElasticsearchNode("6.0.0-beta2", @"c:\Data\elasticsearch-6.0.0-beta2"))
//			{
//				node.Subscribe();
//				node.WaitForStarted(TimeSpan.FromMinutes(2));
//				Console.ReadKey();
//			}

//			using (var cluster = new EphemeralCluster("6.0.0"))
//			{
//				cluster.Start();
//			}

            var config = new EphemeralClusterConfiguration("6.2.3", XPack | Security | SSL, null, 1)
            {
                PrintYamlFilesInConfigFolder        = true,
                ShowElasticsearchOutputAfterStarted = true
            };

            using (var cluster = new EphemeralCluster(config))
            {
                cluster.Start();

                var nodes          = cluster.NodesUris();
                var connectionPool = new StaticConnectionPool(nodes);
                var settings       = new ConnectionSettings(connectionPool).EnableDebugMode();
                if (config.EnableSecurity && !config.EnableSsl)
                {
                    settings = settings.BasicAuthentication(ClusterAuthentication.Admin.Username, ClusterAuthentication.Admin.Password);
                }
                if (config.EnableSsl)
                {
                    settings = settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll);
                    settings = settings.ClientCertificate(new X509Certificate2(config.FileSystem.ClientCertificate));
                }

                var client = new ElasticClient(settings);

                Console.Write(client.XPackInfo().DebugInformation);
            }
//
//			Console.WriteLine($".. DONE ...");
            return(0);
        }
示例#11
0
        protected ElasticClient GetDb(string index)
        {
            if (TypeTable.Count == 0)
            {
                throw new Exception("Elastic Search Mapping Is Not Defined Call The Init Method First");
            }
            this.DefaultIndex = $"{index.ToLower()}";
            var uriString = this.nodeUrl;
            var node      = new Uri(uriString);
            var settings  = new ConnectionSettings(node);

            settings.DefaultIndex(DefaultIndex);
            var userName = this.userName;

            if (!string.IsNullOrEmpty(userName))
            {
                settings.
                BasicAuthentication(userName, this.password);
            }
#if DEBUG
            settings.EnableDebugMode();
#endif
            var client = new ElasticClient(settings);
            return(_elasticClient = client);
        }
示例#12
0
文件: Program.cs 项目: whhub/CSharp
        private static void IndexAccounts(IEnumerable <Account> accounts)
        {
            // Connect Elasticsearch
            // TODO: Console Usage
            var node        = new Uri("http://10.6.14.157:9200");
            var elasticUser = "******";
            var elasticPwd  = "123qwe";
            var setting     = new ConnectionSettings(node);

            setting.BasicAuthentication(elasticUser, elasticPwd);
            var elasticSearchClient = new ElasticClient(setting);


            // Index Mapping
            var descriptor = new CreateIndexDescriptor("account")
                             .Mappings(ms => ms.Map <Account>(m => m.AutoMap()));

            elasticSearchClient.CreateIndex(descriptor);

            foreach (var account in accounts)
            {
                var respose = elasticSearchClient.Index(account, idx => idx.Index("account"));
                Console.WriteLine("Indexed an account with respose : {0}", respose.Result);
            }
        }
示例#13
0
        static ValidationResult CanConnectServer(ElasticsearchSource source)
        {
            try
            {
                var uri          = new Uri(source.HostName + ":" + source.Port);
                var isValid      = false;
                var errorMessage = "";
                var settings     = new ConnectionSettings(uri).RequestTimeout(TimeSpan.FromMinutes(2));
                if (source.AuthenticationType == AuthenticationType.Password)
                {
                    settings.BasicAuthentication(source.Username, source.Password);
                }

                var client = new ElasticClient(settings);
                var result = client.Ping();
                isValid = result.IsValid;
                if (!isValid)
                {
                    errorMessage = "could not connect to elasticsearch Instance";
                }
                return(new ValidationResult
                {
                    IsValid = isValid,
                    ErrorMessage = errorMessage
                });
            }
            catch (Exception e)
            {
                return(new ValidationResult
                {
                    IsValid = false,
                    ErrorMessage = e.InnerException != null?string.Join(Environment.NewLine, e.Message, e.InnerException.Message) : e.Message
                });
            }
        }
示例#14
0
        protected override void Execute(CodeActivityContext context)
        {
            var settings = new ConnectionSettings(new Uri(URL.Get(context)));

            settings.ThrowExceptions(alwaysThrow: true);
            settings.PrettyJson();

            if (AuthenticationRequired.Get(context) == true)
            {
                settings.BasicAuthentication(Username.Get(context), Password.Get(context));
            }


            var esClient   = new ElasticClient(settings);
            var searchData = esClient.Search <UiPathESLog>(sd => sd
                                                           .Index(Index.Get(context))
                                                           .Size(MaxSize.Get(context))
                                                           .Query(q => q.
                                                                  Match(m => m
                                                                        .Field(f => f.processName)
                                                                        .Query(ProcessName.Get(context) == string.Empty ? "*" : ProcessName.Get(context))) &&
                                                                  q.
                                                                  Match(m => m
                                                                        .Field(f => f.robotName)
                                                                        .Query(RobotName.Get(context) == string.Empty ? "*" : RobotName.Get(context))) &&
                                                                  q
                                                                  .DateRange(r => r
                                                                             .Field(f => f.timeStamp)
                                                                             .GreaterThanOrEquals(StartTime.Get(context))
                                                                             .LessThanOrEquals(EndTime.Get(context)))));

            Logs.Set(context, searchData.Documents.ToArray());
        }
示例#15
0
        public ElasticAuditorProvider(ElasticSetting elasticSetting)
        {
            _elasticSetting = elasticSetting;

            _node     = new Uri(_elasticSetting.ConnectionString);
            _settings = new ConnectionSettings(_node);
            _client   = new ElasticClient(_settings);

            if (!string.IsNullOrEmpty(_elasticSetting.Username) && !string.IsNullOrEmpty(_elasticSetting.Password))
            {
                _settings.BasicAuthentication(_elasticSetting.Username, _elasticSetting.Password);
                _settings.ServerCertificateValidationCallback(
                    (o, certificate, arg3, arg4) => true);
            }

            var indexSettings = new IndexSettings {
                NumberOfReplicas = 1, NumberOfShards = 5
            };
            var indexConfig = new IndexState {
                Settings = indexSettings
            };

            ReformatIndex();

            _indexName = string.Format(_elasticSetting.IndexFormat, DateTime.Now.Date.ToString("yyyy.MM.dd"));

            if (!_client.IndexExists(_indexName).Exists)
            {
                _client.CreateIndex(_indexName, c => c
                                    .InitializeUsing(indexConfig)
                                    .Mappings(m => m.Map <Audit>(mp => mp.AutoMap(typeof(Audit)).AutoMap <Audit>())));
            }
        }
        public ElasticRepository(AppSettings appSettings)
        {
            var _elasticConfig = appSettings.ElasticSearchConfig;

            List <Uri> hosts = new List <Uri>();

            foreach (var host in _elasticConfig.HostAddresses.Split(","))
            {
                hosts.Add(new Uri(host));
            }
            IConnectionPool connectionPool;

            if (hosts.Count == 1)
            {
                connectionPool = new StaticConnectionPool(hosts);
            }
            else
            {
                connectionPool = new SingleNodeConnectionPool(hosts.First());
            }

            var settings = new ConnectionSettings(connectionPool).DisablePing();

            if (_elasticConfig.EnableAuthentication)
            {
                settings = settings.BasicAuthentication(_elasticConfig.Username, _elasticConfig.Password);
            }

            if (_elasticConfig.EnableDebugMode)
            {
                settings = settings.EnableDebugMode();
            }

            _elasticClient = new ElasticClient(settings);
        }
示例#17
0
        public ESClientProvider(IOptions <ESClientProviderConfig> options)
        {
            var settings = new ConnectionSettings(new Uri(options.Value.Uri))
                           .InferMappingFor <LogEntry>(i => i
                                                       .IndexName(options.Value.DefaultIndex)
                                                       .TypeName(options.Value.DefaultType)
                                                       );

            if (!string.IsNullOrEmpty(options.Value.UserName) && !string.IsNullOrEmpty(options.Value.Password))
            {
                settings.BasicAuthentication(options.Value.UserName, options.Value.Password);
            }

            Client       = new ElasticClient(settings);
            DefaultIndex = options.Value.DefaultIndex;

            if (Enum.TryParse(options.Value.LogLevel, out Microsoft.Extensions.Logging.LogLevel level))
            {
                LogLevel = level;
            }
            else
            {
                LogLevel = Microsoft.Extensions.Logging.LogLevel.Warning;
            }
        }
    public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
    {
        var settings = new ConnectionSettings(new Uri(configuration["ElasticsearchSettings:uri"]));

        var defaultIndex = configuration["ElasticsearchSettings:defaultIndex"];

        if (!string.IsNullOrEmpty(defaultIndex))
        {
            settings = settings.DefaultIndex(defaultIndex);
        }

        var basicAuthUser     = configuration["ElasticsearchSettings:username"];
        var basicAuthPassword = configuration["ElasticsearchSettings:password"];

        if (!string.IsNullOrEmpty(basicAuthUser) && !string.IsNullOrEmpty(basicAuthPassword))
        {
            settings = settings.BasicAuthentication(basicAuthUser, basicAuthPassword);
        }

        var client = new ElasticClient(settings);

        //https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/lifetimes.html
        services.AddSingleton <IElasticClient>(client);

        //https://www.elastic.co/guide/en/apm/agent/dotnet/current/config-http.html#config-capture-body
        services.Configure <KestrelServerOptions>(options => options.AllowSynchronousIO = true);
        services.Configure <IISServerOptions>(options => options.AllowSynchronousIO     = true);
    }
示例#19
0
        public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
        {
            var settings = new ConnectionSettings(new Uri(configuration["ElasticsearchSettings:uri"]));

            var defaultIndex = configuration["ElasticsearchSettings:defaultIndex"];

            if (!string.IsNullOrEmpty(defaultIndex))
            {
                settings = settings.DefaultIndex(defaultIndex);
            }

            var basicAuthUser     = configuration["ElasticsearchSettings:username"];
            var basicAuthPassword = configuration["ElasticsearchSettings:password"];

            if (!string.IsNullOrEmpty(basicAuthUser) && !string.IsNullOrEmpty(basicAuthPassword))
            {
                settings = settings.BasicAuthentication(basicAuthUser, basicAuthPassword);
            }
            settings.EnableDebugMode();
            settings.DisableDirectStreaming();

            settings.OnRequestCompleted(call =>
            {
                Debug.Write(Encoding.UTF8.GetString(call.RequestBodyInBytes));
            });

            var client = new ElasticClient(settings);

            services.AddSingleton <IElasticClient>(client);
        }
示例#20
0
        public static void AddElasticSearch(this IServiceCollection services, IConfigurationRoot configuration)
        {
            var uri      = new Uri(configuration["ElasticSearchConfig:Url"]);
            var username = configuration["ElasticSearchConfig:Username"];
            var password = configuration["ElasticSearchConfig:Password"];

            var settings = new ConnectionSettings(uri);

            settings.BasicAuthentication(username, password);
            settings.DisableDirectStreaming();
            settings.OnRequestCompleted(call =>
            {
                Debug.WriteLine("Endpoint Called: " + call.Uri);

                if (call.RequestBodyInBytes != null)
                {
                    Debug.WriteLine("Request Body: " + Encoding.UTF8.GetString(call.RequestBodyInBytes));
                }

                if (call.ResponseBodyInBytes != null)
                {
                    Debug.WriteLine("Response Body: " + Encoding.UTF8.GetString(call.ResponseBodyInBytes));
                }

                if (call.ResponseMimeType != null)
                {
                    Debug.WriteLine("Response Mime: " + call.ResponseMimeType);
                }
            });

            var client = new ElasticClient(settings);

            services.AddSingleton <IElasticClient>(client);
        }
示例#21
0
        public static IElasticClient GetElasticClient(Logger logger)
        {
            try
            {
                // Initialise the ElasticClient
                var node       = new Uri(IndexHostName);
                var pool       = new SingleNodeConnectionPool(node);
                var connection = new HttpConnection();

                var settings = new ConnectionSettings(pool, connection,
                                                      sourceSerializer: (builtin, set) => new JsonNetSerializer(builtin, set,
                                                                                                                () => new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Include,
                    TypeNameHandling  = TypeNameHandling.Auto
                }));

                settings.EnableTcpKeepAlive(
                    TimeSpan.FromMilliseconds(IndexKeepAliveTime),
                    TimeSpan.FromMilliseconds(IndexKeepAliveInterval));

                //settings.EnableDebugMode();

                if (IndexTraceEnabled)
                {
                    settings = settings.DisableDirectStreaming()
                               .OnRequestCompleted(details =>
                    {
                        if (details.RequestBodyInBytes != null)
                        {
                            logger.Info($"Elastic Search request for index: {IndexHostName}." + details.RequestBodyInBytes);
                        }

                        if (details.ResponseBodyInBytes != null)
                        {
                            logger.Info($"Elastic Search response for index: {IndexHostName}." + details.ResponseBodyInBytes);
                        }

                        if (details.DebugInformation != null)
                        {
                            logger.Info($"Elastic Search debug infromation response for index: {IndexHostName}." + details.DebugInformation);
                        }
                    });
                }

                if (IndexUntrustedSslCert &&
                    ServicePointManager.ServerCertificateValidationCallback == null)
                {
                    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, errors) => true;
                }
                settings = settings.BasicAuthentication(IndexUsername, IndexPassword);

                return(new ElasticClient(settings));
            }
            catch (Exception ex)
            {
                logger.Error("Connection Exception" + ex);
            }
            return(null);
        }
        private void EnsureConnectionOpen()
        {
            if (_client == null)
            {
                var uri            = ConnectionStringName.GetConnectionString() ?? Uri;
                var nodes          = uri.Render(new LogEventInfo()).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(url => new Uri(url));
                var connectionPool = new StaticConnectionPool(nodes);

                var config =
                    new ConnectionSettings(connectionPool, sourceSerializer: (builtin, settings) => new JsonNetSerializer(
                                               builtin, settings,
                                               () => new JsonSerializerSettings
                {
                    NullValueHandling     = NullValueHandling.Include,
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                },
                                               resolver => resolver.NamingStrategy = new SnakeCaseNamingStrategy()
                                               ));

                if (RequireAuth)
                {
                    config.BasicAuthentication(Username, Password);
                }

                if (DisableAutomaticProxyDetection)
                {
                    config.DisableAutomaticProxyDetection();
                }

                _client = new ElasticLowLevelClient(config);
            }
        }
        public ElasticsearchMonitoringProvider(string uri, string indexName, string username,
                                               string password, bool recreateIndex, ILogger logger)
        {
            var connectionSettings = new ConnectionSettings(new Uri(uri)).DefaultIndex(indexName).ThrowExceptions();

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                connectionSettings = connectionSettings.BasicAuthentication(username, password);
            }

            elasticClient = new ElasticClient(connectionSettings);

            if (recreateIndex)
            {
                logger.LogInformation($"Recreating index {indexName}");
                if (elasticClient.IndexExists(indexName).Exists)
                {
                    elasticClient.DeleteIndex(indexName);
                }
            }

            if (!elasticClient.IndexExists(indexName).Exists)
            {
                var descriptor = new CreateIndexDescriptor(indexName)
                                 .Mappings(ms => ms
                                           .Map <ReceivedMessage>(m => m.AutoMap())
                                           .Map <SentMessage>(m => m.AutoMap())
                                           );

                elasticClient.CreateIndex(indexName, d => descriptor);
            }
        }
示例#24
0
        /// <summary>
        /// Get the ElasticSearch client connection and initialize the index if necessary.
        /// </summary>
        private IElasticClient GetElasticClient(string connectionString, IDictionary config)
        {
            var esClusterConfig = _connectionConfiguration.Parse(connectionString);

            // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
            if (esClusterConfig == null)
            {
                //connection string is supplied in a deprecated format
                #pragma warning disable 618
                esClusterConfig = _connectionConfiguration.BuildClusterConfigDeprecated(config, connectionString);
                #pragma warning restore 618
            }

            var connectionPool = new StaticConnectionPool(esClusterConfig.NodeUris);
            var conSettings    = new ConnectionSettings(connectionPool);
            conSettings.DefaultIndex(esClusterConfig.DefaultIndex);
            //conSettings.DisableDirectStreaming();//This should only be used for debugging, it will slow things down

            // set basic auth if username and password are provided in config string.
            if (!string.IsNullOrWhiteSpace(esClusterConfig.Username) && !string.IsNullOrWhiteSpace(esClusterConfig.Password))
            {
                conSettings.BasicAuthentication(esClusterConfig.Username, esClusterConfig.Password);
            }

            var esClient            = new ElasticClient(conSettings);
            var indexExistsResponse = esClient.IndexExists(new IndexExistsRequest(esClusterConfig.DefaultIndex)).VerifySuccessfulResponse();
            if (!indexExistsResponse.Exists)
            {
                CreateIndexWithMapping(esClient, esClusterConfig.DefaultIndex);
            }
            return(esClient);
        }
示例#25
0
        // PUT: api/elasticDemo/5
        public void Put(int id, [FromBody] string value)
        {
            // Connect tới database ở cloud
            var connectionSettings = new ConnectionSettings(new Uri("https://14cd3e6bc2a32141a8adde7f202b630e.us-east-1.aws.found.io:9243"));

            // Đây là username/password của cloud đó.
            connectionSettings.BasicAuthentication("elastic", "SzdD4h7CfuNiL9HvMmyJ4l7z");
            // Connect
            var elasticClient = new ElasticClient(connectionSettings);

            // Query search cho index có dạng:
            // account / user / 1
            // {
            //      "user" : "tri",
            //      "post_date" : "2017-03/11",
            //      "message" : "Hi, first user"
            // }
            var searchResponse = elasticClient.Search <tweet>(sd => sd
                                                              .Index("account")                               // index dòng 51
                                                              .Type("user")                                   // type dòng 52
                                                              .Size(10000)                                    // số lượng lấy
                                                              .Query(q => q
                                                                     .Match(m => m.Field("user").Query("tri") // kết quả sẽ match với field "user" có giá trị "tri"
                                                                            )));

            //return searchResponse;
        }
示例#26
0
        static void Main(string[] args)
        {
            var nodes = new Uri[]
            {
                new Uri("https://elasticsearch01:9200"),
                new Uri("https://elasticsearch01:9200"),
                new Uri("https://elasticsearch01:9200")
            };

            var pool     = new StaticConnectionPool(nodes);
            var settings = new ConnectionSettings(pool);

            settings.BasicAuthentication("es_user", "test1");
            var idx = "testindex-" + DateTime.Now.ToString("yyyy.MM.dd");

            settings.DefaultIndex(idx);
            var client  = new ElasticClient(settings);
            var request = new IndexExistsRequest(idx);
            var result  = client.IndexExists(request);

            if (!result.Exists)
            {
                var taskCreate = client.CreateIndexAsync(idx).GetAwaiter();
                while (!taskCreate.IsCompleted)
                {
                    Console.WriteLine(taskCreate.IsCompleted);
                }
            }
            Console.ReadLine();
        }
示例#27
0
        /// <summary>
        /// Common connection settings
        /// </summary>
        /// <returns></returns>
        protected virtual ConnectionSettings CommonSettings()
        {
            var url   = _vulcanApplicationSettings.Url;
            var index = _vulcanApplicationSettings.IndexNamePrefix;

            if (string.IsNullOrWhiteSpace(url) || url == "SET THIS")
            {
                throw new Exception("You need to specify the Vulcan Url in AppSettings");
            }

            if (string.IsNullOrWhiteSpace(index) || index == "SET THIS")
            {
                throw new Exception("You need to specify the Vulcan Index in AppSettings");
            }

            var connectionPool = _vulcanConnectionpoolFactory.CreateConnectionPool(url);
            var settings       = new ConnectionSettings(connectionPool, CreateJsonSerializer);
            var username       = _vulcanApplicationSettings.Username;
            var password       = _vulcanApplicationSettings.Password;

            if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
            {
                settings.BasicAuthentication(username, password);
            }

            // Enable bytes to be retrieved in debug mode
            settings.DisableDirectStreaming(_vulcanApplicationSettings.IsDebugMode);

            // Enable compression
            settings.EnableHttpCompression(_vulcanApplicationSettings.EnableHttpCompression);

            return(settings);
        }
示例#28
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                var settings = new ConnectionSettings(new Uri(_options.Uri));

                if (_options.AuthenticateWithBasicCredentials)
                {
                    settings = settings.BasicAuthentication(_options.UserName, _options.Password);
                }
                else if (_options.AuthenticateWithCertificate)
                {
                    settings = settings.ClientCertificate(_options.Certificate);
                }

                var lowlevelClient = new ElasticClient(settings);

                var pingResult = await lowlevelClient.PingAsync(cancellationToken : cancellationToken);

                var isSuccess = pingResult.ApiCall.HttpStatusCode == 200;

                return(isSuccess
                    ? HealthCheckResult.Healthy()
                    : new HealthCheckResult(context.Registration.FailureStatus));
            }
            catch (Exception ex)
            {
                return(new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
            }
        }
示例#29
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //Turn on the OptionsManager that supports IOptions
            services.AddOptions();

            //Add Configuration mappings.
            services.Configure <CGBestBetsDisplayServiceOptions>(Configuration.GetSection("CGBestBetsDisplayService"));
            services.Configure <CGBBIndexOptions>(Configuration.GetSection("CGBestBetsIndex"));

            //Add HttpClient singleton, which is used by the display service.
            services.AddSingleton <HttpClient, HttpClient>();

            //Add our Display Service
            services.AddTransient <IBestBetsDisplayService, CGBestBetsDisplayService>();

            // This will inject an IElasticClient using our configuration into any
            // controllers that take an IElasticClient parameter into its constructor.
            //
            // AddTransient means that it will instantiate a new instance of our client
            // for each instance of the controller.  So the function below will be called
            // on each request.
            services.AddTransient <IElasticClient>(p => {
                // Get the ElasticSearch credentials.
                string username = Configuration["Elasticsearch:Userid"];
                string password = Configuration["Elasticsearch:Password"];

                //Get the ElasticSearch servers that we will be connecting to.
                List <Uri> uris = GetServerUriList();

                // Create the connection pool, the SniffingConnectionPool will
                // keep tabs on the health of the servers in the cluster and
                // probe them to ensure they are healthy.  This is how we handle
                // redundancy and load balancing.
                var connectionPool = new SniffingConnectionPool(uris);

                //Return a new instance of an ElasticClient with our settings
                ConnectionSettings settings = new ConnectionSettings(connectionPool);

                //Let's only try and use credentials if the username is set.
                if (!string.IsNullOrWhiteSpace(username))
                {
                    settings.BasicAuthentication(username, password);
                }

                return(new ElasticClient(settings));
            });

            //Add in the token analyzer
            services.AddTransient <ITokenAnalyzerService, ESTokenAnalyzerService>();

            //Add our Match Service
            services.AddTransient <IBestBetsMatchService, ESBestBetsMatchService>();

            // Create CORS policies.
            services.AddCors();

            // Add framework services.
            services.AddMvc();
        }
示例#30
0
 private ConnectionSettings AddBasicAuthentication(ConnectionSettings settings)
 {
     if (!_shieldEnabled)
     {
         return(settings);
     }
     return(settings.BasicAuthentication("es_admin", "es_admin"));
 }