Elasticsearch .Net Client NEST 索引DataSet数据

  

交流

Elasticsearch.Net、NEST 交流群:523061899

NEST 索引DataSet数据,先序列化然后转成dynamic 类型进行索引;

/// <summary>
        /// 索引dataset
        /// </summary>
        /// <param name="dataSet"></param>
        /// <param name="taskId"></param>
        public void Insert(DataSet dataSet,int taskId)
        {
            StructureData(dataSet);
            foreach (DataTable table in dataSet.Tables)
            {
                string indexName = string.Format("mailtask{0}",taskId);
                string typeName = string.Format("{0}info",table.TableName);
                //序列化
                string js= JsonConvert.SerializeObject(table);
                //反序列化
                dynamic dc = JsonConvert.DeserializeObject<dynamic>(js);
                //索引
                IndexManyExtensions.IndexManyAsync(SimpleClient.Client, dc, indexName, typeName);

                //SimpleClient.Client.IndexMany(dc, indexName, typeName);
            }
        }
        /// <summary>
        /// 给datatable 添加列并赋值
        /// </summary>
        /// <param name="dataSet"></param>
        private void StructureData(DataSet dataSet)
        {
            DateTime sendTime = DateTime.Now;
            foreach (DataTable table in dataSet.Tables)
            {
                table.Columns.Add(new DataColumn("SendTime",typeof(DateTime)) {DefaultValue = sendTime});
            }
        }

 

posted @ 2016-10-14 11:26  Mr. Hu  阅读(1291)  评论(0编辑  收藏  举报
Map