/// <summary> /// 添加一条记录 /// </summary> /// <param name="id"></param> /// <param name="jsonDocument"></param> /// <returns></returns> public bool Index(string id, string jsonDocument) { if (_indexInfo == null) { throw new ArgumentNullException(); } string cmd = new IndexCommand(_indexInfo.IndexName, _indexInfo.IndexType, id); OperationResult result = Client.Put(cmd, jsonDocument); var indexResult = serializer.ToIndexResult(result.Result); return(indexResult.created); }
private static void IndexTweet(Tweet tweet, string id, ElasticConnection connection, JsonNetSerializer serializer) { /* * $ curl -XPUT 'http://localhost:9200/twitter/tweet/1?pretty=true' -d '{ * "User" : "testUser", * "Message" : "trying out Elastic Search" * }' */ // This is url that will be requested from ES. We can grab it and put to any ES admin console (like ElasticHead) to debug ES behavior. string indexCommand = Commands.Index(index: "twitter", type: "tweet", id: id) .Refresh(true) .Pretty(); // this will generate: twitter/tweet/1?pretty=true // This variable contains JSON of serialized tweet, thus we can check if our object serialized correctly // or use it directly in ES admin console. string tweetJson = serializer.ToJson(tweet); var result = connection.Put(indexCommand, tweetJson); // Parse index result. IndexResult indexResult = serializer.ToIndexResult(result); PrintIndexCommand(result, indexResult, indexCommand, tweetJson); }
static void Main1(string[] args) { /* * Person p1 = new Person(); * p1.Id = 1; * p1.Age = 10; * p1.Name = "欧阳帅帅"; * p1.Desc = "欧阳锋家的帅哥公子,人送外号‘小杨中科’";*/ Person p1 = new Person(); p1.Id = 2; p1.Age = 8; p1.Name = "丑娘娘"; p1.Desc = "二丑家的姑娘,是最美丽的女孩"; ElasticConnection client = new ElasticConnection("localhost", 9200); var serializer = new JsonNetSerializer(); //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键” IndexCommand cmd = new IndexCommand("zsz", "persons", p1.Id.ToString()); //Put()第二个参数是要插入的数据 OperationResult result = client.Put(cmd, serializer.Serialize(p1)); var indexResult = serializer.ToIndexResult(result.Result); if (indexResult.created) { Console.WriteLine("创建了"); } else { Console.WriteLine("没创建" + indexResult.error); } Console.ReadKey(); }
static void Main1(string[] args) { Person p2 = new Person(); p2.Id = 3; p2.Age = 10; p2.Name = "handsome person"; p2.Desc = "handsome look, ugly heart"; //Person p1 = new Person(); //p1.Id = 4; //p1.Age = 8; //p1.Name = "ugly person"; //p1.Desc = "ugly look, nice heart"; ElasticConnection client = new ElasticConnection("localhost", 9200); var serializer = new JsonNetSerializer(); //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键” IndexCommand cmd = new IndexCommand("zsz", "persons", p2.Id.ToString()); //Put()第二个参数是要插入的数据 OperationResult result = client.Put(cmd, serializer.Serialize(p2)); var indexResult = serializer.ToIndexResult(result.Result); if (indexResult.created) { Console.WriteLine("created"); } else { Console.WriteLine("faled: " + indexResult.error); } Console.ReadKey(); }
static void Main1() { Person p1 = new Person(); p1.Id = 1; p1.Age = 8; p1.Name = "ll"; p1.Desc = "最美丽的女孩"; ElasticConnection client = new ElasticConnection("localhost", 9200); var serializer = new JsonNetSerializer(); //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键” IndexCommand cmd = new IndexCommand("Hlxzsz", "persons", p1.Id.ToString()); //Put()第二个参数是要插入的数据 OperationResult result = client.Put(cmd, serializer.Serialize(p1)); var indexResult = serializer.ToIndexResult(result.Result); if (indexResult.created) { Console.WriteLine("创建了"); } else { Console.WriteLine("没创建" + indexResult.error); } Console.ReadKey(); }
/// <summary> /// 数据索引 /// </summary> /// <param name="indexName">索引名称</param> /// <param name="indexType">索引类型</param> /// <param name="id">索引文档id,不能重复,如果重复则覆盖原先的</param> /// <param name="jsonDocument">要索引的文档,json格式</param> /// <returns></returns> public IndexResult Index(string indexName, string indexType, string id, string jsonDocument) { var serializer = new JsonNetSerializer(); string cmd = new IndexCommand(indexName, indexType, id); OperationResult result = Client.Put(cmd, jsonDocument); var indexResult = serializer.ToIndexResult(result.Result); return(indexResult); }
public ActionResult Add(HouseAddModel model) { long?userId = AdminHelper.GetUserId(HttpContext); long?cityId = userSerivce.GetById(userId.Value).CityId; if (cityId == null) { return(View("Error", (object)"总部不能进行房源管理")); } HouseAddnewDTO dto = new HouseAddnewDTO(); dto.Address = model.address; dto.Area = model.area; dto.AttachmentIds = model.attachmentIds; dto.CheckInDateTime = model.checkInDateTime; dto.CommunityId = model.CommunityId; dto.DecorateStatusId = model.DecorateStatusId; dto.Description = model.description; dto.Direction = model.direction; dto.FloorIndex = model.floorIndex; dto.LookableDateTime = model.lookableDateTime; dto.MonthRent = model.monthRent; dto.OwnerName = model.ownerName; dto.OwnerPhoneNum = model.ownerPhoneNum; dto.RoomTypeId = model.RoomTypeId; dto.StatusId = model.StatusId; dto.TotalFloorCount = model.totalFloor; dto.TypeId = model.TypeId; long houseId = houseService.AddNew(dto); //生成房源查看的html文件 CreateStaticPage(houseId); //把房源信息写入ElasticSearch //创建与SQLLite的连接 ElasticConnection client = new ElasticConnection("localhost", 9200); var serializer = new JsonNetSerializer(); //写入数据 //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键” IndexCommand indexcmd = new IndexCommand("ZSZHouse", "House", houseId.ToString()); //不用手动创建数据库,es会自动分配空间用zsz命名 //Put()第二个参数是要插入的数据 OperationResult result = client.Put(indexcmd, serializer.Serialize(dto));//把对象序列化成json放入Elastic中返回结果 return(Json(new AjaxResult { Status = "ok" })); }
public void UpdateHeatbeat(string time) { try { var beat = _serializer.Serialize(new ElasticHeartBeat { Date = _time.Now, MachineName = Environment.MachineName }); _connection.Put(new IndexCommand("counter", "heartbeat", Environment.MachineName), beat); } catch (Exception) { } }
private static void CreateIndexAlias(ElasticConnection connection, JsonNetSerializer serializer) { /* * curl -XPUT http://localhost:9200/twitter/_alias/twitter_alias */ string indexAliasCommand = Commands.IndexAlias("twitter", "twitter_alias") .Pretty(); var result = connection.Put(indexAliasCommand); // Parse index result. var indexAliasResult = serializer.ToCommandResult(result); PrintIndexAliasResult(indexAliasResult, indexAliasCommand, result); }
private void Post(EventEntry entry) { try { ElasticConnection elasticConnection = new ElasticConnection(_connectionString); string key = Guid.NewGuid().ToString(); string body = entry.ToJson(); var command = Commands.Index(GetIndex(entry), entry.Schema.ProviderName, key); var result = elasticConnection.Put(command, body); } catch (Exception error) { SemanticLoggingEventSource.Log.CustomSinkUnhandledFault(error.Message); } }
static void Main1(string[] args) { Person p1 = new Person { Id = 2, Age = 11, Name = "独孤求败", Desc = "剑客" }; ElasticConnection client = new ElasticConnection("127.0.0.1", 9200); var serializer = new JsonNetSerializer(); //第一个相当于"数据库",第二个相当于表,第三个相当于主键 IndexCommand cmd = new IndexCommand("MyTest", "persons", p1.Id.ToString()); //数据区域 OperationResult result = client.Put(cmd, serializer.Serialize(p1)); //放进去 var indexResult = serializer.ToIndexResult(result.Result); if (indexResult.created) { Console.WriteLine("创建了"); } else { Console.WriteLine("创建失败" + indexResult.error); } Console.ReadKey(); }
static void Main1(string[] args) { using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=d:/verycd.sqlite3.db")) { conn.Open(); using (var cmd = conn.CreateCommand()) { cmd.CommandText = "select * from verycd"; using (var reader = cmd.ExecuteReader()) { ElasticConnection client = new ElasticConnection("localhost", 9200); var serializer = new JsonNetSerializer(); while(reader.Read()) { long verycdid =reader.GetInt64(reader.GetOrdinal("verycdid")); string title = reader.GetString(reader.GetOrdinal("title")); string status = reader.GetString(reader.GetOrdinal("status")); string brief = reader.GetString(reader.GetOrdinal("brief")); string pubtime = reader.GetString(reader.GetOrdinal("pubtime")); string updtime = reader.GetString(reader.GetOrdinal("updtime")); string category1 = reader.GetString(reader.GetOrdinal("category1")); string category2 = reader.GetString(reader.GetOrdinal("category2")); string ed2k = reader.GetString(reader.GetOrdinal("ed2k")); string content = reader.GetString(reader.GetOrdinal("content")); string related = reader.GetString(reader.GetOrdinal("related")); VerycdItem item = new VerycdItem(); item.verycdid = verycdid; item.title = title; item.status = status; item.brief = brief; item.pubtime = pubtime; item.updtime = updtime; item.category1 = category1; item.category2 = category2; item.ed2k = ed2k; item.content = content; item.related = related; try { //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键” IndexCommand Indexcmd = new IndexCommand("verycd", "items", item.verycdid.ToString()); //Put()第二个参数是要插入的数据 OperationResult result = client.Put(Indexcmd, serializer.Serialize(item)); var indexResult = serializer.ToIndexResult(result.Result); if (indexResult.created) { Console.WriteLine(verycdid + "创建了"); } else { Console.WriteLine(verycdid + "没创建" + indexResult.error); } } catch(Exception ex) { continue; } } Console.ReadKey(); } } } }
/// <summary> /// 把sqllite中的数据存入elasticSearch中 /// </summary> /// <param name="args"></param> static void Main3(string[] args) { //安装sqlLite的ado.net sdk:Install-Package System.Data.SQLite.Core //创建SQLite数据库连接(直接连接文件地址) using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=C:\Users\Administrator\Desktop\掌上租项目\电驴数据库(全部电影)\verycd.sqlite3.db")) { //打开数据库连接 conn.Open(); using (var cmd = conn.CreateCommand()) { cmd.CommandText = "select * from verycd"; //读取数据 using (var reader = cmd.ExecuteReader()) { //连接elasticSearch ElasticConnection client = new ElasticConnection("localhost", 9200); var serializer = new JsonNetSerializer(); while (reader.Read()) { //将读取的数据赋值 long verycdid = reader.GetInt64(reader.GetOrdinal("verycdid")); string title = reader.GetString(reader.GetOrdinal("title")); string status = reader.GetString(reader.GetOrdinal("status")); string brief = reader.GetString(reader.GetOrdinal("brief")); string pubtime = reader.GetString(reader.GetOrdinal("pubtime")); string updtime = reader.GetString(reader.GetOrdinal("updtime")); string category1 = reader.GetString(reader.GetOrdinal("category1")); string category2 = reader.GetString(reader.GetOrdinal("category2")); string ed2k = reader.GetString(reader.GetOrdinal("ed2k")); string content = reader.GetString(reader.GetOrdinal("content")); string related = reader.GetString(reader.GetOrdinal("related")); //创建一个对象,把数据库每条数据给一个对象 VerycdItem item = new VerycdItem(); item.verycdid = verycdid; item.title = title; item.status = status; item.brief = brief; item.pubtime = pubtime; item.updtime = updtime; item.category1 = category1; item.category2 = category2; item.ed2k = ed2k; item.content = content; item.related = related; Console.WriteLine("当前读取到id=" + verycdid); //创建一个elasticSearch(数据库,表名,主键) IndexCommand indexCmd = new IndexCommand("verycd", "items", verycdid.ToString()); //Put()第二个参数是要插入的数据(把数据序列化存入elasticSearch) OperationResult result = client.Put(indexCmd, serializer.Serialize(item)); var indexResult = serializer.ToIndexResult(result.Result); if (indexResult.created) { Console.WriteLine("创建了"); } else { Console.WriteLine("没创建" + indexResult.error); } } } } } Console.ReadKey(); }