static void Main(string[] args)
        {
            RiverConfiguration _riverConfiguration = (RiverConfiguration)ConfigurationManager.GetSection("riverConfiguration");

            ConnectionSettings searchSettings = new ConnectionSettings(new Uri(_riverConfiguration.ElasticSearchConnectionString));
            var searchClient =  new ElasticsearchClient(searchSettings);

            while (true)
            {
                //Connect to DB and listen for a message.
                using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["queueDatabase"].ConnectionString))
                {
                    sqlConnection.Open();

                    SqlCommand receiveCommand = new SqlCommand("WAITFOR( RECEIVE TOP(1) CONVERT(XML, message_body) AS Message FROM RiverUpdateReceiveQueue )", sqlConnection);
                    SqlCommand retrieveDataCommand;

                    RiverMessage riverMessage;
                    using (SqlDataReader receiveCommandReader = receiveCommand.ExecuteReader())
                    {
                        //Deserialize message.
                        receiveCommandReader.Read();
                        XmlSerializer serializer = new XmlSerializer(typeof(RiverMessage));
                        riverMessage = (RiverMessage)serializer.Deserialize(receiveCommandReader.GetXmlReader(0));

                        //Get the entire record out of the DB.
                        retrieveDataCommand = new SqlCommand(string.Format("SELECT TOP(1) * FROM {0} WHERE {1} = {2}", riverMessage.DatabaseTable, _riverConfiguration.Rivers.Cast<RiverElement>().First(x => x.DatabaseTable == riverMessage.DatabaseTable).tableId, riverMessage.Id), sqlConnection);
                    }

                    using (SqlDataReader retrieveDataCommandReader = retrieveDataCommand.ExecuteReader())
                    {
                        JObject item = null;
                        //Read it from the DB and store in a simple JSON object.

                        if (retrieveDataCommandReader.Read())
                        {
                            item = new JObject();
                            for (int i = 0; i < retrieveDataCommandReader.FieldCount; i++)
                            {
                                item[retrieveDataCommandReader.GetName(i)] = new JValue(retrieveDataCommandReader.GetValue(i));
                            }
                        }

                        //Foreach river that wants this type of record. Send it.
                        foreach (var riverRequired in _riverConfiguration.Rivers.Cast<RiverElement>().Where(x => x.DatabaseTable == riverMessage.DatabaseTable))
                        {
                            if (item != null)
                                searchClient.Index(riverRequired.ElasticIndex, riverRequired.ElasticType, riverMessage.Id.ToString(), item.ToString());
                            else
                                searchClient.Delete(riverRequired.ElasticIndex, riverRequired.ElasticType, riverMessage.Id.ToString());
                        }
                    }
                }
            }
        }
		public void AliasesWithDashesAreNotStripped()
		{
			//unique indexaname already contains dasshes but lets be sure about this
			//if the implementation ever changes
			var index = ElasticsearchConfiguration.NewUniqueIndexName() + "-dashes";
			var x = this.Client.CreateIndex(index);
			x.Acknowledged.Should().BeTrue();
			var alias = ElasticsearchConfiguration.NewUniqueIndexName() + "-dashed-alias";
			var aliasResult = this.Client.Alias(a => a.Add(aa => aa.Index(index).Alias(alias)));
			aliasResult.IsValid.Should().BeTrue();
			aliasResult.Acknowledged.Should().BeTrue();

			var getIndicesResult = Client.GetIndicesPointingToAlias(alias);
			getIndicesResult.Should().NotBeEmpty();

			var indexReturned = getIndicesResult.First();
			indexReturned.Should().Be(index).And.Contain("-dashes");


			var getAliasesResult = this.Client.GetAliasesPointingToIndex(index);
			getAliasesResult.Should().NotBeEmpty().And.HaveCount(1);

			var aliasReturned = getAliasesResult.First().Name;
			aliasReturned.Should().Be(alias).And.Contain("-dashed-alias");


			var elasticsearchClient = new ElasticsearchClient(ElasticsearchConfiguration.Settings());
			var dynamicResult = elasticsearchClient.IndicesGetAlias(index);
			IDictionary<string, object> aliases = dynamicResult.Response[index].aliases;
			aliases.Count.Should().Be(1);
			var aliasDynamic = aliases.Keys.First();
			aliasDynamic.Should().Be(alias);

		}
		/** 
		 * This however is still 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 `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 ElasticsearchClient(config);
		}
 public async Task<string> SearchAllAsync(string query, int @from, int size)
 {
     var client = new ElasticsearchClient(_settings);
     var queryString = "{\"from\" : " + from + ", \"size\" : " + size + ", \"query\":{\"match\": {\"name\": {\"query\": \" " + query + " \",\"operator\": \"and\"}}}}";
     var res = await client.SearchAsync<string>(Setting.ElasticSearchIndex,queryString);
     return res.Response;
 }
 public void Ping_should_work()
 {
     var settings = new ConnectionConfiguration(new Uri(TestConfig.Endpoint));
     var client = new ElasticsearchClient(settings, new AwsHttpConnection(settings, TestConfig.AwsSettings));
     var response = client.Ping();
     Assert.AreEqual(200, response.HttpStatusCode.GetValueOrDefault(-1));
 }
 public async Task<string> SearchIngredientsAsync(string query, int @from, int size)
 {
     var client = new ElasticsearchClient(_settings);
     
     var queryString = "{\"from\": " + from +", \"size\": " + size +", \"filter\": { \"or\": [{\"term\": { \"dataType\": \"hop\"}},{\"term\": {\"dataType\": \"fermentable\"}},{\"term\": {\"dataType\": \"yeast\"}},{\"term\": {\"dataType\": \"other\"}}]},\"query\": {\"match\": {\"name\": {\"query\": \"" + query +"\"}}}}";
     var res = await client.SearchAsync<string>(Setting.ElasticSearchIndex, queryString);
     return res.Response;
 }
Example #7
0
        public static Response GetItemsByName(string name, string league, int from, int size, ElasticsearchClient client)
        {
            string query = QueryStringItemByName(name, league, from, size);
            var result = client.Search<string>(query);
            Response response = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Response>(result.Response);

            return response;
        }
Example #8
0
        public SyncLock(ElasticsearchClient client, string index, string type)
        {
            Client = client;
            LockIndex = index;
            LockType = type;

            Open();
        }
        private IElasticsearchClient BuildElasticSearchClient(IContext context)
        {
            Configuration.ISettings settings = context.Kernel.Get<Configuration.ISettings>();

            ConnectionConfiguration config = new ConnectionConfiguration(new Uri(settings.ElasticSearch.Host));
            ElasticsearchClient client = new ElasticsearchClient(config);

            return client;
        }
        static public ElasticsearchClient Client(string url)
        {
            var key = url.ToLower();
            ElasticsearchClient client;
            if (allClients.TryGetValue(key, out client))
                return client;

            client = new ElasticsearchClient(Create(url));
            allClients[key] = client;
            return client;
        }
Example #11
0
 public async void Show()
 {
     var client = new ElasticsearchClient();
     var portfolioId = Guid.NewGuid();
     await new AsyncElasticsearchProjector(Projection.Handlers).
         ProjectAsync(client, new object[]
         {
             new PortfolioAdded {Id = portfolioId, Name = "My portfolio"},
             new PortfolioRenamed {Id = portfolioId, Name = "Your portfolio"},
             new PortfolioRemoved {Id = portfolioId}
         });
 }
Example #12
0
 public async void Show()
 {
     //Spin up a docker image of elastic search and/or change the endpoint below
     var config = new ConnectionConfiguration(new Uri("http://192.168.99.100:32769/"));
     var client = new ElasticsearchClient(config);
     var portfolioId = Guid.NewGuid();
     await new ConnectedProjector<ElasticsearchClient>(Resolve.WhenEqualToHandlerMessageType(Projection.Handlers)).
         ProjectAsync(client, new object[]
         {
             new PortfolioAdded {Id = portfolioId, Name = "My portfolio"},
             new PortfolioRenamed {Id = portfolioId, Name = "Your portfolio"},
             new PortfolioRemoved {Id = portfolioId}
         });
 }
		public void IndexWithDashesAreNotStripped2()
		{
			var index = ElasticsearchConfiguration.NewUniqueIndexName() + "-dashes";
			var x = this.Client.CreateIndex(index);
			x.Acknowledged.Should().BeTrue();
			var alias = ElasticsearchConfiguration.NewUniqueIndexName() + "-dashes-alias";
			var aliasResult = this.Client.Alias(a => a.Add(aa => aa.Index(index).Alias(alias)));
			aliasResult.IsValid.Should().BeTrue();
			aliasResult.Acknowledged.Should().BeTrue();

			var elasticsearchClient = new ElasticsearchClient(ElasticsearchConfiguration.Settings());
			var dynamicResult = elasticsearchClient.IndicesGetAlias(alias);
			dynamicResult.Response.ContainsKey(index).Should().BeTrue();
		}
        static public bool ShouldAdd(ElasticsearchClient client, ref CandidateFile candidate)
        {
            var searchQuery = new { query = new { term = new { _id = candidate.AliasedPath } } };
            ElasticsearchResponse<string> response;
            var timer = new ScopedTimer();
            using (timer)
            {
                response = client.Search<string>(Media.IndexName, JsonConvert.SerializeObject(searchQuery));
            }

            if (!FirstSearchMsec.HasValue)
            {
                FirstSearchMsec = timer.TotalMilliseconds;
            }

            if (!response.Success)
            {
                response.LogFailure(logger);
                return false;
            }

            candidate.Signature = CalculateSignature(candidate.FullFilename);
            candidate.LengthInBytes = new FileInfo(candidate.FullFilename).Length;


            var searchResponse = JsonConvert.DeserializeObject<SearchResponse>(response.Response);

//            if ((timer.TotalMilliseconds - searchResponse.Took) > 200)
//            {
//                logger.Warn("Search took {1} milliseconds for: {0}", candidate.AliasedPath, timer.TotalMilliseconds);
//            }

            // If there's more than one matching path or the signature is different, go through the add process
            // If it's not already in the index, add it.
            if (searchResponse.Hits.Total != 1)
                return true;

            var media = searchResponse.Hits.Hits.First().Media;
            if (media.Signature != candidate.Signature || media.LengthInBytes != candidate.LengthInBytes)
            {
                logger.Error("Add {0} due to mis-match ({0}, {1} -- {2}, {3})", candidate.AliasedPath,
                    media.Signature, media.LengthInBytes,
                    candidate.Signature, candidate.LengthInBytes);
                return true;
            }

            return false;
        }
Example #15
0
        /// <summary>
        /// Connect to ElasticSearch server, before connected all log entries will be catched in queue
        /// </summary>
        /// <param name="url"></param>
        public static void ConnectElasticSearch(string url = "http://localhost:9200/")
        {
            var node = new Uri(url);
            var config = new ConnectionConfiguration(node)
                                .EnableTrace(false)
                                .EnableMetrics(false)
                                .UsePrettyRequests(false)
                                .UsePrettyResponses(false);

            client = new ElasticsearchClient(config);
            consoleOnly = false;

            CancellationToken ct = stopToken.Token;
            Task taskConsumeQueue = Task.Run(() =>
            {
                while (true)
                {
                    // Bulk insert cached logs every 1 second
                    while (logEntries.IsEmpty)
                    {
                        System.Threading.Thread.Sleep(1000);

                        // <- Stop()
                        if (ct.IsCancellationRequested)
                        {
                            return;
                        }
                    }

                    int batchLength = 0;
                    var builder = new StringBuilder();
                    var timestamp = "logger-" + DateTime.UtcNow.ToString("yyyy-MM-dd");
                    var indexOp = "{ \"index\" : { \"_index\" : \"" + timestamp + "\", \"_type\" : \"log\" } }";
                    while (logEntries.IsEmpty == false && batchLength < 100)
                    {
                        JObject entry;
                        if (logEntries.TryDequeue(out entry))
                        {
                            builder.AppendLine(indexOp);
                            builder.AppendLine(entry.ToString(Formatting.None));
                            batchLength++;
                        }
                    }
                    if (batchLength > 0)
                        client.BulkAsync(builder.ToString());
                }
            });
        }
        public void Calling_A_Null_Field_Should_Not_Throw_With_ElasticsearchNet_Serializer()
        {
            var client = new ElasticsearchClient();
            var result = client.Search("{ size: 10}");
            var hit = result.Response["hits"]["hits"][0];

            Assert.DoesNotThrow(() => { var x = hit["testfield"]; });
            var exists = false;
            Assert.DoesNotThrow(() => { exists = hit["testfield"] != null; });
            exists.Should().BeFalse();
            Assert.DoesNotThrow(() => { exists = hit["_index"] != null; });
            exists.Should().BeTrue();

            var source = hit["_source"];
            ((object) source).Should().NotBeNull();
            Assert.DoesNotThrow(() => { var x = source["name"]; });
            string field = source["name"];

            field.Should().NotBeNullOrWhiteSpace();
        }
Example #17
0
        public SyncResponse Exec()
        {
            var startedOn = DateTime.UtcNow;
            log.Info("process started at " + startedOn.NormalizedFormat());
            client = new ElasticsearchClient(_config.ElasticSearchConfiguration);

            using (var _lock = new SyncLock(client, LogIndex, LockType))
            {
                DateTime? lastSyncDate = ConfigureIncrementalProcess(_config.SqlCommand, _config.ColumnsToCompareWithLastSyncDate);

                var data = GetSerializedObject();
                log.Info(String.Format("{0} objects have been serialized.", data.Count()));

                var syncResponse = new SyncResponse(startedOn);

                syncResponse = IndexProcess(data, syncResponse);

                if (_config.DeleteConfiguration != null)
                {
                    _config.SqlConnection.Open();
                    Dictionary<object, Dictionary<string, object>> deleteData = null;

                    if (lastSyncDate != null)
                        ConfigureIncrementalProcess(_config.DeleteConfiguration.SqlCommand, _config.DeleteConfiguration.ColumnsToCompareWithLastSyncDate, lastSyncDate);

                    using (SqlDataReader rdr = _config.DeleteConfiguration.SqlCommand.ExecuteReader())
                    {
                        deleteData = rdr.Serialize();
                    }
                    _config.SqlConnection.Close();

                    syncResponse = DeleteProcess(deleteData, syncResponse);
                }

                syncResponse = Log(syncResponse);

                log.Info(String.Format("process duration: {0}ms", Math.Truncate((syncResponse.EndedOn - syncResponse.StartedOn).TotalMilliseconds)));

                return syncResponse;
            }
        }
        public ElasticsearchDestinationWriter(DocumentDatabase database, SqlReplicationConfig _cfg, SqlReplicationStatistics replicationStatistics)
		{
            var cfg = new ElasticsearchReplicationConfig(_cfg);

			this.database = database;
			this.cfg = cfg;
            this.targetIndexName = cfg.FactoryName.ToLowerInvariant(); // Elasticsearch requires all index names to be lowercased
			this.replicationStatistics = replicationStatistics;
            
            try
            {
                elasticsearchClient = cfg.GetElasticClient();
            }
            catch (UriFormatException e)
            {
                if (database != null)
                    database.AddAlert(new Alert
                    {
                        AlertLevel = AlertLevel.Error,
                        CreatedAt = SystemTime.UtcNow,
                        Exception = e.ToString(),
                        Title = "Invalid Elasticsearch URL provided",
                        Message = "Elasticsearch Replication could not parse one of the provided node URLs",
                        UniqueKey = "Elasticsearch Replication Connection Error: " + cfg.ConnectionString
                    });
            }
			catch (Exception e)
			{
			    if (database != null)
			        database.AddAlert(new Alert
			        {
			            AlertLevel = AlertLevel.Error,
			            CreatedAt = SystemTime.UtcNow,
			            Exception = e.ToString(),
			            Title = "Elasticsearch Replication could not open connection",
			            Message = "Elasticsearch Replication could not open connection to " + cfg.ConnectionString,
			            UniqueKey = "Elasticsearch Replication Connection Error: " + cfg.ConnectionString
			        });
				throw;
			}
		}
        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;

            var configuration = new ConnectionConfiguration(options.ConnectionPool)
                .SetTimeout(options.ConnectionTimeout)
                .SetMaximumAsyncConnections(20);

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

            _client = new ElasticsearchClient(configuration, connection: options.Connection, serializer: options.Serializer);

            _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
               );

            this._registerTemplateOnStartup = options.AutoRegisterTemplate;
        }
        static public void Add(ElasticsearchClient client, IEnumerable<Media> mediaList)
        {
            var bulk = new List<string>();
            foreach (var media in mediaList)
            {
                bulk.Add(JsonConvert.SerializeObject(new BulkIndexCommand(Media.IndexName, Media.TypeName, media.Path)));
                bulk.Add(JsonConvert.SerializeObject(media, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }));
            }

            var response = client.Bulk<string>(bulk);
            if (!response.Success)
            {
                response.LogFailure(logger, "insert failed");
                return;
            }

            var bulkResponse = JsonConvert.DeserializeObject<BulkResponse>(response.Response);
            if (bulkResponse.Errors)
            {
                logger.Error("At least some inserts failed: {0}", response.Response);
            }
        }
Example #21
0
        static void Main(string[] args)
        {
            var uri = new Uri(URI_STRING);
            var settings = new ConnectionConfiguration(uri)
                .PrettyJson(true)
                .UsePrettyResponses(true);
            var client = new ElasticsearchClient(settings);

            ItemNamesDataContext itemNamesDB = new ItemNamesDataContext(DATA_CONNETION_STRING);
            ItemsDataContext itemsDB = new ItemsDataContext(DATA_CONNETION_STRING);

            while (true)
            {
                // Ordering so we prioritize items that haven't been updated in a while (or ever - so null = yesterday)
                foreach (ItemName itemName in itemNamesDB.ItemNames.OrderBy(row => row.LastChecked ?? DateTime.Now.AddDays(-1)))
                {
                    string[] leagues = {
                    "Standard", "Hardcore", "Warbands", "Tempest"
                };

                    foreach (string league in leagues)
                    {
                        int amountOfItems;
                        int scanned = 0;
                        List<float> prices = new List<float>();

                        do
                        {
                            Response response;
                            try
                            {
                                response = GetItemsByName(itemName.Name, league, scanned, 200, client);

                                amountOfItems = response.hits.total;
                                scanned += response.hits.hits.Count();

                                foreach (Hit hit in response.hits.hits)
                                {
                                    prices.Add(hit._source.shop.chaosEquiv);
                                }
                            }
                            catch (System.InvalidOperationException e)
                            {
                                // Deserializer found null value -> query resulted empty response
                                amountOfItems = 0;
                            }

                            // Just to monitor status
                            Console.WriteLine(string.Format("{0} / {1}", scanned, amountOfItems));

                            // Let's try to not get banned
                            System.Threading.Thread.Sleep(2000);
                        } while (scanned < amountOfItems);

                        prices.Sort();

                        var entry = new Item
                        {
                            Name = itemName.Name,
                            Amount = amountOfItems,
                            League = league,
                            MaxPrice = prices.Any() ? prices.Last() : 0,
                            MinPrice = prices.Any() ? prices.First() : 0,
                            MeanPrice = prices.Any() ? prices.Average() : 0,
                            MedianPrice = prices.Any() ? prices[prices.Count() / 2] : 0,
                            Timestamp = DateTime.Now
                        };

                        itemsDB.Items.InsertOnSubmit(entry);
                        Console.WriteLine(string.Format("min_price: {0}, max_price: {1}, median: {2}, mean: {3}, item: {4}",
                            entry.MinPrice, entry.MaxPrice, entry.MedianPrice, entry.MeanPrice, itemName.Name));
                    }

                    itemName.LastChecked = DateTime.Now;

                    try
                    {
                        itemNamesDB.SubmitChanges();
                        itemsDB.SubmitChanges();
                    }
                    catch (Exception e)
                    {
                        // DB might be busy, or being accessed by someone else and submitting may fail
                        // It happens only every few hundred submits so I decided to just keep ignore it and continue

                        continue;
                    }
                }
            }
        }
Example #22
0
 public void PostFood(Food food)
 {
     var ESclient = new ElasticsearchClient();
     var response = ESclient.Index<Food>("food", food.Type, food.Id, food);
     redDB.StringSet(food.Id, food.Name);
 }
		/**
		 * The following is a list of available connection configuration options:
		 */

		public void AvailableOptions()
		{
			//hide
			var client = new ElasticsearchClient();
			//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.
			*/
		}
        public async Task SearchAllElasticSearch()
        {
            var node = new Uri("http://localhost:9200");
            var settings = new ConnectionConfiguration(node);
            var client = new ElasticsearchClient(settings);

            var query = "{\"query\" : { \"term\": { \"name\" : \"safale\"}}}";
            var res = client.Search<string>(Setting.ElasticSearchIndex, query);
            dynamic json = JsonConvert.DeserializeObject(res.Response);
            var result = json.hits.hits; 
           
         }
		/**
		 * If your Elasticsearch node does not live at `http://localhost:9200` but i.e `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 ElasticsearchClient(config);
		}
		/** # 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 `ElasticsearchClient`, it will be a non-failover connection to `http://localhost:9200`
		 */

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

		}
 public IndexMediaQueue(Func<bool> dependencyIsActive, int numTasks, string serverUrl, Action<int> itemsIndexed)
     : base(dependencyIsActive, numTasks)
 {
     this.itemsIndexed = itemsIndexed;
     this.client = FindAPhotoConnectionSettings.Client(serverUrl);
 }
		/**
		 * The following is a list of available connection configuration options:
		 */

		public void AvailableOptions()
		{
			//hide
			var client = new ElasticsearchClient();
			//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).
				*/

				.EnableMetrics()
				/** Enable more meta data to be returned per API call about requests (ping, sniff, failover, and general stats). */

				.EnableTrace()
				/**
				* Will cause `Elasticsearch.Net` to write connection debug information on the TRACE output of your application.
				*/

				.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
				.SetConnectionStatusHandler(s => { })
				/** 
				* Allows you to pass a `Action&lt;IElasticsearchResponse&gt;` that can eaves drop every time a response (good or bad) is created. If you have complex logging needs 
				* this is a good place to add that in.
				*/

				.SetGlobalQueryStringParameters(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.
				*/

				.SetProxy(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).
				*/

				//TODO document this properly once we figure out exceptions
				.ThrowExceptions()
				/**
				* As an alternative to the C/go like error checking on `response.IsValid`, you can instead tell the client to always throw 
				 * an `ElasticsearchServerException` when a call resulted in an exception on the Elasticsearch server. Reasons for 
				 * such exceptions could be search parser errors and index missing exceptions.
				*/

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

				.SetBasicAuthentication("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 #29
0
 public void GetServerHealth()
 {
     var client = new ElasticsearchClient();
     var result = client.CatHealth(param => param.V(true));
     Assert.IsNotNull(result);
 }
Example #30
0
 public void GetIndicesStatus()
 {
     var client = new ElasticsearchClient();
     var result = client.CatIndices(param => param.V(true));
     Assert.IsNotNull(result);
 }
        public static Task <ElasticsearchResponse <T> > IndicesPutAliasAsync <T>(this ElasticsearchClient client, string index, string name, object body, Func <IndicesPutAliasRequestParameters, IndicesPutAliasRequestParameters> requestParameters = null)
        {
            var selector = Obsolete.UpCastSelector <IndicesPutAliasRequestParameters, PutAliasRequestParameters>(requestParameters);

            return(client.IndicesPutAliasAsync <T>(index, name, selector));
        }