Esempio n. 1
0
        /// <summary>
        /// 创建索引
        /// </summary>
        public void InitIndex(object data)
        {
            var       docs = new List <SolrInputDocument>();
            DataTable list = data as DataTable;

            foreach (DataRow pro in list.Rows)
            {
                var model = new SolrInputDocument();

                PropertyInfo[] properites = typeof(IndexCustomerTagModel).GetProperties(); //得到实体类属性的集合
                //string[] dateFields = { "CreateDate" };
                string field = string.Empty;                                               //存储fieldname
                foreach (PropertyInfo propertyInfo in properites)                          //遍历数组
                {
                    object val = pro[propertyInfo.Name];
                    if (val != DBNull.Value)
                    {
                        model.Add(propertyInfo.Name, new SolrInputField(propertyInfo.Name, val));
                    }
                }
                docs.Add(model);
            }

            var result = updateOperations.Update("core1", "/update", new UpdateOptions()
            {
                Docs = docs, CommitOptions = commitOptions
            });
            var header = binaryResponseHeaderParser.Parse(result);

            System.Console.WriteLine(string.Format("Update Status:{0} QTime:{1}", header.Status, header.QTime));
            //System.Console.ReadLine();
        }
 /// <summary>
 /// 保存索引
 /// </summary>
 /// <typeparam name="T">对象实体</typeparam>
 /// <param name="collectionName">查询的集合(solr中的集体名称)</param>
 /// <param name="listObj">保存对的对象集</param>
 /// <param name="serializer">序列方式</param>
 /// <returns>保存结果</returns>
 public IndexOperationResponse SaveIndex <T>(string collectionName, List <T> listObj, ISolrObjectSerializer <T> serializer)
 {
     try
     {
         collectionName = string.IsNullOrEmpty(collectionName) ? searchConfig.Collection : collectionName;
         IList <SolrInputDocument> docs = (IList <SolrInputDocument>)serializer.Serialize(listObj);
         var result = updateOperations.Update(collectionName, "/update", new UpdateOptions()
         {
             OptimizeOptions = optimizeOptions, Docs = docs
         });
         var header = binaryResponseHeaderParser.Parse(result);
         return(new IndexOperationResponse
         {
             IsSuccess = header.Status == 0,
             Message = string.Empty,
             QTime = header.QTime,
             Status = header.Status
         });
     }
     catch (Exception ex)
     {
         return(new IndexOperationResponse
         {
             IsSuccess = false,
             Message = ex.Message,
             QTime = -1,
             Status = -1
         });
     }
 }