Esempio n. 1
0
 public async Task <AnsInfo> SearchAns(SearchInfo Searchwords)
 {
     try
     {
         AnsInfo ai     = new AnsInfo();
         string  search = Searchwords.SearchString + "&" + Searchwords.page + "&" + Searchwords.filter + "&" + Searchwords.filetype;
         if (Cache.CacheServices.cache.Exists(search))
         {
             var cat = Cache.CacheServices.cache.Get(search);
             ai = (AnsInfo)cat;
             DeactivateOnIdle();
             return(await Task.FromResult(ai));
         }
         else
         {
             Cache.CacheServices cs = new Cache.CacheServices();
             ai = cs.SearchforCache(Searchwords);
             DeactivateOnIdle();
             return(await Task.FromResult(ai));
         }
         throw new NotImplementedException();
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Data);
         throw;
     }
 }
Esempio n. 2
0
        public async Task <AnsInfo> SearchAns(SearchInfo si, Guid guid)
        {
            var     searchgrain = GrainClient.GrainFactory.GetGrain <ISearchGrain>(guid);
            AnsInfo ai          = searchgrain.SearchAns(si).Result;

            return(await Task.FromResult(ai));
        }
        public AnsInfo SearchforCache(SearchInfo Searchwords)
        {
            AnsInfo ai = new AnsInfo();

            Xapian.MSet xm = new Xapian.MSet();
            XapianLogic xl = new XapianLogic();

            if (Searchwords.filter == 5)    //sec, not cache
            {
                List <SearchResult> ls = new List <SearchResult>();
                uint num = 0;
                xl.SearchforsecReturn(Searchwords.SearchString, Searchwords.page, Searchwords.filter, Searchwords.filetype, out num, out ls);
            }
            else
            {
                xl.SearchReturnforSilo(Searchwords.SearchString, Searchwords.filter, Searchwords.filetype, out xm);
                SearchReturn(xm, Searchwords, out ai);
                string search = Searchwords.SearchString + "&" + Searchwords.page + "&" + Searchwords.filter + "&" + Searchwords.filetype;
                cache.Put(search, ai);
            }
            return(ai);
        }
        private void SearchReturn(Xapian.MSet xm, SearchInfo Searchwords, out AnsInfo ai)
        {
            ai = new AnsInfo();
            List <SearchResult> XapResList = new List <SearchResult>();
            string query = Searchwords.SearchString;

            query = query.Replace("\\", "");
            query = query.Replace("/", "");
            int    page      = Searchwords.page;
            var    segmenter = new JiebaSegmenter();
            var    segments  = segmenter.Cut(query);
            string querystr  = string.Join(" ", segments);   //分词

            //若返回不为空
            if (xm != null)
            {
                ai.totalnum = xm.Size();    //结果数目
                int pagecount = 0;
                for (Xapian.MSetIterator iter = xm.Begin(); iter != xm.End(); ++iter)
                {
                    SearchResult sr = new SearchResult();
                    ++pagecount;
                    if (pagecount <= ((page - 1) * 10))     //获得分页
                    {
                        continue;
                    }
                    else
                    {
                        if (XapResList.Count >= 10)         //每页10个结果
                        {
                            break;
                        }

                        Xapian.Document iterdoc    = iter.GetDocument();
                        bool            ftpflag    = false;               //ftp标记,转码用
                        bool            emflag     = false;
                        string          strcontent = iterdoc.GetData();   //取出正文
                        string          strtitle   = iterdoc.GetValue(3); //取出标题 ValueTitle
                        string          strahref   = iterdoc.GetValue(1); //取出链接
                        string          source     = iterdoc.GetValue(0);
                        string          strcut     = "";
                        int             contentlen = strcontent.Length;  //判断正文长度,为下面筛选含有关键词片段做准备
                        uint            docid      = iter.GetDocId();

                        if (source == "4")
                        {
                            sr.allcontent = strcontent;
                        }
                        if (source == "2")
                        {
                            ftpflag  = true;
                            strahref = UrlEncode(strahref);             //若为ftp链接,需要转码
                        }
                        string[]      strquerycut = querystr.Split(' ');
                        string        emlink      = "";
                        List <string> tmp         = new List <string>();
                        foreach (var item in strquerycut)
                        {
                            if (item == "e" || item == "E" || item == "m" || item == "M" ||
                                item == "em" || item == "Em" || item == "Em" || item == "EM" ||
                                item == "<" || item == ">")
                            {
                                emflag = true;
                                if (emlink != "")
                                {
                                    emlink = emlink + "|" + item;
                                }
                                else
                                {
                                    emlink = item;
                                }
                            }
                            else
                            {
                                tmp.Add(item);
                            }
                        }
                        HashSet <string> hs        = new HashSet <string>(tmp); //此时已经去掉重复的数据保存在hashset中
                        String[]         strunique = new String[hs.Count];
                        hs.CopyTo(strunique);

                        int cutlen = strunique.Length;
                        int count  = 0;

                        if (emlink != "" && cutlen == 0)
                        {
                            foreach (var item in strquerycut)
                            {
                                //消掉*问号空格
                                if (item == " " || item == "")
                                {
                                    continue;
                                }
                                CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
                                int         conpos  = Compare.IndexOf(strcontent, item, CompareOptions.IgnoreCase); //根据位置标红
                                                                                                                    //int conpos = strcontent.IndexOf(item);      //根据位置标红
                                if (conpos != -1)
                                {
                                    if (contentlen - conpos > 150 && conpos > 50)
                                    {
                                        //截取150字作为cache
                                        strcut = strcontent.Substring(conpos - 50, 200);
                                        if (emflag)
                                        {
                                            strtitle = ReplaceCntent(emlink, strtitle);
                                            strcut   = ReplaceCntent(emlink, strcut);
                                        }
                                        strcut = "..." + strcut + "...";
                                        goto Finally;
                                    }
                                    else if (conpos > 50)
                                    {
                                        ////截取150字作为cache
                                        strcut = strcontent.Substring(conpos - 50, contentlen - conpos + 50);
                                        if (emflag)
                                        {
                                            strtitle = ReplaceCntent(emlink, strtitle);
                                            strcut   = ReplaceCntent(emlink, strcut);
                                        }

                                        strcut = "..." + strcut + "...";
                                        goto Finally;
                                    }
                                    else if (contentlen - conpos > 150)
                                    {
                                        //截取150字作为cache
                                        strcut = strcontent.Substring(0, conpos + 150);
                                        if (emflag)
                                        {
                                            strtitle = ReplaceCntent(emlink, strtitle);
                                            strcut   = ReplaceCntent(emlink, strcut);
                                        }

                                        strcut = "..." + strcut + "...";
                                        goto Finally;
                                    }
                                    else
                                    {
                                        //strcut = HttpUtility.HtmlEncode(strcut);
                                        //不够150的全拿出
                                        strcut = strcontent;
                                        if (emflag)
                                        {
                                            strtitle = ReplaceCntent(emlink, strtitle);
                                            strcut   = ReplaceCntent(emlink, strcut);
                                        }

                                        strcut = "..." + strcut + "...";
                                        goto Finally;
                                    }
                                }
                                else
                                {
                                    CompareInfo Comparetitle = CultureInfo.InvariantCulture.CompareInfo;
                                    int         conpostitle  = Comparetitle.IndexOf(strtitle, item, CompareOptions.IgnoreCase); //根据位置标红
                                    if (conpostitle != -1)
                                    {
                                        if (contentlen > 200)
                                        {
                                            strcut = strcontent.Substring(0, 200);
                                            if (emflag)
                                            {
                                                strtitle = ReplaceCntent(emlink, strtitle);
                                                strcut   = ReplaceCntent(emlink, strcut);
                                            }

                                            strcut = "..." + strcut + "...";
                                            goto Finally;
                                        }
                                        else
                                        {
                                            strcut = strcontent;
                                            if (emflag)
                                            {
                                                strtitle = ReplaceCntent(emlink, strtitle);
                                                strcut   = ReplaceCntent(emlink, strcut);
                                            }

                                            strcut = "..." + strcut + "...";
                                            goto Finally;
                                        }
                                    }
                                    else
                                    {
                                        ++count;
                                    }
                                }
                            }
                        }
                        else
                        {
                            //每一个词都查一遍
                            foreach (var item in strunique)
                            {
                                //消掉*问号空格
                                if (item == " " || item == "")
                                {
                                    continue;
                                }
                                CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
                                int         conpos  = Compare.IndexOf(strcontent, item, CompareOptions.IgnoreCase); //根据位置标红
                                                                                                                    //int conpos = strcontent.IndexOf(item);      //根据位置标红
                                if (conpos != -1)
                                {
                                    if (contentlen - conpos > 150 && conpos > 50)
                                    {
                                        //截取150字作为cache
                                        strcut = strcontent.Substring(conpos - 50, 200);
                                        if (emflag)
                                        {
                                            strtitle = ReplaceCntent(emlink, strtitle);
                                            strcut   = ReplaceCntent(emlink, strcut);
                                        }
                                        //strcut = HttpUtility.HtmlEncode(strcut);
                                        for (; count < cutlen; count++)
                                        {
                                            if (strunique[count] == " " || strunique[count] == "")
                                            {
                                                continue;
                                            }
                                            //标红,大小写不敏感,regex替换法,replace大小写敏感
                                            strtitle = Regex.Replace(strtitle, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                            //strtitle = strtitle.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                            //strcut = strcut.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                            strcut = Regex.Replace(strcut, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                        }
                                        strcut = "..." + strcut + "...";
                                        goto Finally;
                                    }
                                    else if (conpos > 50)
                                    {
                                        ////截取150字作为cache
                                        strcut = strcontent.Substring(conpos - 50, contentlen - conpos + 50);
                                        if (emflag)
                                        {
                                            strtitle = ReplaceCntent(emlink, strtitle);
                                            strcut   = ReplaceCntent(emlink, strcut);
                                        }
                                        //strcut = HttpUtility.HtmlEncode(strcut);
                                        for (; count < cutlen; count++)
                                        {
                                            if (strunique[count] == " " || strunique[count] == "")
                                            {
                                                continue;
                                            }
                                            //标红
                                            strtitle = Regex.Replace(strtitle, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                            //strtitle = strtitle.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                            //strcut = strcut.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                            strcut = Regex.Replace(strcut, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                        }
                                        strcut = "..." + strcut + "...";
                                        goto Finally;
                                    }
                                    else if (contentlen - conpos > 150)
                                    {
                                        //截取150字作为cache
                                        strcut = strcontent.Substring(0, conpos + 150);
                                        if (emflag)
                                        {
                                            strtitle = ReplaceCntent(emlink, strtitle);
                                            strcut   = ReplaceCntent(emlink, strcut);
                                        }
                                        //strcut = HttpUtility.HtmlEncode(strcut);
                                        for (; count < cutlen; count++)
                                        {
                                            if (strunique[count] == " " || strunique[count] == "")
                                            {
                                                continue;
                                            }
                                            //标红
                                            strtitle = Regex.Replace(strtitle, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                            //strtitle = strtitle.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                            //strcut = strcut.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                            strcut = Regex.Replace(strcut, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                        }
                                        strcut = "..." + strcut + "...";
                                        goto Finally;
                                    }
                                    else
                                    {
                                        //strcut = HttpUtility.HtmlEncode(strcut);
                                        //不够150的全拿出
                                        strcut = strcontent;
                                        if (emflag)
                                        {
                                            strtitle = ReplaceCntent(emlink, strtitle);
                                            strcut   = ReplaceCntent(emlink, strcut);
                                        }
                                        for (; count < cutlen; count++)
                                        {
                                            if (strunique[count] == " " || strunique[count] == "")
                                            {
                                                continue;
                                            }
                                            //标红
                                            strtitle = Regex.Replace(strtitle, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                            //strtitle = strtitle.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                            //strcut = strcut.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                            strcut = Regex.Replace(strcut, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                        }
                                        strcut = "..." + strcut + "...";
                                        goto Finally;
                                    }
                                }
                                else
                                {
                                    CompareInfo Comparetitle = CultureInfo.InvariantCulture.CompareInfo;
                                    int         conpostitle  = Comparetitle.IndexOf(strtitle, item, CompareOptions.IgnoreCase); //根据位置标红
                                    if (conpostitle != -1)
                                    {
                                        if (contentlen > 200)
                                        {
                                            strcut = strcontent.Substring(0, 200);
                                            if (emflag)
                                            {
                                                strtitle = ReplaceCntent(emlink, strtitle);
                                                strcut   = ReplaceCntent(emlink, strcut);
                                            }
                                            //strcut = HttpUtility.HtmlEncode(strcut);
                                            for (; count < cutlen; count++)
                                            {
                                                if (strunique[count] == " " || strunique[count] == "")
                                                {
                                                    continue;
                                                }
                                                //标红
                                                strtitle = Regex.Replace(strtitle, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                                //strtitle = strtitle.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                                //strcut = strcut.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                                strcut = Regex.Replace(strcut, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                            }
                                            strcut = "..." + strcut + "...";
                                            goto Finally;
                                        }
                                        else
                                        {
                                            strcut = strcontent;
                                            if (emflag)
                                            {
                                                strtitle = ReplaceCntent(emlink, strtitle);
                                                strcut   = ReplaceCntent(emlink, strcut);
                                            }
                                            //strcut = HttpUtility.HtmlEncode(strcut);
                                            for (; count < cutlen; count++)
                                            {
                                                if (strunique[count] == " " || strunique[count] == "")
                                                {
                                                    continue;
                                                }
                                                //标红
                                                strtitle = Regex.Replace(strtitle, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                                //strtitle = strtitle.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                                //strcut = strcut.Replace(strquerycut[count], "<font color = red>" + strquerycut[count] + "</font>");
                                                strcut = Regex.Replace(strcut, Regex.Escape(strunique[count]), "<em>" + strunique[count] + "</em>", RegexOptions.IgnoreCase);
                                            }
                                            strcut = "..." + strcut + "...";
                                            goto Finally;
                                        }
                                    }
                                    else
                                    {
                                        ++count;
                                    }
                                }
                            }
                        }


                        //找到合适的内容之后返回结果
Finally:
                        sr.ahref = iterdoc.GetValue(1);
                        if (ftpflag)                   //判断是否需要转码链接
                        {
                            sr.ahrefencode = strahref; //ftp则使用转码链接
                        }
                        else
                        {
                            sr.ahrefencode = sr.ahref;
                        }
                        sr.link    = iterdoc.GetValue(2);
                        sr.title   = strtitle;
                        sr.snippet = strcut;
                        XapResList.Add(sr);
                    }
                }
                ai.retinfo = XapResList;
            }
            else
            {
                ai.totalnum = 0;
                ai.retinfo  = null;
            }
        }
Esempio n. 5
0
        public async Task <ActionResult> Result(string search, string filter, string page)
        {
            if (search == "我想玩旅行商问题的游戏上上下下左右左右BABA")
            {
                return(View("Amazing"));
            }
            TimeSpan            ts;
            DateTime            DateTimestart = DateTime.Now;
            DateTime            DateTimeend;
            List <SearchResult> XapAns;
            bool   urlflag      = false;
            string searchbackup = search;

            //角色等级
            switch (filter)
            {
            case null:
                if (User.IsInRole("Admin") || User.IsInRole("Sec"))
                {
                    filter = "0";           //管理员和等级用户检索就是全部可见的
                }
                else
                {
                    filter = "0";           //非管理员检索结果不可见等级
                }
                break;

            case "0":
                if (User.IsInRole("Admin") || User.IsInRole("Sec"))
                {
                    filter = "0";           //管理员和等级用户检索就是全部可见的
                }
                break;

            case "1":
                break;

            case "2":
                break;

            case "3":
                break;

            case "4":
            case "5":
                if (!User.IsInRole("Admin") && !User.IsInRole("Sec"))
                {
                    log.Warn("用户" + User.Identity.Name + "恶意访问!!");
                    filter = "0";
                }
                break;

            default:
                filter = "0";
                break;
            }
            //分页
            if (page == null)
            {
                page = "0";
            }
            ViewBag.Page   = page;
            ViewBag.Filter = filter;

            if (string.IsNullOrEmpty(search))
            {
                //如果没有词检索就返回
                return(RedirectToAction("Index"));
            }
            else
            {
                if (IsUrl(search))
                {
                    urlflag = true;
                    search  = Regex.Replace(search, @"/", " ");
                }

                //分词处理
                ViewBag.SearchWord = searchbackup;
                XapianLogic xl  = new XapianLogic();
                uint        num = 0;
                if (urlflag)
                {
                    var        guid        = GetGuid();
                    var        searchgrain = GrainClient.GrainFactory.GetGrain <ISearchGrain>(guid);
                    SearchInfo si          = new SearchInfo
                    {
                        SearchString = searchbackup,
                        filter       = int.Parse(filter),
                        page         = int.Parse(page),
                        filetype     = "1980/01/01"
                    };
                    Console.WriteLine(guid + "\n");
                    AnsInfo ai = searchgrain.SearchAns(si).Result;
                    Console.WriteLine(guid + "ret" + "\n");
                    //xl.SearchReturn(searchbackup, int.Parse(page), int.Parse(filter),"1980/01/01", out num, out XapAns, out ts);
                    num         = ai.totalnum;
                    XapAns      = ai.retinfo;
                    DateTimeend = DateTime.Now;
                    ts          = DateTimeend - DateTimestart;
                    ts.TotalMilliseconds.ToString();        //查询时间返回
                }
                else
                {
                    //如果是Ftp和共享文件夹的来源,支持用+fileextension:excel、word、ppt、pdf、txt、html,查找过滤
                    //if ((filter == "2" || filter == "3")&&search.Contains("+fileextension"))
                    var guid = GetGuid();
                    Console.WriteLine(guid + "\n");
                    //var searchgrain = GrainClient.GrainFactory.GetGrain<ISearchGrain>(guid);
                    Console.WriteLine(guid + "ret" + "\n");
                    SearchInfo si = new SearchInfo
                    {
                        SearchString = searchbackup,
                        filter       = int.Parse(filter),
                        page         = int.Parse(page)
                    };

                    if (search.Contains("+fileextension"))
                    {
                        int    pos           = search.IndexOf("+fileextension"); //若有使用扩展名检索,则筛选扩展名
                        string extension     = search.Substring(pos + 15, search.Length - pos - 15);
                        string searchkeyword = search.Substring(0, pos);
                        si.filetype = extension;
                        //xl.SearchReturn(searchkeyword, int.Parse(page), int.Parse(filter), extension, out num, out XapAns, out ts); //带有扩展名检索
                        AnsInfo ai = new AnsInfo();
                        ai = await SearchAns(si, guid);

                        num         = ai.totalnum;
                        XapAns      = ai.retinfo;
                        DateTimeend = DateTime.Now;
                        ts          = DateTimeend - DateTimestart;
                        ts.TotalMilliseconds.ToString();        //查询时间返回
                    }
                    else
                    {
                        si.filetype = "0";
                        AnsInfo ai = new AnsInfo();
                        ai = await SearchAns(si, guid);

                        //xl.SearchReturn(search, int.Parse(page), int.Parse(filter), "0", out num, out XapAns, out ts);  //无扩展名检索
                        num         = ai.totalnum;
                        XapAns      = ai.retinfo;
                        DateTimeend = DateTime.Now;
                        ts          = DateTimeend - DateTimestart;
                        ts.TotalMilliseconds.ToString();        //查询时间返回
                    }
                }


                if (num == 0)
                {
                    //如果没有检索到结果
                    ViewBag.ZeroCheck = "0";
                    TempData["Zero"]  = "内容未检索到!";
                    ViewBag.Ansnum    = 0;
                    ViewBag.PageCount = 0;
                    ViewBag.time      = ts;
                    return(View());
                }
                else
                {
                    ViewBag.AnsNum = num;
                }
                //检索到则返回结果
                ViewBag.WebContent = XapAns;
                ViewBag.PageCount  = (uint)Math.Ceiling(num / 10.0);
                //检索所用时间
                ViewBag.time = ts;
                return(View());
            }
        }