public void UploadDataToElastic(List <SearchEntity> entities) { List <string> bulkUploads = new List <string>(); StringBuilder bulk = new StringBuilder(100000); /* * Should look like this (for animal): * * { "create" : { "_index" : "zoo", "_type" : "animal", "_id" : "1" } } * { "name" : "White Fang", "kind" : "Wolf", "age": 3 } */ for (int i = 0; i < entities.Count; i += 1) { bulk.Append("{\"create\":{\"_index\":\"psychologists\",\"_type\":\"psychologist\",\"_id\":\"" + i + "\"}}\n"); bulk.Append($"{{\"id\":\"{entities[i].Id}\",\"nickName\":\"{entities[i].NickName}\",\"education\":\"{entities[i].Education}\",\"areaOfExpertise\":\"{entities[i].AreaOfExpertise}\",\"isAccountActivated\": \"{entities[i].IsAccountActivated}\"}}\n"); if (i % 1000 == 0) { bulkUploads.Add(bulk.ToString()); bulk.Clear(); } } if (bulk.Length != 0) { bulkUploads.Add(bulk.ToString()); bulk.Clear(); } var uri = new Uri(_elasticURL + "/_bulk"); for (int i = 0; i < bulkUploads.Count; i++) { try { string response = CustomHttpSender.SendHttpRequest(uri, "POST", bulkUploads[i]); if (response.Contains("\"errors\":true")) { Console.WriteLine("data upload warning"); } else { Console.WriteLine($"data uploaded {i + 1} out of {bulkUploads.Count}"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
//query data from whole elastic public string AutocompleteDataFromElastic(string userSearch) { string requestUrl = string.Format("{0}/{1}/_search/?size=10", _elasticURL, _indexName); string requestBody = "{\"query\":{\"match_phrase_prefix\":{\"nickName\": \" " + userSearch + " \"}}}"; try { return(CustomHttpSender.SendHttpRequest(new Uri(requestUrl), "POST", requestBody)); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(string.Empty); }
//simple search public string SearchUserNames(string userSearch, int numberOfResult) { string requestUrl = string.Format("{0}/{1}/_search/?size={2}", _elasticURL, _indexName, numberOfResult); string requestBody = "{\"query\":{\"match\":{\"nickName\": \" " + userSearch + " \"}}}"; try { return(CustomHttpSender.SendHttpRequest(new Uri(requestUrl), "POST", requestBody)); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(string.Empty); }
public void CreateElasticIndex() { try { var uri = new Uri(_elasticURL + "/" + _indexName); string response = CustomHttpSender.SendHttpRequest(uri, "PUT"); if (!response.Contains("\"acknowledged\":true")) { Console.WriteLine(response); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public void DeleteExistingType(string typeName) { var path = new Uri(_elasticURL + "/" + _indexName + "/" + typeName); try { string response = CustomHttpSender.SendHttpRequest(path, "DELETE"); if (!response.Contains("\"acknowledged\":true")) { Console.WriteLine(response); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
//creates new type (table) in elastic public void CreateNewType(string typeName, string typeBody) { var path = new Uri(_elasticURL + "/" + _indexName + "/_mapping/" + typeName); try { string response = CustomHttpSender.SendHttpRequest(path, "PUT", typeBody); if (!response.Contains("\"acknowledged\":true")) { Console.WriteLine(response); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public void DeleteElasticIndex() { try { string response = CustomHttpSender.SendHttpRequest(new Uri(_elasticURL + "/" + _indexName), "DELETE"); if (!response.Contains("\"acknowledged\":true")) { Console.WriteLine(response); } else { Console.WriteLine("index deleted"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
//take json file and set it to elastic (creates index/type & apply rules for mapping) public void CreateElasticFromIndexSettings() { string requestBody = ReadElasticSettings(ConfigurationManager.AppSettings["IndexSettingsPath"]); var path = new Uri(_elasticURL + "/" + _indexName); try { string response = CustomHttpSender.SendHttpRequest(path, "PUT", requestBody); if (response.Contains("\"errors\":true")) { Console.WriteLine(response); } else { Console.WriteLine("index created"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }