Пример #1
0
        public ExceptionSerializationTests()
        {
            var pool           = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var connection     = new InMemoryConnection();
            var values         = new ConnectionConfiguration(pool, connection);
            var lowlevelClient = new ElasticLowLevelClient(values);

            _elasticsearchNetSerializer = lowlevelClient.Serializer;
        }
Пример #2
0
        public void BaseUriWithTrailingSlashIsRespected()
        {
            var pool           = new SingleNodeConnectionPool(new Uri("http://localhost:9200/"));
            var settings       = new ConnectionSettings(pool, new InMemoryConnection());
            var client         = new ElasticClient(settings);
            var searchResponse = client.Search <Project>(s => s.AllIndices());

            searchResponse.ApiCall.Uri.ToString().Should().Be("http://localhost:9200/_all/_search?typed_keys=true");
        }
        /**
         *
         * ==== ThreadPool statistics
         *
         * It can often be useful to see the statistics for thread pool threads, particularly when
         * trying to diagnose issues with the client. The client can collect statistics for both
         * worker threads and asynchronous I/O threads, and expose these on the response and
         * in debug information.
         *
         * Similar to collecting TCP statistics, ThreadPool statistics can be collected for all requests
         * by configuring `EnableThreadPoolStats` on `ConnectionSettings`
         */
        public void ConnectionSettingsThreadPoolStats()
        {
            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var settings = new ConnectionSettings(connectionPool)
                           .EnableThreadPoolStats();       // <1> collect thread pool statistics for *all* requests

            var client = new ElasticClient(settings);
        }
Пример #4
0
        static Program()
        {
            //创建es连接,使用SingleNodeConnectionPool
            var uri      = new Uri("http://192.168.1.34:9200");
            var pool     = new SingleNodeConnectionPool(uri);
            var settings = new ConnectionSettings(pool).DefaultIndex("people");

            client = new ElasticClient(settings);
        }
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="esUrl"></param>

        public ElasticSearchIndex(string esUrl)
        {
            var connectionPool = new SingleNodeConnectionPool(new Uri(esUrl));

            using var connectionSettings = new ConnectionSettings(connectionPool).DisableDirectStreaming();
            var client = new ElasticClient(connectionSettings);

            ElasticClient = client;
        }
Пример #6
0
        public TestsBase(Func <ConnectionSettings, IElasticsearchSerializer> serializerFactory = null)
        {
            var node           = new Uri("http://localhost:9200");
            var connectionPool = new SingleNodeConnectionPool(node);

            var settings = new ConnectionSettings(connectionPool, serializerFactory).DefaultIndex("my-application" + Guid.NewGuid());

            Client = new ElasticClient(settings);
        }
        private void CreateElasticClient()
        {
            var node     = new Uri("http://localhost:9200"); //DevSkim: ignore DS137138
            var pool     = new SingleNodeConnectionPool(node);
            var settings = new ConnectionSettings(pool);

            //The default index when no index has been explicitly specified and no default indices are specified for the given CLR type
            //settings.DefaultIndex("default-index");
            settings.DefaultMappingFor <Post>(p => p
                                              .IndexName("my-post-index")
                                              );

            //throw exception instead of checking result.IsValid (except when result.SuccessOrKnownError is false)
            settings.ThrowExceptions();

            //Solve problem with my proxy
            settings.DisableAutomaticProxyDetection();

            settings.EnableDebugMode(callDetails =>
            {
                //Console.WriteLine(callDetails.DebugInformation); //Below code display shorter summary that does not include tcp and threadpool statistics

                Console.WriteLine("Request:");
                Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
                if (callDetails.RequestBodyInBytes != null)
                {
                    var json = $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}";

                    try
                    {
                        using var document = JsonDocument.Parse(json);
                        using var ms       = new MemoryStream();
                        using var writer   = new Utf8JsonWriter(ms, new JsonWriterOptions { Indented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
                        document.WriteTo(writer);
                        writer.Flush();
                        json = Encoding.UTF8.GetString(ms.ToArray());
                    }
                    catch { }

                    Console.WriteLine(json);
                }

                Console.WriteLine();

                Console.WriteLine("Response:");
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}");
                if (callDetails.ResponseBodyInBytes != null)
                {
                    Console.WriteLine($"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}");
                }

                Console.WriteLine($"{new string('-', 30)}\n");
                Console.WriteLine();
            });

            elasticClient = new ElasticClient(settings);
        }
Пример #8
0
        /**
         * This can be useful in tracking down numerous problems and can also be useful when filing an
         * {github}/issues[issue] on our github repository.
         *
         * By default, the request and response bytes are not available within the debug information, but
         * can be enabled globally on Connection Settings
         *
         */
        public void DisableDirectStreaming()
        {
            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var settings = new ConnectionSettings(connectionPool)
                           .DisableDirectStreaming();      // <1> disable direct streaming for *all* requests

            var client = new ElasticClient(settings);
        }
Пример #9
0
        public void InitElasticSearchUsingHttp()
        {
            var uri            = new Uri("http://localhost:9500");
            var connection     = new MyCustomHttpConnection();
            var connectionPool = new SingleNodeConnectionPool(uri);
            var settings       = new ConnectionSettings(connectionPool, connection);

            _elasticClient = new ElasticClient(settings);
        }
Пример #10
0
        private void SetupElasticsearch()
        {
            var esDomain = Environment.GetEnvironmentVariable("ELASTICSEARCH_DOMAIN_URL")
                           ?? "http://localhost:9202";
            var pool     = new SingleNodeConnectionPool(new Uri(esDomain));
            var settings = new ConnectionSettings(pool, JsonNetSerializer.Default)
                           .PrettyJson().ThrowExceptions().DisableDirectStreaming();

            _elasticSearchClient = new ElasticClient(settings);
        }
Пример #11
0
        private ElasticClient CreateAwsHostedElasticClient()
        {
            var httpConnection = new AwsHttpConnection(
                new BasicAWSCredentials(_awsAccessKeyId, _awsSecretAccessKey), RegionEndpoint.USEast2);
            var pool          = new SingleNodeConnectionPool(new Uri(_awsElasticSearchUrl));
            var config        = new ConnectionSettings(pool, httpConnection);
            var elasticClient = new ElasticClient(config);

            return(elasticClient);
        }
Пример #12
0
        public void GlobalSetup()
        {
            var connection         = new InMemoryConnection();
            var connectionPool     = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var connectionSettings = new ConnectionSettings(connectionPool, connection)
                                     .DefaultIndex("some-index");

            Client = new ElasticClient(connectionSettings);
            _run   = Routine();
        }
        public Operations()
        {
            var pool = new SingleNodeConnectionPool(new Uri(elasticConnection));

            var connectionSettings = new ConnectionSettings(pool, JsonNetSerializer.Default).BasicAuthentication(elasticUser, elasticPassword);

            connectionSettings.DefaultIndex(index);

            client = new ElasticClient(connectionSettings);
        }
Пример #14
0
 /**
  * In addition to exposing debug information on the response, debug mode will also cause the debug
  * information to be written to the trace listeners in the `System.Diagnostics.Debug.Listeners` collection
  * by default, when the request has completed. A delegate can be passed when enabling debug mode to perform
  * a different action when a request has completed, using <<logging-with-on-request-completed, `OnRequestCompleted`>>
  */
 public void DebugModeOnRequestCompleted()
 {
     var pool   = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
     var client = new ElasticClient(new ConnectionSettings(pool)
                                    .EnableDebugMode(apiCallDetails =>
     {
         // do something with the call details e.g. send with logging framework
     })
                                    );
 }
        private IElasticClient CreateElasticClient()
        {
            var connection     = new InMemoryConnection();
            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var settings = new ConnectionSettings(connectionPool, connection)
                           .DefaultIndex("abp-es-test");

            return(new ElasticClient(settings));
        }
Пример #16
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            var pool     = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var settings = new ConnectionSettings(pool).DefaultFieldNameInferrer(p => p).DefaultIndex("livros");
            var client   = new ElasticClient(settings);

            services.AddSingleton(client);
        }
Пример #17
0
        private void InitializeClient(string url)
        {
            _logger.LogInformation($"Connecting to elastic search on {url}");
            var pool     = new SingleNodeConnectionPool(new Uri(url));
            var settings = new ConnectionSettings(pool);

            _client = new ElasticClient(settings);
            CheckConnection();
            _logger.LogInformation($"Successfully created connection to Elastic Search");
        }
Пример #18
0
        public IElasticClient CreateClient(string jsonResponse, Action <JsonSerializerSettings, IConnectionSettingsValues> settingsOverride)
        {
            var connection         = new InMemoryConnection(Encoding.UTF8.GetBytes(jsonResponse));
            var connectionPool     = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var connectionSettings = new ConnectionSettings(connectionPool, connection, new MyCustomJsonFactory(settingsOverride))
                                     .DefaultIndex("default-index");
            var client = new ElasticClient(connectionSettings);

            return(client);
        }
        public void Setup()
        {
            var httpConnection = new AwsHttpConnection(Region);
            var pool           = new SingleNodeConnectionPool(new Uri(TestConfig.Endpoint));
            var config         = new ConnectionConfiguration(pool, httpConnection);

            config.DisableDirectStreaming();
            _client    = new ElasticLowLevelClient(config);
            _indexName = $"unittest_{Guid.NewGuid().ToString("n")}";
        }
        private static IElasticClient CreateClient(Uri connectionString)
        {
            var node = new UriBuilder(connectionString);


            var connectionPool     = new SingleNodeConnectionPool(node.Uri);
            var connectionSettings = new ConnectionSettings(connectionPool);

            return(new ElasticClient(connectionSettings));
        }
        public static void AddElasticSearch(this IServiceCollection services, IConfiguration configuration)
        {
            var options        = configuration.GetAWSOptions();
            var httpConnection = new AwsHttpConnection(options);
            var pool           = new SingleNodeConnectionPool(new Uri(configuration.GetSection("AWS:ElasticUrl").Value));
            var config         = new ConnectionSettings(pool, httpConnection);
            var client         = new ElasticClient(config);

            services.AddSingleton <IElasticClient>(client);
        }
        public LowLevelResponseTypes()
        {
            var connection = new InMemoryConnection(Response().Utf8Bytes());

            this.Client = new ElasticClient(new ConnectionSettings(connection).ApplyDomainSettings());

            var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            this.LowLevelClient = new ElasticLowLevelClient(new ConnectionConfiguration(pool, connection, new LowLevelRequestResponseSerializer()));
        }
        public static IElasticClient GetClient()
        {
            var connectionPool = new SingleNodeConnectionPool(new Uri(ElastisearchUrl));
            var settings       = new ConnectionSettings(connectionPool, connectionSettings => new MyJsonNetSerializer(connectionSettings))
                                 .DefaultIndex(IndexName)
                                 .DisableDirectStreaming()
                                 .PrettyJson();

            return(new ElasticClient(settings));
        }
Пример #24
0
        /**=== Overriding Json.NET settings
         *
         * Overriding the default Json.NET behaviour in NEST is an expert behavior but if you need to get to the nitty gritty, this can be really useful.
         */

        /**
         * The easiest way is to create an instance of `SerializerFactory` that allows you to register a modification callback
         * in the constructor
         */
        public void EasyWay()
        {
            var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var connectionSettings = new ConnectionSettings(
                pool,
                new HttpConnection(),
                new SerializerFactory((jsonSettings, nestSettings) => jsonSettings.PreserveReferencesHandling = PreserveReferencesHandling.All));

            var client = new ElasticClient(connectionSettings);
        }
Пример #25
0
        public void Asterisk_encoded_url_should_work()
        {
            var httpConnection = new AwsHttpConnection(Region, Credentials);
            var pool           = new SingleNodeConnectionPool(new Uri(TestConfig.Endpoint));
            var config         = new ConnectionConfiguration(pool, httpConnection);
            var client         = new ElasticLowLevelClient(config);
            var response       = client.Get <Stream>("index*", "type", "id");

            Assert.AreEqual(404, response.HttpStatusCode.GetValueOrDefault(-1));
        }
Пример #26
0
        public void NestPing_should_work()
        {
            var httpConnection = new AwsHttpConnection(Region, Credentials);
            var pool           = new SingleNodeConnectionPool(new Uri(TestConfig.Endpoint));
            var config         = new Nest.ConnectionSettings(pool, httpConnection);
            var client         = new Nest.ElasticClient(config);
            var response       = client.Ping();

            Assert.AreEqual(true, response.IsValid);
        }
Пример #27
0
        public void Ping_should_work()
        {
            var httpConnection = new AwsHttpConnection(Region, Credentials);
            var pool           = new SingleNodeConnectionPool(new Uri(TestConfig.Endpoint));
            var config         = new ConnectionConfiguration(pool, httpConnection);
            var client         = new ElasticLowLevelClient(config);
            var response       = client.Ping <object>();

            Assert.AreEqual(200, response.HttpStatusCode.GetValueOrDefault(-1));
        }
Пример #28
0
        /// <summary>
        /// Index
        /// </summary>
        /// <returns>action结果</returns>
        public IActionResult Index()
        {
            var pool     = new SingleNodeConnectionPool(new Uri("http://*****:*****@timestamp", GreaterThanOrEqualTo = DateTime.Parse("2019-04-10") };
             * var x = client.Search<ElasticsearchLogModel>(search);
             */
            var levels  = new[] { "debug", "information", "error", "fatal" };
            var filters = new List <Func <QueryContainerDescriptor <ElasticsearchLogModel>, QueryContainer> >();

            filters.Add(fq => fq.DateRange(r => r.Field(f => f.Timestamp).GreaterThanOrEquals(DateTime.Parse("2019-04-11")).LessThan(DateTime.Parse("2019-04-13"))));
            if (levels != null)
            {
                filters.Add(fq => fq.Terms(t => t.Field(f => f.Level).Terms(levels)));
            }

            var musts = new List <Func <QueryContainerDescriptor <ElasticsearchLogModel>, QueryContainer> >();

            musts.Add(mu => mu.Match(mq => mq.Field(f => f.MessageTemplate).Query("化")));
            musts.Add(mu => mu.Match(mq => mq.Field(f => f.Message).Query("初始化")));
            var x = client.Search <ElasticsearchLogModel>(s => s.Query(q => q
                                                                       .Bool(bq => bq.Filter(filters).Must(musts))));

            var resut = client.Search <ElasticsearchLogModel>(s => s
                                                              .From(0)
                                                              .Size(10)
                                                              .Query(q => q
                                                                     .Bool(b => b
                                                                           .Must(mu => mu.Match(mq => mq.Field(f => f.Level).Query("Information")),
                                                                                 mu => mu.Match(mq => mq.Field(f => f.Application).Query("ElasticSearchDemo.API4")))
                                                                           .Filter(fi => fi
                                                                                   .DateRange(r => r
                                                                                              .Field(f => f.Timestamp)
                                                                                              .GreaterThanOrEquals(DateTime.Parse("2019-04-09"))
                                                                                              .LessThan(DateTime.Parse("2019-04-11"))))
                                                                           )
                                                                     ));


            return(View());
        }
Пример #29
0
        public void FailedReIndexResponseMarkedAsInvalidAndContainFailures()
        {
            var pool = new SingleNodeConnectionPool(new Uri("http://*****:*****@"{
				""took"": 4,
				""timed_out"": false,
				""total"": 1,
				""updated"": 0,
				""created"": 0,
				""deleted"": 0,
				""batches"": 1,
				""version_conflicts"": 0,
				""noops"": 0,
				""retries"": {
					""bulk"": 0,
					""search"": 0
				},
				""throttled_millis"": 0,
				""requests_per_second"": -1.0,
				""throttled_until_millis"": 0,
				""failures"": [{
					""index"": ""employees-v2"",
					""type"": ""employee"",
					""id"": ""57f7ce8df8a10336a0cf935b"",
					""cause"": {
						""type"": ""mapper_parsing_exception"",
						""reason"": ""failed to parse [id]"",
						""caused_by"": {
							""type"": ""number_format_exception"",
							""reason"": ""For input string: \""57f7ce8df8a10336a0cf935b\""""
						}
					},
					""status"": 400
				}]
			}"            ;

            var connection = new InMemoryConnection(Encoding.UTF8.GetBytes(json), 400);
            var settings   = new ConnectionSettings(pool, connection);
            var client     = new ElasticClient(settings);

            var reindexResponse = client.ReindexOnServer(r => r
                                                         .Source(s => s
                                                                 .Index("employees-v1")
                                                                 .Type("employee")
                                                                 )
                                                         .Destination(d => d
                                                                      .Index("employees-v2")
                                                                      )
                                                         .Conflicts(Conflicts.Proceed)
                                                         );

            reindexResponse.IsValid.Should().BeFalse();
            reindexResponse.Failures.Should().NotBeNull().And.HaveCount(1);
        }
Пример #30
0
        public void Random_encoded_url_should_work()
        {
            var randomString   = Guid.NewGuid().ToString("N");
            var httpConnection = new AwsHttpConnection(Region, Credentials);
            var pool           = new SingleNodeConnectionPool(new Uri(TestConfig.Endpoint));
            var config         = new ConnectionConfiguration(pool, httpConnection);
            var client         = new ElasticLowLevelClient(config);
            var response       = client.Get <Stream>(randomString, string.Join(",", Enumerable.Repeat(randomString, 2)), randomString);

            Assert.AreEqual(404, response.HttpStatusCode.GetValueOrDefault(-1));
        }