public Bulk ( IList |
||
bulkObjects | IList |
|
return | ElasticSearch.Client.Domain.OperateResult |
public void TestBulk() { var client = new ElasticSearchClient("localhost"); var fields = new Dictionary<string, object>(); fields.Add("name", "jack"); fields.Add("age", 25); var index = "index_123123123121"; try { client.DeleteIndex(index); client.CreateIndex(index); } catch (Exception e) { Console.WriteLine(e); } var result = client.Bulk(new List<BulkObject>() { new BulkObject() { Id = "1", Index = index, Type = "type", Fields = fields }, new BulkObject() { Id = "2", Index = index, Type = "type", Fields = fields }, new BulkObject() { Id = "3", Index = index, Type = "type", Fields = fields } }); Assert.AreEqual(true, result.Success); client.Refresh(); var c = client.Count(index, "type", "age:25"); Assert.AreEqual(3, c); result = client.Delete(index, "type", new string[] { "1", "2", "3" }); Assert.AreEqual(true, result.Success); result=client.DeleteIndex(index); Assert.AreEqual(true, result.Success); }
public void CanIndexGetGemsGame() { var id = Guid.NewGuid().ToString(); var client = new ElasticSearchClient("ec2-107-22-42-34.compute-1.amazonaws.com", 9500, TransportType.Thrift); var data = new Dictionary<string, object> {{"GameId", "asdf"}}; var bulkObject = new BulkObject("gems","gameindex" , id, data); client.Bulk(new List<BulkObject> {bulkObject}); var loaded = client.Get("gems", "gameindex", id); Assert.IsNotNull(loaded); Assert.IsNotNull(loaded.GetFields()); Assert.AreEqual("asdf", loaded.GetFields()["GameId"]); }
private void IndexTransfer(string index, string toIndex, int from, int limit, int bulkSize, ElasticSearchClient srcClient, ElasticSearchClient descClient, bool complicatedSource,bool resolveTenant,bool showLog) { var docs = srcClient.Search(index, "*", from, limit, "_id:asc"); WriteLog("Search:{0},{1},{2}", index, from, limit); int i = 0; var bulkObjects = new List<BulkObject>(); if(complicatedSource) { //complicated object if (!string.IsNullOrEmpty(docs.Response)) { var obj = JObject.Parse(docs.Response); var hits = obj["hits"]["hits"]; foreach (var hit in hits) { var source = ((Newtonsoft.Json.Linq.JObject)(hit["_source"])).ToString().Replace("\r\n",string.Empty);// hit["_source"].Value<string>(); var _type = hit["_type"].Value<string>(); var _id = hit["_id"].Value<string>(); i++; if(showLog) { WriteLog("curl -XPOST http://localhost:9200/{0}/{1}/{2} -d'{3}'", toIndex, _type, _id, source); } bulkObjects.Add(new BulkObject(toIndex,_type, _id, source)); if (i > bulkSize) { descClient.Bulk(bulkObjects); bulkObjects.Clear(); i = 0; WriteLog("Buik Commit."); } } } } else { foreach (var variable in docs.GetHits().Hits) { #region logging // WriteLog("\tIndex:{0}", variable.Index); // WriteLog("\tType:{0}", variable.Type); // WriteLog("\tId:{0}", variable.Id); Dictionary<string, object> fields = variable.Fields; // WriteLog("\tTotalFieldsCount:{0}", fields.Count); #endregion #region // foreach (var VARIABLE in fields) // { //WriteLog(string.Format("\t\t{0}:{1}", VARIABLE.Key, VARIABLE.Value)); // } #endregion #region BulkInsert i++; bulkObjects.Add(new BulkObject( toIndex, variable.Type,variable.Id, fields)); if (i > bulkSize) { descClient.Bulk(bulkObjects); bulkObjects.Clear(); i = 0; WriteLog("Buik Commit."); } #endregion } } #region final cleanup if (i > 0) { descClient.Bulk(bulkObjects); WriteLog("Final Cleanup,{0}.", bulkObjects.Count); bulkObjects.Clear(); } #endregion }
public void TestBulkForFramdThrift() { var client = new ElasticSearchClient("localhost"); var fields = new Dictionary<string, object>(); fields.Add("name", "jack"); fields.Add("age", 25); var index = "index_bulk_framed"; try { client.DeleteIndex(index); client.CreateIndex(index); } catch (Exception e) { Console.WriteLine(e); } var jsondata = JsonSerializer.Get(fields); var result = client.Bulk(new List<BulkObject>() { new BulkObject() { Id = "1", Index = index, Type = "type", JsonData = jsondata }, new BulkObject() { Id = "2", Index = index, Type = "type", JsonData = jsondata }, new BulkObject() { Id = "3", Index = index, Type = "type", JsonData = jsondata }, new BulkObject() { Id = "4", Index = index, Type = "type", JsonData = jsondata }, new BulkObject() { Id = "5", Index = index, Type = "type",ParentId = "1", JsonData = jsondata }, new BulkObject() { Id = "6", Index = index, Type = "type",ParentId = "1", JsonData = jsondata }, new BulkObject() { Id = "7", Index = index, Type = "type",ParentId = "1", JsonData = jsondata }, new BulkObject() { Id = "8", Index = index, Type = "type",ParentId = "1", JsonData = jsondata }, }); Assert.AreEqual(true, result.Success); }