/// <summary> /// 用于推送的查询 /// </summary> /// <param name="query">检索语句</param> /// <param name="filter">等级不同与否</param> /// <param name="addtime">添加时间</param> /// <returns>检索结果</returns> public string SearchForPush(string query, int filter, string addtime) { string str = ""; Xapian.MSet xm = searchforpush(query, filter, addtime); if (!Directory.Exists(localdb)) { Directory.CreateDirectory(localdb); } if (xm != null) { if (xm.Size() == 0) { return(null); } else { str = "订阅词" + query + "有" + xm.Size().ToString() + "条更新!\n"; int i = 0; for (Xapian.MSetIterator iter = xm.Begin(); iter != xm.End(); ++iter) { //构造检索结果 Xapian.Document iterdoc = iter.GetDocument(); str = str + (++i).ToString() + " 、标题:" + iterdoc.GetValue(3) + "\n 链接:" + iterdoc.GetValue(1) + "\n"; } } } else { return(null); } return(str); }
/// <summary> /// 在数据库中删除一篇文档 /// </summary> /// <param name="dbname">数据库名</param> /// <param name="hashcodelist">文章路径hashcode列表</param> /// <returns>是否成功,成功为1,失败为0</returns> public int delDocument(string dbname, List <string> hashcodelist) { string DBName = dbname; try { Xapian.WritableDatabase database; database = new Xapian.WritableDatabase(DBName, Xapian.Xapian.DB_CREATE_OR_OPEN); foreach (var item in hashcodelist) { Xapian.Enquire enquire = new Xapian.Enquire(database); //设置检索的前缀 Xapian.QueryParser qp = new Xapian.QueryParser(); qp.SetDatabase(database); qp.SetDefaultOp(Xapian.Query.op.OP_ELITE_SET); qp.SetStemmingStrategy(Xapian.QueryParser.stem_strategy.STEM_NONE); //检索hash值 string querystr = item; qp.AddPrefix("", "Q"); //hash前缀为Q Xapian.Query query = qp.ParseQuery(querystr); Console.WriteLine("query is" + query.GetDescription() + "\n"); //开始检索 enquire.SetQuery(query); //返回结果 Xapian.MSet XapAns = enquire.GetMSet(0, int.MaxValue); var a = XapAns.Size(); for (Xapian.MSetIterator iter = XapAns.Begin(); iter != XapAns.End(); ++iter) { Xapian.Document iterdoc = iter.GetDocument(); if (iterdoc.GetValue(VALUE_HASHCODE) != item) //防止hash检查出错 { continue; } else { uint docid = iter.GetDocId(); //获取唯一id database.DeleteDocument(docid); //删除文档 } } } database.Commit(); //提交数据库 database.Close(); //关闭数据库 } catch (Exception e) { log.Error(e.Message); Console.Error.WriteLine("Exception: " + e.ToString()); return(0); } return(1); }
/// <summary> /// 更新一篇文章列表 /// </summary> /// <param name="dbname">数据库路径</param> /// <param name="list">文章列表</param> /// <returns>是否成功,成功为1,失败为0</returns> public int updateDocument(string dbname, List <xapIndex> list) { ChineseSeg cs = new ChineseSeg(); string DBName = dbname; try { Xapian.WritableDatabase database; database = new Xapian.WritableDatabase(DBName, Xapian.Xapian.DB_CREATE_OR_OPEN); foreach (var item in list) { Xapian.Enquire enquire = new Xapian.Enquire(database); //设置检索的前缀 Xapian.QueryParser qp = new Xapian.QueryParser(); qp.SetDatabase(database); qp.SetDefaultOp(Xapian.Query.op.OP_ELITE_SET); qp.SetStemmingStrategy(Xapian.QueryParser.stem_strategy.STEM_NONE); //通过hash查找文章 string querystr = item.hashcode; qp.AddPrefix("", "Q"); //hash前缀为Q Xapian.Query query = qp.ParseQuery(querystr); Console.WriteLine("query is" + query.GetDescription() + "\n"); //开始检索 enquire.SetQuery(query); //返回结果 Xapian.MSet XapAns = enquire.GetMSet(0, int.MaxValue); for (Xapian.MSetIterator iter = XapAns.Begin(); iter != XapAns.End(); ++iter) { Xapian.Document iterdoc = iter.GetDocument(); if (iterdoc.GetValue(VALUE_HASHCODE) != item.hashcode) //以防出现hash筛选错误 { continue; } else { uint docid = iter.GetDocId(); //获取唯一id Xapian.Document doc = new Xapian.Document(); Xapian.TermGenerator indexer = new Xapian.TermGenerator(); doc.SetData(HttpUtility.HtmlEncode(item.content)); //设置负载域 DateTime DateTimestart = DateTime.Now; doc.AddValue(VALUE_TIME, DateTimestart.ToString("yyyy/MM/dd")); //插入时间 doc.AddValue(VALUE_AHREF, item.ahref); //原文链接 doc.AddValue(VALUE_LOCALINK, item.link); //本地链接 doc.AddValue(VALUE_TITLE, item.title); //文章标题 doc.AddValue(VALUE_SOURCE, item.source.ToString()); //来源类型 doc.AddValue(VALUE_SECLEVEL, item.seclevel.ToString()); //等级 doc.AddValue(VALUE_EXTENSION, item.extension.ToString()); //扩展名 doc.AddValue(VALUE_HASHCODE, item.hashcode); //hash indexer.SetDocument(doc); indexer.SetStemmingStrategy(Xapian.TermGenerator.stem_strategy.STEM_NONE); //设置不解析策略 string strcut = cs.JiebaSeg(item.content); string titlecut = cs.JiebaSeg(item.title); indexer.IndexText(strcut, 1, "C"); //设置内容前缀 indexer.IndexText(titlecut, 1, "T"); //设置标题前缀 indexer.IndexText(item.hashcode, 1, "Q"); //设置文档名hash indexer.IndexText(item.ahref, 1, "A"); //设置链接前缀(用于推送文件夹订阅) database.ReplaceDocument(docid, doc); //替换文档 } } } database.Commit(); //提交数据库 database.Close(); //关闭数据库 } catch (Exception e) { log.Error(e.Message); Console.Error.WriteLine("Exception: " + e.ToString()); return(0); } return(1); }
public override bool Apply(Xapian.Document doc) { return(doc.GetValue(0) == "yes"); }
/// <summary> /// 检索结果 /// </summary> /// <param name="query">检索词</param> /// <param name="page">分页号</param> /// <param name="filter">来源/等级不同过滤</param> /// <param name="filetype">类型过滤</param> /// <param name="num">返回结果数目</param> /// <param name="XapResList">返回检索结果</param> /// <param name="ts">返回检索时间</param> public void SearchReturn(string query, int page, int filter, string filetype, out uint num, out List <SearchResult> XapResList, out TimeSpan ts) { DateTime DateTimestart = DateTime.Now; DateTime DateTimeend; Xapian.MSet xm; XapResList = new List <SearchResult>(); { query = query.Replace("\\", ""); query = query.Replace("/", ""); } string querystr = _cs.JiebaSegnotSearch(query); //分词 if (!Directory.Exists(localdb)) { Directory.CreateDirectory(localdb); } if (filetype == "1980/01/01") { xm = searchforpush(query, filter, filetype); //检索 } else { xm = search(querystr, filter, filetype); //检索 } //若返回不为空 if (xm != null) { num = 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); } } } else { num = 0; } DateTimeend = DateTime.Now; ts = DateTimeend - DateTimestart; ts.TotalMilliseconds.ToString(); //查询时间返回 }
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; } }