Example #1
0
        public Elastic(string index_path)
        {
            try
            {
                var line = File.ReadLines(index_path);
                indexname = line.First().Trim();
            }
            catch (IOException e)
            {
                Console.WriteLine("Error reading " + index_path);
                System.Environment.Exit(-1);
            }

            string user     = "******";
            string password = "******";

            uri = "http://10.4.3.188:9200";
            var config = new ConnectionConfiguration(new Uri(uri)).BasicAuthentication(user, password);

            client = new ElasticLowLevelClient(config);

            Console.WriteLine("Connecting to ElasticSearch...");
            var response = client.Cat.Health <StringResponse>();

            if (response.HttpStatusCode != 200)
            {
                System.Environment.Exit(-1);
            }
            Console.WriteLine("OK", Console.ForegroundColor = ConsoleColor.Green);
            Console.ResetColor();
        }
Example #2
0
        public void con()
        {
            var settings = new ConnectionConfiguration(new Uri("http://localhost:9200"))
                           .RequestTimeout(TimeSpan.FromMinutes(2));
            var connection = new ElasticLowLevelClient(settings);

            var searchResponse = connection.Search <StringResponse>("test", PostData.Serializable(new
            {
                from  = 0,
                size  = 10,
                query = new
                {
                    match = new
                    {
                        nombre = new
                        {
                            query = "Yesid2"
                        }
                    }
                }
            }));

            var successful   = searchResponse.Success;
            var responseJson = searchResponse.Body;

            Console.WriteLine(responseJson);
        }
        public bool CreateIndex(IIndexCreateRequest request)
        {
            // TODO Validation
            if (request is null)
            {
                Logger.LogError("Definition is invalid");
                throw new ArgumentException("Argument \"definition\" is null or not of type ElasticIndexDefinition");
            }
            var node     = new Uri(request.IndexerEndpoint);
            var fileText = System.IO.File.ReadAllText(request.IndexDefinitionFilePath);

            var settings = new ConnectionSettings(node);
            var postData = PostData.String(fileText);
            var client   = new ElasticLowLevelClient(settings);
            var response = client.DoRequest <StringResponse>(HttpMethod.PUT, request.IndexName, postData);

            if (response.HttpStatusCode.HasValue)
            {
                // TODO Good or bad?
                Logger.LogInformation($"StatusCode {response.HttpStatusCode}");
            }
            Logger.LogDebug(response.DebugInformation);
            Logger.LogInformation(response.Body);
            return(response.Success);
        }
Example #4
0
 public override void Dispose()
 {
     if (initialised)
     {
         client = null;
     }
 }
Example #5
0
        /**
         * Often you may need to pass additional configuration options to the client such as the address of Elasticsearch if it's running on
         * a remote machine. This is where `ConnectionConfiguration` comes in; an instance can be instantiated to provide
         * the client with different configuration values.
         */
        public void UsingConnectionSettings()
        {
            var settings = new ConnectionConfiguration(new Uri("http://example.com:9200"))
                           .RequestTimeout(TimeSpan.FromMinutes(2));

            var lowlevelClient = new ElasticLowLevelClient(settings);
        }
 /**
  * This will still be a non-failover connection, meaning if that `node` goes down the operation will not be retried on any other nodes in the cluster.
  *
  * To get a failover connection we have to pass an <<connection-pooling, IConnectionPool>> instance instead of a `Uri`.
  */
 public void InstantiatingAConnectionPoolClient()
 {
     var node           = new Uri("http://mynode.example.com:8082/apiKey");
     var connectionPool = new SniffingConnectionPool(new[] { node });
     var config         = new ConnectionConfiguration(connectionPool);
     var client         = new ElasticLowLevelClient(config);
 }
Example #7
0
        public JsonResult Delete(int id)
        {
            var oResult = new Object();

            try
            {
                var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/"))
                               .RequestTimeout(TimeSpan.FromMinutes(2));

                var lowlevelClient = new ElasticLowLevelClient(settings);

                var searchResponse = lowlevelClient.Delete <string>("user", "guest", id.ToString());

                bool successful = searchResponse.Success;

                oResult = new
                {
                    Bresult = successful,
                    Notice  = successful ? "删除成功!" : "删除失败!"
                };

                var responseJson = searchResponse.Body;

                return(Json(oResult, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                oResult = new
                {
                    Bresult = false,
                    Notice  = String.Format("删除失败!异常:{0}", ex.Message)
                };
                return(Json(oResult, JsonRequestBehavior.AllowGet));
            }
        }
Example #8
0
        public async Task OpenAsync(string correlationId)
        {
            if (IsOpened())
            {
                return;
            }

            var connection = await _connectionResolver.ResolveAsync(correlationId);

            var uri = new Uri(connection.Uri);

            try
            {
                // Create client
                var settings = new ConnectionConfiguration(uri)
                               .RequestTimeout(TimeSpan.FromMinutes(2))
                               .ThrowExceptions(true);
                _client = new ElasticLowLevelClient(settings);

                // Create index if it doesn't exist
                await CreateIndex(correlationId, true);

                if (_timer == null)
                {
                    _timer = new FixedRateTimer(OnTimer, _interval, _interval);
                    _timer.Start();
                }
            }
            catch
            {
                // Do nothing if elastic search client was not initialized
                _errorConsoleLogger.Error(correlationId, $"Failed to initialize Elastic Search Logger with uri='{uri}'");
            }
        }
Example #9
0
        static Program()
        {
            //创建es连接
            var settings = new ConnectionConfiguration(new Uri("http://192.168.1.40:9200")).RequestTimeout(TimeSpan.FromMinutes(2));

            client = new ElasticLowLevelClient(settings);
        }
Example #10
0
        public void SendLogEventsToAirlock_GotItAtElastic()
        {
            const int eventCount = 10;
            var       log        = IntegrationTestsEnvironment.Log;
            var       logEvents  = GenerateLogEvens(eventCount);

            PushToAirlock(logEvents);

            var connectionPool = new StickyConnectionPool(new[] { new Uri("http://localhost:9200") });
            var elasticConfig  = new ConnectionConfiguration(connectionPool, cfg =>
            {
                cfg.EnableDebugMode();
                return(null);
            });
            var elasticClient = new ElasticLowLevelClient(elasticConfig);
            var indexName     = $"{IntegrationTestsEnvironment.Project}-{IntegrationTestsEnvironment.Environment}-{logEvents.First().Timestamp:yyyy.MM.dd}";

            var testId = logEvents.First().Properties["testId"];
            var expectedLogMessages = new HashSet <string>(logEvents.Select(x => x.Message));

            WaitHelper.WaitSafe(
                () =>
            {
                var elasticsearchResponse = elasticClient.Search <string>(
                    indexName,
                    "LogEvent",
                    new
                {
                    from  = 0,
                    size  = eventCount,
                    query = new
                    {
                        match = new
                        {
                            testId,
                        }
                    }
                });
                log.Debug(elasticsearchResponse.DebugInformation);
                if (!elasticsearchResponse.Success)
                {
                    log.Error(elasticsearchResponse.OriginalException);
                    return(WaitAction.ContinueWaiting);
                }

                var hits = (JArray)((dynamic)JObject.Parse(elasticsearchResponse.Body)).hits.hits;
                if (expectedLogMessages.Count != hits.Count)
                {
                    log.Error($"Invalid event count: {hits.Count}, expected: {expectedLogMessages.Count}");
                    return(WaitAction.ContinueWaiting);
                }

                foreach (dynamic hit in hits)
                {
                    string message = hit._source.Message;
                    Assert.True(expectedLogMessages.Contains(message));
                }
                return(WaitAction.StopWaiting);
            });
        }
Example #11
0
        public ElasticIndexer(ElasticSearchSettings settings)
        {
            string uri = settings.Uri;

            Uri    = new Uri("https://" + uri);
            Client = LoadClient();
        }
Example #12
0
        public ElasticSearchDriver(ILogger <ElasticSearchDriver> logger, Uri uri, string index)
        {
            IndexName = index;
            Logger    = logger;


            var connectionSettings = new ConnectionSettings(uri)
                                     .DefaultIndex(index)
                                     .ThrowExceptions();

            lowLevelClient = new ElasticLowLevelClient(connectionSettings);
            client         = new ElasticClient(connectionSettings);

            if (!ExtantIndicies.Contains(index))
            {
                lock (ExtantIndicies)
                {
                    var exists = client.IndexExists(index);
                    if (!exists.Exists)
                    {
                        Logger.LogInformation("Index does not exist, creating it now");

                        var descriptor = new CreateIndexDescriptor(index)
                                         .Mappings(ms => ms
                                                   .Map <Document>(m => m.AutoMap())
                                                   );

                        var response = client.CreateIndex(descriptor);
                    }
                    ExtantIndicies.Add(index);
                }
            }
        }
Example #13
0
        public void CheckSessionIndexIsDeleted()
        {
            string indexAndTypeName = $"index_{Guid.NewGuid().ToString()}";

            var mapping   = @"{""properties"": {""message"": {""type"": ""string"", ""store"": true}}}";
            var documents = @"[{""message"": ""The quick brown fox jumps over the lazy dog""}]";

            var esUri    = new Uri("http://localhost:9200");
            var config   = new ConnectionConfiguration(esUri);
            var esClient = new ElasticLowLevelClient(config);

            var uniqueNameResolverMock = new Mock <IUniqueNameResolver>();

            uniqueNameResolverMock.Setup(r => r.GetUniqueName()).Returns(indexAndTypeName);
            var idGeneratorMock = new Mock <IIdGenerator>();

            idGeneratorMock.Setup(g => g.NextId()).Returns(1);
            var validator = new EsfStateInputValidator();

            using (var session = new Domain.ElasticsearchSession(esClient, uniqueNameResolverMock.Object, idGeneratorMock.Object, validator))
            {
                session.CreateMapping(mapping);
                session.InsertDocuments(documents);

                var indexExistsResponse = esClient.IndicesGet <string>(indexAndTypeName);
                Assert.True(indexExistsResponse.Success);
            }

            var indexExistsResponse2 = esClient.IndicesGet <string>(indexAndTypeName);

            Assert.False(indexExistsResponse2.Success);
        }
Example #14
0
        private ElasticsearchSinkState(ElasticsearchSinkOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.IndexFormat))
            {
                throw new ArgumentException("options.IndexFormat");
            }
            if (string.IsNullOrWhiteSpace(options.TypeName))
            {
                throw new ArgumentException("options.TypeName");
            }
            if (string.IsNullOrWhiteSpace(options.TemplateName))
            {
                throw new ArgumentException("options.TemplateName");
            }

            _templateName        = options.TemplateName;
            _templateMatchString = IndexFormatRegex.Replace(options.IndexFormat, @"$1*$2");

            _indexDecider = options.IndexDecider ?? ((@event, offset) => string.Format(options.IndexFormat, offset));

            _options = options;

            Func <ConnectionConfiguration, IElasticsearchSerializer> serializerFactory = null;

            if (options.Serializer != null)
            {
                serializerFactory = s => options.Serializer;
            }

            var configuration = new ConnectionConfiguration(options.ConnectionPool, options.Connection, serializerFactory)
                                .RequestTimeout(options.ConnectionTimeout);

            if (options.ModifyConnectionSettings != null)
            {
                configuration = options.ModifyConnectionSettings(configuration);
            }

            configuration.ThrowExceptions();

            _client = new ElasticLowLevelClient(configuration);

            _formatter = options.CustomFormatter ?? new ElasticsearchJsonFormatter(
                formatProvider: options.FormatProvider,
                renderMessage: true,
                closingDelimiter: string.Empty,
                serializer: options.Serializer,
                inlineFields: options.InlineFields
                );

            _durableFormatter = options.CustomDurableFormatter ?? new ElasticsearchJsonFormatter(
                formatProvider: options.FormatProvider,
                renderMessage: true,
                closingDelimiter: Environment.NewLine,
                serializer: options.Serializer,
                inlineFields: options.InlineFields
                );

            _registerTemplateOnStartup  = options.AutoRegisterTemplate;
            TemplateRegistrationSuccess = !_registerTemplateOnStartup;
        }
Example #15
0
        public void TestReadPage()
        {
            var connection = new Connection {
                Name     = "input",
                Provider = "elasticsearch",
                Index    = "colors",
                Server   = "localhost",
                Port     = 9200
            }.WithValidation();

            connection.Url = connection.BuildElasticUrl();

            var pool     = new SingleNodeConnectionPool(new Uri(connection.Url));
            var settings = new ConnectionConfiguration(pool);
            var client   = new ElasticLowLevelClient(settings);
            var context  = new ConnectionContext(new PipelineContext(new TraceLogger(), null, new Entity {
                Name     = "rows",
                Alias    = "rows",
                Page     = 2,
                PageSize = 20
            }.WithDefaults()), connection);
            var code = new Field {
                Name = "code", Index = 0
            }.WithDefaults();
            var total = new Field {
                Name = "total", Type = "int", Index = 1
            }.WithDefaults();

            var reader = new ElasticReader(context, new[] { code, total }, client, new RowFactory(2, false, false), ReadFrom.Input);

            var rows = reader.Read().ToArray();

            Assert.AreEqual(20, rows.Length);
        }
        public ElasticsearchService(IOptionsMonitor <ElasticsearchOptions> options
                                    , ILoggerFactory loggerFac)
        {
            _elasticsearchOptions = options.CurrentValue;
            options.OnChange((elasticsearchOpt, s) => {
                _elasticsearchOptions = elasticsearchOpt;
                System.Diagnostics.Debug
                .WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(elasticsearchOpt) + "---" + s);
            });

            var lowlevelClient = new ElasticLowLevelClient();
            var urlColl        = new Uri[_elasticsearchOptions.Host.Length];

            for (int i = 0; i < _elasticsearchOptions.Host.Length; i++)
            {
                urlColl[i] = new Uri(_elasticsearchOptions.Host[i]);
            }
            _loggerFac = loggerFac;
            var connectionPool = new SniffingConnectionPool(urlColl);
            var settings       = new ConnectionSettings(connectionPool)
                                 .RequestTimeout(TimeSpan.FromMinutes(_elasticsearchOptions.TimeOut))
                                 .DefaultIndex("mylogjob");

            _elasticClient = new ElasticClient(settings);
        }
Example #17
0
		/**
		 * This will still be a non-failover connection, meaning if that `node` goes down the operation will not be retried on any other nodes in the cluster.
		 *
		 * To get a failover connection we have to pass an <<connection-pooling, IConnectionPool>> instance instead of a `Uri`.
		 */
		public void InstantiatingAConnectionPoolClient()
		{
			var node = new Uri("http://mynode.example.com:8082/apiKey");
			var connectionPool = new SniffingConnectionPool(new[] { node });
			var config = new ConnectionConfiguration(connectionPool);
			var client = new ElasticLowLevelClient(config);
		}
Example #18
0
        /// <summary>
        /// Main
        /// </summary>
        /// <param name="args">参数</param>
        static void Main(string[] args)
        {
            var settings = new ConnectionConfiguration(new Uri("http://localhost:9200"))
                           .RequestTimeout(TimeSpan.FromMinutes(2));
            var client = new ElasticLowLevelClient(settings);

            //新增一个人

            /*
             * var person = new Person {
             *  FirstName = "Martign",
             *  LastName = "Laarman"
             * };
             * var indexResponse = client.Index<BytesResponse>("people", "person", "1", PostData.Serializable(person));
             * var responseBytes = indexResponse.Body;
             */

            //批量新增
            var persons = new object[] {
                new { index = new { _index = "people", _type = "person", _id = "2" } },
                new { FirstName = "Russ", LastName = "Cam" },
                new { index = new { _index = "people", _type = "person", _id = "3" } },
                new { FirstName = "Rose", LastName = "Cam" },
                new { index = new { _index = "people", _type = "person", _id = "4" } },
                new { FirstName = "Malilian", LastName = "Menluo" }
            };
            var bulkResponse   = client.Bulk <StringResponse>(PostData.MultiJson(persons));
            var responseStream = bulkResponse.Body;
        }
Example #19
0
        private void cmdSearch_Click(object sender, EventArgs e)
        {
            var settings = new ConnectionConfiguration(EndPoint())
                           //.ThrowExceptions()
                           .RequestTimeout(TimeSpan.FromMinutes(2));

            var lowlevelClient = new ElasticLowLevelClient(settings);

            var searchResponse = lowlevelClient.Search <StringResponse>("people", "person", PostData.Serializable(new
            {
                size  = 10,
                query = new
                {
                    match = new
                    {
                        firstName = new { query = "David" }
                    }
                }
            }));

            var successful          = searchResponse.Success;
            var responseJson        = searchResponse.Body;
            var successOrKnownError = searchResponse.SuccessOrKnownError;
            var exception           = searchResponse.OriginalException;

            txtResult.Text = PrettyPrintJSON(responseJson);
        }
Example #20
0
        private void cmdInsert_Click(object sender, EventArgs e)
        {
            var settings = new ConnectionConfiguration(EndPoint())
                           .RequestTimeout(TimeSpan.FromMinutes(2));

            var lowlevelClient = new ElasticLowLevelClient(settings);

            var person = new Person
            {
                firstName = "David",
                lastName  = "Betteridge"
            };

            var indexResponse = lowlevelClient.Index <BytesResponse>("people", "person", "David", PostData.Serializable(person));
            var responseBytes = indexResponse.Body;

            txtResult.Text = PrettyPrintJSON(BytesToString(responseBytes));

            /************************************************************************************************
             *                      We can also get the result in string format
             \/
             *************************************************************************************************/
            //var indexResponse = lowlevelClient.Index<StringResponse>("people", "person", "David", PostData.Serializable(person));
            //var responseString = indexResponse.Body;
            //txtResult.Text = PrettyPrintJSON(responseString);
        }
Example #21
0
        /**
         * Using these details, it is possible to make decisions around what should be done in your application.
         *
         * The default behaviour of not throwing exceptions can be changed by setting `.ThrowExceptions()` on `ConnectionConfiguration`
         */
        public void HandlingErrors()
        {
            var settings = new ConnectionConfiguration(new Uri("http://example.com:9200"))
                           .ThrowExceptions();

            var lowlevelClient = new ElasticLowLevelClient(settings);
        }
Example #22
0
        }                                                                             //Singleton pattern

        private ElasticTelemetry()
        {
            try
            {
                //Load custom app.config for this assembly
                var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);



                //Configure connection settings
                var connectionSettings = new ConnectionConfiguration(new Uri(appConfig.AppSettings.Settings["ElasticServer"].Value));
                connectionSettings
                .BasicAuthentication(appConfig.AppSettings.Settings["ElasticUser"].Value, appConfig.AppSettings.Settings["ElasticPassword"].Value)
                .RequestTimeout(TimeSpan.FromSeconds(30))
                .DisableDirectStreaming();

                //Create Elastic client
                _ElasticClient = new ElasticLowLevelClient(connectionSettings);

                //Set Elastic Index and Type
                _ElasticIndex = appConfig.AppSettings.Settings["ElasticIndex"].Value;
                _ElasticType  = appConfig.AppSettings.Settings["ElasticType"].Value;
            }
            catch (Exception e) { logger.Fatal(e); }
        }
        /**[[configuration-options]]
         * === Configuration options
         *
         * Connecting to Elasticsearch with <<elasticsearch-net-getting-started,Elasticsearch.Net>> and <<nest-getting-started,NEST>> is easy, but
         * it's entirely possible that you'd like to change the default connection behaviour. There are a number of configuration options available
         * on `ConnectionSettings` (and `ConnectionConfiguration` for Elasticsearch.Net) that can be used to control
         * how the clients interact with Elasticsearch.
         *
         * ==== Options on ConnectionConfiguration
         *
         * The following is a list of available connection configuration options on `ConnectionConfiguration`; since
         * `ConnectionSettings` derives from `ConnectionConfiguration`, these options are available for both
         * Elasticsearch.Net and NEST:
         *
         * :xml-docs: Elasticsearch.Net:ConnectionConfiguration`1
         *
         * ==== Options on ConnectionSettings
         *
         * The following is a list of available connection configuration options on `ConnectionSettings`:
         *
         * :xml-docs: Nest:ConnectionSettingsBase`1
         *
         * Here's an example to demonstrate setting several configuration options using the low level client
         */
        public void AvailableOptions()
        {
            var connectionConfiguration = new ConnectionConfiguration()
                                          .DisableAutomaticProxyDetection()
                                          .EnableHttpCompression()
                                          .DisableDirectStreaming()
                                          .PrettyJson()
                                          .RequestTimeout(TimeSpan.FromMinutes(2));

            var lowLevelClient = new ElasticLowLevelClient(connectionConfiguration);


            /**
             * And with the high level client
             */
            var connectionSettings = new ConnectionSettings()
                                     .DefaultMappingFor <Project>(i => i
                                                                  .IndexName("my-projects")
                                                                  .TypeName("project")
                                                                  )
                                     .EnableDebugMode()
                                     .PrettyJson()
                                     .RequestTimeout(TimeSpan.FromMinutes(2));

            var client = new ElasticClient(connectionSettings);

            /**[NOTE]
             * ====
             *
             * Basic Authentication credentials can alternatively be specified on the node URI directly
             */
            var uri      = new Uri("http://*****:*****@localhost:9200");
            var settings = new ConnectionConfiguration(uri);
        }
        public void Initialize(string configFilepath, bool isTestMode, SimpleSettings settings,
                               IDataRepository dataManager)
        {
            try
            {
                if (!File.Exists(configFilepath))
                {
                    throw new FileNotFoundException($"{configFilepath} was not found");
                }

                XDocument xDocument = XDocument.Load(configFilepath);
                var       elkurl    = xDocument.Element("ELKUrl")?.Value;
                _elasticClient         = new ElasticClient(new Uri(elkurl));
                _elasticLowLevelClient =
                    new ElasticLowLevelClient(
                        new ConnectionConfiguration(new SniffingConnectionPool(new List <Uri>()
                {
                    new Uri(elkurl)
                })));
                _indexPath        = new List <string>();
                _addDocumentQueue = new ConcurrentBag <object[]>();
                Task.Factory.StartNew(new Action(UpdateNodeRun));
            }
            catch (Exception e)
            {
                Logger.Error(e, "Initialize");
            }
        }
        public ElasticSearchConnection()
        {
            var settings = new ConnectionConfiguration(new Uri("http://localhost:9200"))
                           .RequestTimeout(TimeSpan.FromMinutes(2));

            this.client = new ElasticLowLevelClient(settings);
        }
Example #26
0
        public void VerifyingEverythingInElasticsearch(int age, int isOver50)
        {
            const string message = "isOver50 != searchResults.Documents.Count";

            var uri      = new Uri(_elasticConfig.Uri);
            var settings = new ConnectionSettings(uri).DefaultIndex(_elasticConfig.Index);
            var elasticLowLevelClient = new ElasticLowLevelClient(settings);
            var client = new ElasticClient(settings);


            int tries = 60;

            while (tries-- > 0)
            {
                var searchResults = client.Search <IncidentReport>(s => s
                                                                   .From(0)
                                                                   .Size(500)
                                                                   .Query(q => q.Term(p => p.ReportedBy.Age, age)));


                try
                {
                    Assert.AreEqual(isOver50, searchResults.Documents.Count, message);
                    return;
                }
                catch (Exception exception)
                {
                    _logger.Warning(exception, message);
                }

                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            throw new Exception("isOver50 != searchResults.Documents.Count");
        }
Example #27
0
 //General - Set the settings for this project
 private void setSettings(AppSettings settings)
 {
     appSettings = settings;
     database    = new LLDatabase(appSettings.elasticip, appSettings.defaultIndex);
     if (database.isValid())
     {
         bottomDBStatusText.Text = "Connected";
         client = database.getClient();
         if (queryBuilder == null)
         {
             queryBuilder = new LLQueryBuilder(client);
         }
         else
         {
             queryBuilder.setClient(client);
         }
         queryBuilder.setMainIndex(appSettings.defaultIndex);
         queryBuilder.setFileExclusions(appSettings.exclusions);
         enableComponents();
     }
     else
     {
         bottomDBStatusText.Text = "No connection";
         MessageBox.Show("Connection failed! Check your settings...");
         logger.debug("setSettings failed! -> elastic ip: " + appSettings.elasticip + " elastic index" + appSettings.defaultIndex);
     }
 }
Example #28
0
        public void Call_to_Elasticsearch_propagates_Trace_Context_when_HttpDiagnosticsSubscriber_subscribed()
        {
            using var localServer = LocalServer.Create(context =>
            {
                var traceparent = context.Request.Headers.Get("traceparent");
                traceparent.Should().NotBeNullOrEmpty();

                var elasticTraceparent = context.Request.Headers.Get("elastic-apm-traceparent");
                elasticTraceparent.Should().NotBeNullOrEmpty().And.Be(traceparent);

                var tracestate = context.Request.Headers.Get("tracestate");
                tracestate.Should().NotBeNullOrEmpty().And.Contain("es=s:1");

                context.Response.StatusCode = 200;
            });

            using var agent     = new ApmAgent(new TestAgentComponents(payloadSender: new MockPayloadSender(), configuration: new MockConfiguration(exitSpanMinDuration: "0", spanCompressionEnabled: "false")));
            using var subscribe = agent.Subscribe(new ElasticsearchDiagnosticsSubscriber(), new HttpDiagnosticsSubscriber());

            var client = new ElasticLowLevelClient(new ConnectionConfiguration(new Uri(localServer.Uri)));

            agent.Tracer.CaptureTransaction("Transaction", ApiConstants.TypeDb, t =>
            {
                var response = client.Cat.Indices <StringResponse>();
            });
        }
        protected ServerErrorTestsBase()
        {
            var settings = FixedResponseClient.CreateConnectionSettings(ResponseJson, 500);

            LowLevelClient  = new ElasticLowLevelClient(settings);
            HighLevelClient = new ElasticClient(settings);
        }
Example #30
0
        public static void execute(string host, int port, Dictionary <ulong, FileNameAndParentFrn> files, int chunkSize = 10000)
        {
            var settings = new ConnectionConfiguration(new Uri("http://" + host + ":" + port))
                           .RequestTimeout(TimeSpan.FromMinutes(2));
            var lowlevelClient = new ElasticLowLevelClient(settings);

            var index = DateTime.Now.ToString("yyyyMMddHHmmss");
            var type  = "pathes";

            long id = 1;

            foreach (var chank in files.Values.Chunks(chunkSize))
            {
                var json = new List <object>();
                foreach (var file in chank)
                {
                    json.Add(new Index(id, index, type));
                    json.Add(new FileEntry(file.Path, ""));

                    id++;
                }

                var indexResponse = lowlevelClient.Bulk <StreamResponse>(PostData.MultiJson(json));
                using (var responseStream = indexResponse.Body)
                {
                    Console.Write(".");
                }
            }
            Console.WriteLine("");
        }
Example #31
0
        /// <summary>
        /// Returns a new instance of ElasticLowLevelClient configured
        /// with DataSetFetcher instance settings
        /// </summary>
        /// <returns></returns>
        protected ElasticLowLevelClient getClient()
        {
            var conf   = new ConnectionConfiguration(ClusterUri);
            var client = new ElasticLowLevelClient(conf);

            return(client);
        }
Example #32
0
        public static void MainMethod()
        {
            try
            {
                //var lowlevelClient = new ElasticLowLevelClient();

                var settings = new ConnectionConfiguration(new Uri("http://120.27.213.67:9200")).RequestTimeout(TimeSpan.FromMinutes(2));
                //var lowlevelClient = new ElasticLowLevelClient(settings);

                //var uris = new[]{new Uri("http://120.27.213.67:9200"),};
                //var connectionPool = new SniffingConnectionPool(uris);
                //var settings = new ConnectionConfiguration(connectionPool);
                var lowlevelClient = new ElasticLowLevelClient(settings);

                var person = new Person {
                    FirstName = "Martijn", LastName = "Laarman"
                };
                var    indexResponse = lowlevelClient.Index <StringResponse>("people", "person", "1", PostData.Serializable(person));
                string responseBytes = indexResponse.Body;
                //var asyncIndexResponse = await lowlevelClient.IndexAsync<StringResponse>("people", "person", "1", PostData.Serializable(person));
                //string responseString = asyncIndexResponse.Body;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #33
0
 public void Ping_should_work()
 {
     var httpConnection = new AwsHttpConnection(TestConfig.AwsSettings);
     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));
 }
Example #34
0
 public void Asterisk_encoded_url_should_work()
 {
     var httpConnection = new AwsHttpConnection(TestConfig.AwsSettings);
     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));
 }
Example #35
0
 public void Random_encoded_url_should_work()
 {
     var randomString = Guid.NewGuid().ToString("N");
     var httpConnection = new AwsHttpConnection(TestConfig.AwsSettings);
     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));
 }
        private ElasticsearchSinkState(ElasticsearchSinkOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.IndexFormat)) throw new ArgumentException("options.IndexFormat");
            if (string.IsNullOrWhiteSpace(options.TypeName)) throw new ArgumentException("options.TypeName");
            if (string.IsNullOrWhiteSpace(options.TemplateName)) throw new ArgumentException("options.TemplateName");

            this._templateName = options.TemplateName;
            this._templateMatchString = IndexFormatRegex.Replace(options.IndexFormat, @"$1*$2");

            _indexDecider = options.IndexDecider ?? ((@event, offset) => string.Format(options.IndexFormat, offset));

            _typeName = options.TypeName;
            _options = options;

            Func<ConnectionConfiguration, IElasticsearchSerializer> serializerFactory = null;
            if (options.Serializer != null)
            {
                serializerFactory = s => options.Serializer;
            }
            ConnectionConfiguration configuration = new ConnectionConfiguration(options.ConnectionPool, options.Connection, serializerFactory)
                .RequestTimeout(options.ConnectionTimeout);

            if (options.ModifyConnectionSettings != null)
                configuration = options.ModifyConnectionSettings(configuration);

            configuration.ThrowExceptions();
            _client = new ElasticLowLevelClient(configuration);

            _formatter = options.CustomFormatter ?? new ElasticsearchJsonFormatter(
                formatProvider: options.FormatProvider,
                renderMessage: true,
                closingDelimiter: string.Empty,
                serializer: options.Serializer,
                inlineFields: options.InlineFields
            );
            _durableFormatter = options.CustomDurableFormatter ?? new ElasticsearchJsonFormatter(
               formatProvider: options.FormatProvider,
               renderMessage: true,
               closingDelimiter: Environment.NewLine,
               serializer: options.Serializer,
               inlineFields: options.InlineFields
               );

            _registerTemplateOnStartup = options.AutoRegisterTemplate;
        }
Example #37
0
        private static List<Tuple<string, string, string, DateTime>> HierarchySearch()
        {
            var node = new Uri("http://boom-box-1.boomerang.com:9200");
            var config = new ConnectionConfiguration(node);
            var client = new ElasticLowLevelClient(config);
            var result = client.SearchGet<object>("hsbc_conform", "osf_hierarchies", (arg) => arg.AddQueryString("size", "100"));
            var root = JObject.FromObject(result.Body);
            var hierarchies = new List<Tuple<string, string, string, DateTime>>();

            foreach (var item in root["hits"]["hits"])
            {
                var date = item["_source"]["ctl"]["effective_date"].Value<string>();
                var country = item["_source"]["conform"]["geography"].Value<string>();
                var channel = item["_source"]["conform"]["channel"].Value<string>();

                var parsedDate = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);
                var tuple = Tuple.Create(channel, country, date, parsedDate);
                hierarchies.Add(tuple);
            }

            return hierarchies;
        }
Example #38
0
		/**== Connecting
		 * Connecting to Elasticsearch with `Elasticsearch.Net` is quite easy and there a few options to suit a number of different use cases.
		 *
		 * [[connection-strategies]]
		 * === Choosing the right Connection Strategy
		 * If you simply new an `ElasticLowLevelClient`, it will be a non-failover connection to `http://localhost:9200`
		 */
		public void InstantiateUsingAllDefaults()
		{
			var client = new ElasticLowLevelClient();
		}
Example #39
0
		/**
		 * If your Elasticsearch node does not live at `http://localhost:9200` but instead lives somewhere else, for example, `http://mynode.example.com:8082/apiKey`, then
		 * you will need to pass in some instance of `IConnectionConfigurationValues`.
		 *
		 * The easiest way to do this is:
		 */
		public void InstantiatingASingleNodeClient()
		{
			var node = new Uri("http://mynode.example.com:8082/apiKey");
			var config = new ConnectionConfiguration(node);
			var client = new ElasticLowLevelClient(config);
		}
Example #40
0
		/**
		 * The following is a list of available connection configuration options:
		 */
		public void AvailableOptions()
		{
			var config = new ConnectionConfiguration()
				.DisableAutomaticProxyDetection() // <1> Disable automatic proxy detection. When called, defaults to `true`.
				.EnableHttpCompression() // <2> Enable compressed request and responses from Elasticsearch (Note that nodes need to be configured to allow this. See the {ref_current}/modules-http.html[http module settings] for more info).
				.DisableDirectStreaming(); // <3> By default responses are deserialized directly from the response stream to the object you tell it to. For debugging purposes, it can be very useful to keep a copy of the raw response on the result object, which is what calling this method will do.

			var client = new ElasticLowLevelClient(config);
			var result = client.Search<SearchResponse<object>>(new { size = 12 });

			/** `.ResponseBodyInBytes` will only have a value if the client configuration has `DisableDirectStreaming` set */
			var raw = result.ResponseBodyInBytes;

			/**
			 * Please note that using `.DisableDirectStreaming` only makes sense if you need the mapped response **and** the raw response __at the same time__.
			 * If you need only a `string` response simply call
			 */
			var stringResult = client.Search<string>(new { });
			/**
			* and similarly, if you need only a `byte[]`
			*/
			var byteResult = client.Search<byte[]>(new { });

			/** other configuration options */
			config = config
				.GlobalQueryStringParameters(new NameValueCollection()) // <1> Allows you to set querystring parameters that have to be added to every request. For instance, if you use a hosted elasticserch provider, and you need need to pass an `apiKey` parameter onto every request.
				.Proxy(new Uri("http://myproxy"), "username", "pass") // <2> Sets proxy information on the connection.
				.RequestTimeout(TimeSpan.FromSeconds(4)) // <3> [[request-timeout]] Sets the global maximum time a connection may take. Please note that this is the request timeout, the builtin .NET `WebRequest` has no way to set connection timeouts (see http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx[the MSDN documentation on `HttpWebRequest.Timeout` Property]).
				.ThrowExceptions() // <4> As an alternative to the C/go like error checking on `response.IsValid`, you can instead tell the client to <<thrown-exceptions, throw exceptions>>.
				.PrettyJson() // <5> forces all serialization to be indented and appends `pretty=true` to all the requests so that the responses are indented as well
				.BasicAuthentication("username", "password"); // <6> sets the HTTP basic authentication credentials to specify with all requests.

			/**
			* NOTE: Basic authentication credentials can alternatively be specified on the node URI directly:
			*/
			var uri = new Uri("http://*****:*****@localhost:9200");
			var settings = new ConnectionConfiguration(uri);

			/**
			*...but this may become tedious when using connection pooling with multiple nodes.
			*
			* [[thrown-exceptions]]
			* === Exceptions
			* There are three categories of exceptions that may be thrown:
			*
			* `ElasticsearchClientException`:: These are known exceptions, either an exception that occurred in the request pipeline
			* (such as max retries or timeout reached, bad authentication, etc...) or Elasticsearch itself returned an error (could
			* not parse the request, bad query, missing field, etc...). If it is an Elasticsearch error, the `ServerError` property
			* on the response will contain the the actual error that was returned.  The inner exception will always contain the
			* root causing exception.
			*
			* `UnexpectedElasticsearchClientException`:: These are unknown exceptions, for instance a response from Elasticsearch not
			* properly deserialized.  These are usually bugs and {github}/issues[should be reported]. This exception also inherits from `ElasticsearchClientException`
			* so an additional catch block isn't necessary, but can be helpful in distinguishing between the two.
			*
			* Development time exceptions:: These are CLR exceptions like `ArgumentException`, `ArgumentOutOfRangeException`, etc.
			* that are thrown when an API in the client is misused.
			* These should not be handled as you want to know about them during development.
			*
			*/
		}
		/**
		 * The following is a list of available connection configuration options:
		 */

		public void AvailableOptions()
		{
			//hide
			var client = new ElasticLowLevelClient();
			//endhide

			var config = new ConnectionConfiguration()

				.DisableAutomaticProxyDetection()
				/** Disable automatic proxy detection.  Defaults to true. */

				.EnableHttpCompression()
				/**
				 * Enable compressed request and reesponses from Elasticsearch (Note that nodes need to be configured 
				 * to allow this.  See the [http module settings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html) for more info).
				*/

				.DisableDirectStreaming()
				 /**
				  * By default responses are deserialized off stream to the object you tell it to.
				  * For debugging purposes it can be very useful to keep a copy of the raw response on the result object. 
				  */;

			var result = client.Search<SearchResponse<object>>(new { size = 12 });
			var raw = result.ResponseBodyInBytes;
			/** This will only have a value if the client configuration has ExposeRawResponse set */

			/** 
			 * Please note that this only make sense if you need a mapped response and the raw response at the same time. 
			 * If you need a `string` or `byte[]` response simply call:
			 */
			var stringResult = client.Search<string>(new { });

			//hide
			config = config
				//endhide
				.GlobalQueryStringParameters(new NameValueCollection())
				/**
				* Allows you to set querystring parameters that have to be added to every request. For instance, if you use a hosted elasticserch provider, and you need need to pass an `apiKey` parameter onto every request.
				*/

				.Proxy(new Uri("http://myproxy"), "username", "pass")
				/** Sets proxy information on the connection. */

				.RequestTimeout(TimeSpan.FromSeconds(4))
				/**
				* Sets the global maximum time a connection may take.
				 * Please note that this is the request timeout, the builtin .NET `WebRequest` has no way to set connection timeouts 
				 * (see http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx).
				*/

				.ThrowExceptions()
				/**
				* As an alternative to the C/go like error checking on `response.IsValid`, you can instead tell the client to throw 
				* exceptions. 
				*
				* There are three category of exceptions thay may be thrown:
				*  
				* 1) ElasticsearchClientException: These are known exceptions, either an exception that occurred in the request pipeline
				* (such as max retries or timeout reached, bad authentication, etc...) or Elasticsearch itself returned an error (could 
				* not parse the request, bad query, missing field, etc...). If it is an Elasticsearch error, the `ServerError` property 
				* on the response will contain the the actual error that was returned.  The inner exception will always contain the 
				* root causing exception.
				*                                  
				* 2) UnexpectedElasticsearchClientException:  These are unknown exceptions, for instance a response from Elasticsearch not
				* properly deserialized.  These are usually bugs and should be reported.  This excpetion also inherits from ElasticsearchClientException
				* so an additional catch block isn't necessary, but can be helpful in distinguishing between the two.
				*
				* 3) Development time exceptions: These are CLR exceptions like ArgumentException, NullArgumentException etc... that are thrown
				* when an API in the client is misused.  These should not be handled as you want to know about them during development.
				*
				*/

				.PrettyJson()
				/**
				* Forces all serialization to be indented and appends `pretty=true` to all the requests so that the responses are indented as well
				*/

				.BasicAuthentication("username", "password")
				/** Sets the HTTP basic authentication credentials to specify with all requests. */;

			/**
			* **Note:** This can alternatively be specified on the node URI directly:
			 */

			var uri = new Uri("http://*****:*****@localhost:9200");
			var settings = new ConnectionConfiguration(uri);

			/**
			*  ...but may become tedious when using connection pooling with multiple nodes.
			*/
		}
Example #42
0
        public void SaveObject(Object obj)
        {
            Type typeObj = obj.GetType();
            if (!System.Configuration.ConfigurationManager.AppSettings["StorageType"].Equals("SQL"))
            {
                var node = new Uri(blueChappieSetttings.BlueChappieElasticServer + "/" + typeObj.Name + "lib/");
                var config = new ConnectionConfiguration(node);
                var client = new ElasticLowLevelClient(config);
                string imgInfo = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
                PostData<object> postingData = imgInfo;
                dynamic result0 = client.DoRequest<object>(HttpMethod.PUT, typeObj.Name, postingData);
            }

            else

            {
                System.Data.DataSet dataSet = new System.Data.DataSet();
                System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection("Database=BlueChappie;Server=localhost;uid=ProTrack2;pwd=protrack123;Connect Timeout=30;Min Pool Size=5;Max Pool Size=900;");
                System.Data.SqlClient.SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                sqlDataAdapter.SelectCommand = new System.Data.SqlClient.SqlCommand("SELECT TOP 0 * FROM " + typeObj.Name + "lib with (nolock)", sqlConnection);
                sqlDataAdapter.Fill(dataSet, typeObj.Name + "lib");
                System.Data.DataTable tbl = dataSet.Tables[0];
                System.Data.DataRow newRow = tbl.Rows.Add();
                foreach (System.Data.DataColumn fld in tbl.Columns)
                {
                    foreach (PropertyInfo imgFields in typeObj.GetProperties())
                    {
                        if (imgFields.Name.Equals(fld.ColumnName))
                        {
                            if (!fld.ReadOnly)
                            {
                                newRow.SetField(fld.ColumnName.ToString(), imgFields.GetValue(obj));
                            }
                        }
                    }
                }
                System.Data.DataRow dsRow = dataSet.Tables[typeObj.Name + "lib"].NewRow();
                dsRow = newRow;
                new SqlCommandBuilder(sqlDataAdapter);
                int result = sqlDataAdapter.Update(dataSet, typeObj.Name + "lib");
            }
        }
Example #43
0
 private void Initialise()
 {
     if (!initialised)
     {
         Uri host = new Uri(server);
         ConnectionConfiguration config = new ConnectionConfiguration(host);
         client = new ElasticLowLevelClient(config);
         initialised = true;
     }
 }
		/** # Connecting 
		 * Connecting to *Elasticsearch* with `Elasticsearch.Net` is quite easy but has a few toggles and options worth knowing.
		 * 
		 * # Choosing the right connection strategy
		 * If you simply new an `ElasticLowLevelClient`, it will be a non-failover connection to `http://localhost:9200`
		 */

		public void InstantiateUsingAllDefaults()
		{
			var client = new ElasticLowLevelClient();
			var tokenizers = new TokenizersDescriptor();

		}