示例#1
0
        /// <summary>
        /// 索引数据
        /// </summary>
        private void CreateSearchIndex(HttpContext context)
        {
            //检查权限
            if (!new BLL.manager_role().Exists(adminInfo.role_id, "lucene_index", DTEnums.ActionEnum.Build.ToString()))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"您没有确认订单的权限!\"}");
                return;
            }
            bool   hasnew    = false;
            int    is_create = DTRequest.GetQueryInt("create");
            int    is_lock   = is_create;
            string strWhere  = "status=0";

            BLL.article               bll  = new BLL.article();
            BLL.channel               chl  = new BLL.channel();
            BLL.luceneconfig          xml  = new BLL.luceneconfig();
            List <Model.luceneconfig> list = xml.Load();

            foreach (Model.luceneconfig model in list)
            {
                if (!chl.Exists(model.name))
                {
                    continue;
                }
                if (is_create == 0)
                {
                    strWhere += " and id >" + model.id.ToString();
                }
                if (model.status == 1)
                {
                    DataTable dt     = bll.GetList(model.name, 1000, strWhere, "id asc").Tables[0];
                    int       _count = dt.Rows.Count;
                    if (_count > 0)
                    {
                        int count = 0;
                        hasnew = true;
                        LuceneHelper.CreateIndex(LuceneHelper.INDEX_DIR, is_lock);
                        LuceneHelper.MaxMergeFactor = 301;
                        LuceneHelper.MinMergeDocs   = 301;
                        string     _maxid = dt.Rows[_count - 1]["ID"].ToString();
                        SearchItem item   = null;
                        if (is_lock > 0)
                        {
                            is_lock = 0;
                        }
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            item             = new SearchItem();
                            item.id          = Utils.StrToInt(dt.Rows[j]["id"].ToString(), 0);
                            item.site_id     = Utils.StrToInt(dt.Rows[j]["site_id"].ToString(), 0);
                            item.channel_id  = Utils.StrToInt(dt.Rows[j]["channel_id"].ToString(), 0);
                            item.category_id = Utils.StrToInt(dt.Rows[j]["category_id"].ToString(), 0);
                            item.title       = dt.Rows[j]["title"].ToString();
                            //if (dt.Columns.Contains("sub_title"))
                            //{
                            //    item.sub_title = dt.Rows[j]["sub_title"].ToString();
                            //}
                            //else
                            //{
                            //    item.sub_title = "";
                            //}
                            item.call_index = dt.Rows[j]["call_index"].ToString();
                            item.tags       = dt.Rows[j]["tags"].ToString();
                            item.img_url    = dt.Rows[j]["img_url"].ToString();
                            //item.zhaiyao = dt.Rows[j]["zhaiyao"].ToString();
                            item.zhaiyao = Utils.DropHTML(dt.Rows[j]["content"].ToString());
                            //if (dt.Columns.Contains("market_price"))
                            //{
                            //    item.market_price = Utils.StrToDecimal(dt.Rows[j]["market_price"].ToString(), 0);
                            //}
                            //else
                            //{
                            //    item.market_price = 0;
                            //}
                            //if (dt.Columns.Contains("sell_price"))
                            //{
                            //    item.sell_price = Utils.StrToInt(dt.Rows[j]["sell_price"].ToString(), 0);
                            //}
                            //else
                            //{
                            //    item.sell_price = 0;
                            //}
                            //item.is_top = Utils.StrToInt(dt.Rows[j]["is_top"].ToString(), 0);
                            //item.is_red = Utils.StrToInt(dt.Rows[j]["is_red"].ToString(), 0);
                            //item.is_hot = Utils.StrToInt(dt.Rows[j]["is_hot"].ToString(), 0);
                            item.add_time    = DateTime.Parse(dt.Rows[j]["add_time"].ToString());
                            item.update_time = Utils.StrToDateTime(dt.Rows[j]["update_time"].ToString(), item.add_time);
                            LuceneHelper.IndexString(LuceneHelper.INDEX_DIR, item).ToString();
                            count++;
                            if (count % 300 == 0)
                            {
                                LuceneHelper.CloseWithoutOptimize();
                                LuceneHelper.CreateIndex(LuceneHelper.INDEX_DIR, 0);
                                LuceneHelper.MaxMergeFactor = 301;
                                LuceneHelper.MinMergeDocs   = 301;
                            }
                        }
                        xml.Update("Lucene/" + model.name + "/lastid", _maxid);
                        xml.Update("Lucene/" + model.name + "/lasttime", System.DateTime.Now.ToString());
                        xml.Save();
                        LuceneHelper.Close();
                    }
                }
            }
            if (hasnew)
            {
                JsonHelper.WriteJson(context, new
                {
                    status = 2,
                    msg    = "再去追加一次吧"
                });
            }
            else
            {
                JsonHelper.WriteJson(context, new
                {
                    status = 1,
                    msg    = "索引更新完成"
                });
            }
        }