public IActionResult Elastic() { var settings = new ConnectionConfiguration(new Uri("http://*****:*****@" { ""query"": { ""multi_match"" : { ""query"": ""TOTACHI 5w масло"", ""type"": ""cross_fields"", ""fields"": [ ""name"" ], ""operator"": ""and"" } } } "); var searchResponse2 = lowlevelClient.Search <StringResponse>("products", "product", PostData.Serializable(new { query = new { multi_match = new { fields = new [] { "name" }, query = "TOTACHI 5w масло", type = "cross_fields", @operator = "and" } } })); var successful = searchResponse2.Success; var responseJson = searchResponse2.Body; var results = JObject.Parse(responseJson); StringBuilder sb = new StringBuilder(); foreach (var result in results["hits"]["hits"]) { var prod = result["_source"].ToObject <ProductDto>(); sb.AppendLine(prod.Name); } return(Content(sb.ToString())); }
public StringResponse Execute(ElasticsearchSearch query) { if (string.IsNullOrEmpty(query.Index)) { return(lowLevelClient.Search <StringResponse>(query.Query)); } else if (string.IsNullOrEmpty(query.Type)) { return(lowLevelClient.Search <StringResponse>(query.Index, query.Query)); } else { return(lowLevelClient.Search <StringResponse>(query.Index, query.Type, query.Query)); } }
public static string Search(string index, object option) { var searchResponse = _lowlevelClient.Search <StringResponse>(PostData.Serializable(option)); string responseJson = searchResponse.Body; return(responseJson); }
public IActionResult LowLevelSearchPersonByNameAsync(string name) { // Connecting using additional settings var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/")) .RequestTimeout(TimeSpan.FromMinutes(2)); var lowlevelClient = new ElasticLowLevelClient(settings); var searchResponse = lowlevelClient.Search <StringResponse>("people", PostData.Serializable(new { from = 0, size = 10, query = new { multi_match = new { fields = "firstName", query = name } } })); var responseJson = searchResponse.Body; return(Ok(responseJson)); }
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 Source[] GetNews() { string esInstance = Environment.GetEnvironmentVariable("ES_INSTANCE"); if (esInstance == "" || esInstance == null) { return(null); } var settings = new ConnectionConfiguration(new Uri(esInstance)) .RequestTimeout(TimeSpan.FromMinutes(2)); var lowlevelClient = new ElasticLowLevelClient(settings); var searchResponse = lowlevelClient.Search <StringResponse>("post", PostData.Serializable(new { size = 1000, query = new { match_all = new {} } })); var responseJson = searchResponse.Body; SearchResult searchResult = JsonSerializer.Deserialize <SearchResult>(responseJson); Source[] hits = searchResult.hits.hits; // foreach (Post post in hits) { // Console.WriteLine(post); // } return(hits); }
public string QueryLowLevel(string query) { var settings = new ConnectionConfiguration(new Uri("http://*****:*****@" { ""query"": { ""match"": { ""body"": """ + query + @""" } } }"); var successful = searchResponse.Success; var responseJson = searchResponse.Body; return(System.Text.Encoding.Default.GetString(responseJson)); }
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); }); }
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); }
public static DateTime GetLastDateTimeSerie(string databaseName, string collectionName, string timelineField, ElasticLowLevelClient lowLevalClient) { JObject json = new JObject( new JProperty("aggs", new JObject() { new JProperty("lastUpdated", new JObject() { new JProperty("max", new JObject() { new JProperty("field", timelineField) }) }) }) ); SearchRequestParameters param = new SearchRequestParameters(); param.SetQueryString("size", 0); var search = lowLevalClient.Search <StringResponse>(databaseName, collectionName, json.ToString(), param); if (search.Success) { var jsonResponse = JObject.Parse(search.Body); var longTicks = jsonResponse.SelectToken("aggregations.lastUpdated.value"); if (!string.IsNullOrEmpty(longTicks.ToString())) { return(DateTimeHelper.JavaTimeStampToDateTime(longTicks.Value <double>())); } } return(DateTime.MinValue); }
private string Query(string index, string type, string query) { ElasticsearchResponse <string> result = _client.Search <string>(index, type, query); var body = result.Body; return(body); }
/// <summary> /// Get documents that matches filter. /// </summary> /// <param name="type">Type name.</param> /// <param name="filter">Filter for species observations.</param> /// <returns>Documents that matches filter.</returns> protected DocumentFilterResponse GetDocuments(String type, String filter) { ElasticsearchResponse <DynamicResponse> response; Int32 startIndex, stopIndex; DocumentFilterResponse documentFilterResponse; String shardInformation, documentCount; String[] splitShardInformation; response = _client.Search <DynamicResponse>(IndexName, type, filter, qs => qs.AddQueryString("filter_path", "took,timed_out,_shards,hits.total,hits.hits._source")); CheckResponse(response); documentFilterResponse = new DocumentFilterResponse(); if (response.Body.IsNotNull()) { // Get shard information. shardInformation = (String)(response.Body.Values.ElementAt(2)); splitShardInformation = shardInformation.Split(':'); documentFilterResponse.ShardTotalCount = splitShardInformation[1].Substring(0, splitShardInformation[1].IndexOf(',')).WebParseInt32(); documentFilterResponse.ShardSuccessfulCount = splitShardInformation[2].Substring(0, splitShardInformation[2].IndexOf(',')).WebParseInt32(); documentFilterResponse.ShardFailedCount = splitShardInformation[3].Substring(0, splitShardInformation[3].IndexOf('}')).WebParseInt32(); documentFilterResponse.DocumentCount = 0; documentFilterResponse.DocumentsJson = (String)(response.Body.Values.ElementAt(3)); documentFilterResponse.TimedOut = (Boolean)(response.Body.Values.ElementAt(1)); if (!documentFilterResponse.TimedOut) { // Get species observation count. startIndex = documentFilterResponse.DocumentsJson.IndexOf(':') + 1; stopIndex = documentFilterResponse.DocumentsJson.IndexOf(','); if (stopIndex < 0) { // There are no character ',' in the response when 0 documents matched. stopIndex = documentFilterResponse.DocumentsJson.IndexOf('}'); } documentCount = documentFilterResponse.DocumentsJson.Substring(startIndex, stopIndex - startIndex); documentFilterResponse.DocumentCount = documentCount.WebParseInt64(); } //Debug.WriteLine("Total document count = " + // documentFilterResponse.DocumentCount + // ", time = " + documentFilterResponse.ElapsedTime + "ms" + // ", timed out = " + documentFilterResponse.TimedOut + "."); } return(documentFilterResponse); }
static async Task Main(string[] args) { // var lowlevelClient = new ElasticLowLevelClient(); var settings = new ConnectionConfiguration(new Uri("http://localhost:9200")).RequestTimeout(TimeSpan.FromMinutes(2)); var lowlevelClient = new ElasticLowLevelClient(settings); // var uris = new [] // { // new Uri("http://es01:9200"), // new Uri("http://es02:9201"), // new Uri("http://es03:9202"), // }; // var connectionPool = new SniffingConnectionPool(uris); // var settings = new ConnectionConfiguration(connectionPool); // var lowlevelClient = new ElasticLowLevelClient(settings); var person = new Person { FirstName = "Will", LastName = "Laarman" }; var ndexResponse = lowlevelClient.Index <BytesResponse>("people", "1", PostData.Serializable(person)); byte[] responseBytes = ndexResponse.Body; Console.WriteLine(); Console.WriteLine(System.Text.Encoding.UTF8.GetString(responseBytes)); Console.WriteLine(); var asyncIndexResponse = await lowlevelClient.IndexAsync <StringResponse>("people", "1", PostData.Serializable(person)); string responseString = asyncIndexResponse.Body; var searchResponse = lowlevelClient.Search <StringResponse>("people", PostData.Serializable(new { from = 0, size = 10, query = new { match = new { firstName = new { query = "Will" } } } })); var successful = searchResponse.Success; var successOrKnownError = searchResponse.SuccessOrKnownError; var exception = searchResponse.OriginalException; var responseJson = searchResponse.Body; Console.WriteLine(responseJson); }
protected override void Execute(CodeActivityContext context) { String Error = ""; bool Res = false; bool Success = false; String Resp = ""; try { var node = new Uri(URL.Get(context)); ConnectionConfiguration config; if (AuthenticationRequired.Get(context) == true) { config = new ConnectionConfiguration(node).BasicAuthentication(UserName.Get(context), Password.Get(context)); } else { config = new ConnectionConfiguration(node); } var lowlevelClient = new ElasticLowLevelClient(config); var searchData = lowlevelClient.Search <BytesResponse>(Index.Get(context), Type.Get(context), Query.Get(context)); var responseBytes = searchData.Body; if (responseBytes == null) { responseBytes = searchData.ResponseBodyInBytes; } Resp = Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length); Success = searchData.Success; if (Success == true) { Error = ""; Res = true; ErrorMessage.Set(context, Error); Response.Set(context, Resp); Result.Set(context, Res); } else { throw searchData.OriginalException; } } catch (Exception ex) { Error = "Failed to search for the data. " + ex.Message; Res = false; ErrorMessage.Set(context, Error); Response.Set(context, Resp); Result.Set(context, Res); } }
public QuerySearchResponse <T> MatchAll(string index, string type) { string json = "{\"query\": { \"match_all\": {} }}"; PostData <object> jsonPostData = new PostData <object>(json); ElasticsearchResponse <QuerySearchResponse <T> > result = _elasticsearchClient.Search <QuerySearchResponse <T> >(index, type, jsonPostData); return(result.Body); }
private static void UnsafeQueryTemplateSample(ElasticLowLevelClient client, string loopupTerm) { var aggsQueryFilePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "aggs-query.json"); var queryTemplate = ReadToEnd(aggsQueryFilePath); var queryToIssue = queryTemplate.Replace("@lookupTerm", $"\"{loopupTerm}\""); var result = client.Search <string>(queryToIssue); System.Console.WriteLine(result.HttpStatusCode); System.Console.WriteLine(result.Body); }
public AdminUser _GetById(int id) { AdminUser user = null; try { var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/")) .RequestTimeout(TimeSpan.FromMinutes(2)); var lowlevelClient = new ElasticLowLevelClient(settings); var searchResponse = lowlevelClient.Search <string>("user", "guest", new { query = new { match = new { Id = id } } }); bool successful = searchResponse.Success; if (!successful) { return(user); } var responseJson = searchResponse.Body; ESearchRoot <User> root = JsonHelper.JSONStringObject <ESearchRoot <User> >(responseJson); HitsItem <User> item = root.hits.hits[0]; if (item != null && item._source != null) { user = new AdminUser { Id = item._source.Id, RealName = item._source.RealName, Account = item._source.Account, Email = item._source.Email, Phone = item._source.Phone, //IsUse=item._source.IsUse, Avatar = item._source.Avatar, Description = item._source.Description, CreateTime = item._source.CreateTime, ModifyTime = item._source.ModifyTime }; } } catch (Exception ex) { } return(user); }
static void Test() { var nodes = new Uri[] { new Uri("http://10.204.124.222:9200"), }; var pool = new StaticConnectionPool(nodes); var settings = new ConnectionSettings(pool).DefaultIndex("photo_album_image"); var client = new ElasticClient(settings); var lowLevelClient = new ElasticLowLevelClient(settings); var data = new ImageModel { Description = " 说明 ", CreateTime = DateTime.Now, Filename = "filename.jpng", KeyWorld = new System.Collections.Generic.List <string> { "test" }, Id = Guid.NewGuid().ToString(), Location = "shengzheng", Subject = "有望", Videos = "test.mp4" }; var ndexResponse = lowLevelClient.Index <ImageModel>("索引", "PostData", PostData.Serializable(data)); var searchResponse = lowLevelClient.Search <StringResponse>("people", PostData.Serializable(new { from = 0, size = 10, query = new { match = new { firstName = new { query = "Martijn" } } } })); var successful = searchResponse.Success; var responseJson = searchResponse.Body; var response = client.IndexDocument(data); var t = response.Result; var searchReponse = client.Search <ImageModel>(s => s.From(0).Size(10).Query(q => q. Match(m => m.Field(f => f.Filename).Query("filename.jpng")))); var people = searchReponse.Documents; }
static void search(ElasticLowLevelClient client) { var request = new { query = new { term = new { Name = "Monique" } } }; var search = client.Search <string>("people", "person", request); var successful = search.Success; var responseJson = search.Body; Console.WriteLine("Search successful: {0}", successful); Console.WriteLine("Search Response: {0}", responseJson); }
// todo: error checking public string QueryElastic(string token, ref string doc_id) { string query = "{\"query\": {\"match\": {\"message\": {\"query\": \"" + token + "\", \"fuzziness\": \"AUTO\"}}}}"; PostData body = PostData.String(query); var response = client.Search <BytesResponse>(indexname, body); //Console.WriteLine(response.DebugInformation); dynamic obj = JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(response.Body)); string top_match = obj.hits.hits[0]._source.message; doc_id = obj.hits.hits[0]._id; return(top_match); }
//Execute the query private String executeQuery(String index, String query) { try { var searchResponse = client.Search <StringResponse>(index, query); lastError = ""; return(searchResponse.Body); } catch (Exception e) { logger.error("Exception when trying to execute a query! -> " + e.Message); lastError = e.Message; return(""); } }
static async Task Main(string[] args) { var settings = new ConnectionConfiguration(new Uri("http://localhost:9200")).RequestTimeout(TimeSpan.FromMinutes(2)); var lowlevelClient = new ElasticLowLevelClient(settings); var person = new { FirstName = "liu", LastName = "jie", Age = "18" }; //var indexResponse = lowlevelClient.Index<BytesResponse>("people", "1", PostData.Serializable(person)); //byte[] responseBytes = indexResponse.Body; //异步的方式 //如果不指定id,那么会每次添加都会自动生成一个id,就算是相同的数据,指定了的话,如果原先没有doc就会创建,已有的话就会update var asyncIndexResponse = await lowlevelClient.IndexAsync <StringResponse>("people", "2", PostData.Serializable(person)); string responseString = asyncIndexResponse.Body; Console.WriteLine(responseString); Console.ReadKey(); //search 搜索 var searchResponse = lowlevelClient.Search <StringResponse>("people", PostData.Serializable(new { from = 0, size = 10, query = new { match = new { LastName = new { query = "jie" } } } })); var successful = searchResponse.Success; var responseJson = searchResponse.Body; Console.WriteLine(successful); Console.WriteLine(responseJson); Console.ReadKey(); }
public void Search() { var searchResponse = lowlevelClient.Search <StringResponse>(index, mapping, PostData.Serializable(new { from = 0, size = 10, query = new { wildcard = new { Url = "*.com" } } })); var successful = searchResponse.Success; var responseJson = searchResponse.Body; }
static void Searching() { var searchResponse = client.Search <StringResponse>("people", "person", PostData.Serializable(new { from = 0, size = 10, query = new { match = new { field = "firstName", query = "Martijn" } } })); var successful = searchResponse.Success; var responseJson = searchResponse.Body; }
/** * 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 sometimes 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. The `.ThrowExceptions()` setting has no bearing on these as * they will always be thrown, and also should not be handled by a consumer. * */ }
public List <AdminUser> GetBySomeWhere(string keyword, int limit, int pageSize, out int total) { List <AdminUser> users = new List <AdminUser>(); total = 0; try { var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/")) .RequestTimeout(TimeSpan.FromMinutes(2)); var lowlevelClient = new ElasticLowLevelClient(settings); //根据不同的参数 来构建不同的查询条件 var request = new object(); if (!String.IsNullOrEmpty(keyword)) { request = new { from = limit, size = pageSize, query = new { match = new { Description = keyword } }, highlight = new { fields = new { Description = new { } } }, sort = new { Id = new { order = "desc" } } }; } else { request = new { from = limit, size = pageSize, query = new { match_all = new { } }, highlight = new { fields = new { Description = new { } } }, sort = new { Id = new { order = "desc" } } }; } var searchResponse = lowlevelClient.Search <string>("user", "guest", request); bool successful = searchResponse.Success; var responseJson = searchResponse.Body; if (!successful) { return(users); } ESearchRoot <User> root = JsonHelper.JSONStringObject <ESearchRoot <User> >(responseJson); if (root != null) { total = root.hits.total; foreach (HitsItem <User> item in root.hits.hits) { if (item._source != null) { string highlightDescription = String.Empty; StringBuilder sbDs = new StringBuilder(); if (item.highlight != null && item.highlight.Description.Count > 0) { //ighlightDescription = item.highlight.Description[0]; foreach (var d in item.highlight.Description) { sbDs.Append(d); } highlightDescription = sbDs.ToString(); } AdminUser user = new AdminUser { Id = item._source.Id, RealName = item._source.RealName, Account = item._source.Account, Email = item._source.Email, Phone = item._source.Phone, //IsUse=item._source.IsUse, Avatar = item._source.Avatar, Description = item._source.Description, HighlightDescription = highlightDescription, CreateTime = item._source.CreateTime, ModifyTime = item._source.ModifyTime }; users.Add(user); } } } return(users); } catch (ElasticsearchClientException ex) { //Log4Helper.Error } return(users); }
/** * 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. */ }
static async public Task checkNews() { var searchItems = new List <string> { "nextech ar", "hive blockchain stock", "covid-19 vaccine oxford", "imaginear", "YDX INNOVATION CORP", "vsby stock", "LOOP INSIGHTS INC" }; foreach (string searchText in searchItems) { // line of items of cli list string xml = await FetchData(searchText); XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); // only print the first few elements XmlNodeList items = doc.GetElementsByTagName("item"); DateTime utcDate = DateTime.UtcNow; for (int i = 0; i < 5; i++) { string pubDateStr = ""; try { pubDateStr = items[i]["pubDate"].InnerText; } catch (NullReferenceException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); continue; } DateTime pubDate = Convert.ToDateTime(pubDateStr); // Console.WriteLine(pubDate); if (utcDate.Subtract(pubDate).TotalHours < 24 * 2) { Console.WriteLine("Within 2 days"); // check if id is in db string esInstance = Environment.GetEnvironmentVariable("ES_INSTANCE"); var settings = new ConnectionConfiguration(new Uri(esInstance)) .RequestTimeout(TimeSpan.FromMinutes(2)); var lowlevelClient = new ElasticLowLevelClient(settings); var searchResponse = lowlevelClient.Search <StringResponse>("post", PostData.Serializable(new { query = new { match = new { guid = items[i]["guid"].InnerText// items[i]["guid"].InnerText } } })); var successful = searchResponse.Success; var responseJson = searchResponse.Body; SearchResult searchResult = JsonSerializer.Deserialize <SearchResult>(responseJson); if (searchResult.hits.total.value == 0) { SendItemToDiscord(items[i]); } else { Console.WriteLine("Match Already in DB, not going to print"); } } } } await Task.CompletedTask; }
public void Integration() { var builder = new ContainerBuilder(); builder.RegisterModule(new RootModule(@"Files\Shorthand.xml")); var container = builder.Build(); var pool = new SingleNodeConnectionPool(new Uri(ElasticConnection.Url)); var settings = new ConnectionConfiguration(pool); var client = new ElasticLowLevelClient(settings); // CORRECT DATA AND INITIAL LOAD using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) { cn.Open(); Assert.AreEqual(3, cn.Execute(@" UPDATE [Order Details] SET UnitPrice = 14.40, Quantity = 42 WHERE OrderId = 10253 AND ProductId = 39; UPDATE Orders SET CustomerID = 'CHOPS', Freight = 22.98 WHERE OrderId = 10254; UPDATE Customers SET ContactName = 'Palle Ibsen' WHERE CustomerID = 'VAFFE'; ")); } var root = ResolveRoot(container, SqlTestFile, true); var responseSql = new PipelineAction(root).Execute(); Assert.AreEqual(200, responseSql.Code); root = ResolveRoot(container, ElasticTestFile, true); var responseElastic = new PipelineAction(root).Execute(); Assert.AreEqual(200, responseElastic.Code); Assert.AreEqual(2155, client.Count <DynamicResponse>("northwind", "star", "{\"query\" : { \"match_all\" : { }}}").Body["count"].Value); // FIRST DELTA, NO CHANGES root = ResolveRoot(container, ElasticTestFile, false); responseElastic = new PipelineAction(root).Execute(); Assert.AreEqual(200, responseElastic.Code); Assert.AreEqual(string.Empty, responseElastic.Content); Assert.AreEqual(2155, client.Count <DynamicResponse>("northwind", "star", "{\"query\" : { \"match_all\" : { }}}").Body["count"].Value); // CHANGE 2 FIELDS IN 1 RECORD IN MASTER TABLE THAT WILL CAUSE CALCULATED FIELD TO BE UPDATED TOO using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) { cn.Open(); const string sql = @"UPDATE [Order Details] SET UnitPrice = 15, Quantity = 40 WHERE OrderId = 10253 AND ProductId = 39;"; Assert.AreEqual(1, cn.Execute(sql)); } // RUN AND CHECK SQL root = ResolveRoot(container, SqlTestFile, false); responseSql = new PipelineAction(root).Execute(); Assert.AreEqual(200, responseSql.Code); Assert.AreEqual(string.Empty, responseSql.Content); using (var cn = new SqlServerConnectionFactory(OutputConnection).GetConnection()) { cn.Open(); Assert.AreEqual(1, cn.ExecuteScalar <int>("SELECT TOP 1 Updates FROM NorthWindControl WHERE Entity = 'Order Details' AND BatchId = 9;")); Assert.AreEqual(15.0, cn.ExecuteScalar <decimal>("SELECT OrderDetailsUnitPrice FROM NorthWindStar WHERE OrderDetailsOrderId= 10253 AND OrderDetailsProductId = 39;")); Assert.AreEqual(40, cn.ExecuteScalar <int>("SELECT OrderDetailsQuantity FROM NorthWindStar WHERE OrderDetailsOrderId= 10253 AND OrderDetailsProductId = 39;")); Assert.AreEqual(15.0 * 40, cn.ExecuteScalar <int>("SELECT OrderDetailsExtendedPrice FROM NorthWindStar WHERE OrderDetailsOrderId= 10253 AND OrderDetailsProductId = 39;")); } // RUN AND CHECK ELASTIC root = ResolveRoot(container, ElasticTestFile, false); responseElastic = new PipelineAction(root).Execute(); Assert.AreEqual(200, responseElastic.Code); Assert.AreEqual(string.Empty, responseElastic.Content); var response = client.Search <DynamicResponse>( "northwind", "star", @"{ ""query"" : { ""constant_score"" : { ""filter"" : { ""bool"" : { ""must"" : [ { ""term"" : {""orderdetailsorderid"" : 10253}}, { ""term"" : {""orderdetailsproductid"" : 39}} ] } } } } }"); var hits = (response.Body["hits"]["hits"] as ElasticsearchDynamicValue).Value as IList <object>; var hit = hits[0] as IDictionary <string, object>; var source = hit["_source"] as IDictionary <string, object>; Assert.AreEqual(source["orderdetailsunitprice"], 15.0); Assert.AreEqual(source["orderdetailsquantity"], 40); Assert.AreEqual(source["orderdetailsextendedprice"], 40 * 15.0); // CHANGE 1 RECORD'S CUSTOMERID AND FREIGHT ON ORDERS TABLE using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) { cn.Open(); Assert.AreEqual(1, cn.Execute("UPDATE Orders SET CustomerID = 'VICTE', Freight = 20.11 WHERE OrderId = 10254;")); } // RUN AND CHECK SQL root = ResolveRoot(container, SqlTestFile, false); responseSql = new PipelineAction(root).Execute(); Assert.AreEqual(200, responseSql.Code); Assert.AreEqual(string.Empty, responseSql.Content); using (var cn = new SqlServerConnectionFactory(OutputConnection).GetConnection()) { cn.Open(); Assert.AreEqual(1, cn.ExecuteScalar <int>("SELECT Updates FROM NorthWindControl WHERE Entity = 'Orders' AND BatchId = 18;")); Assert.AreEqual("VICTE", cn.ExecuteScalar <string>("SELECT OrdersCustomerId FROM NorthWindStar WHERE OrderDetailsOrderId= 10254;")); Assert.AreEqual(20.11, cn.ExecuteScalar <decimal>("SELECT OrdersFreight FROM NorthWindStar WHERE OrderDetailsOrderId= 10254;")); } // RUN AND CHECK ELASTIC root = ResolveRoot(container, ElasticTestFile, false); responseElastic = new PipelineAction(root).Execute(); Assert.AreEqual(200, responseElastic.Code); Assert.AreEqual(string.Empty, responseElastic.Content); response = client.Search <DynamicResponse>( "northwind", "star", @"{ ""query"" : { ""constant_score"" : { ""filter"" : { ""bool"" : { ""must"" : [ { ""term"" : {""orderdetailsorderid"" : 10254}} ] } } } } }"); hits = (response.Body["hits"]["hits"] as ElasticsearchDynamicValue).Value as IList <object>; hit = hits[0] as IDictionary <string, object>; source = hit["_source"] as IDictionary <string, object>; Assert.AreEqual(source["orderscustomerid"], "VICTE"); Assert.AreEqual(source["ordersfreight"], 20.11); }
/** * 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. * */ }
private async void searchResult(string srv_url, string index, string type, string srch) { if (srv_url[srv_url.Length - 1] != '/') { srv_url += "/"; } if (!srv_url.StartsWith("http")) { srv_url = "http://" + srv_url; } int totalCnt = 30; int start = 0; try { var settings = new ConnectionConfiguration(new Uri(srv_url + index + "/" + type + "/")) .RequestTimeout(TimeSpan.FromMinutes(2)) .ConnectionLimit(-1); var lowlevelClient = new ElasticLowLevelClient(settings); StringResponse searchResponse; JObject details; while (start < totalCnt) { string requestBody = "{ \"from\":\"" + start.ToString() + "\", \"size\":\"10\", \"query\": { \"match\": { \"PdfText\": \"" + srch + "\"}}}"; start += 10; /*var requestBody = PostData.Serializable( * new * { * from = 0, * size = 30, * query = new * { * match = new * { * PdfText = srch * } * } * }); */ searchResponse = lowlevelClient.Search <StringResponse>(requestBody); Console.WriteLine("Search Finished"); if (searchResponse.Success) { var reponseJson = searchResponse.Body; Console.Write(reponseJson); details = JObject.Parse(reponseJson); totalCnt = (int)details["hits"]["total"]["value"]; for (int i = 0; i < details["hits"]["hits"].Count(); i++) { Abschluse pdf = new Abschluse(); pdf.Id = (int)details["hits"]["hits"][i]["_id"]; pdf.PdfLink = details["hits"]["hits"][i]["_source"]["PdfLink"].ToString(); pdf.PdfText = details["hits"]["hits"][i]["_source"]["PdfText"].ToString(); pdf.DocumentUrl = details["hits"]["hits"][i]["_source"]["DocumentUrl"].ToString(); m_lstPdfInfos.Add(pdf); } } else { /* * var dialog = new MessageDialog(searchResponse.DebugInformation); * await dialog.ShowAsync(); */ } } } catch (Exception e) { var dialog = new MessageDialog(e.Message + "\n" + m_lstPdfInfos.Count.ToString() + " Rows searched"); await dialog.ShowAsync(); Console.WriteLine(e); return; } var dialog1 = new MessageDialog(m_lstPdfInfos.Count.ToString() + " Rows searched"); await dialog1.ShowAsync(); }
/** * 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. */ }