private void CreateIndex() { string sresult = ""; try { //读取数据库数据 SqlDataReader myred = ExecuteQuery(); //建立索引字段 //Lucene.Net.Analysis.Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT); StockFooAnalyzer analyzer = new StockFooAnalyzer(System.Configuration.ConfigurationManager.AppSettings["AnalyzerPath"].ToString()); FSDirectory dy = FSDirectory.Open(new DirectoryInfo(Server.MapPath("IndexDirectory"))); IndexWriter writer = new IndexWriter(dy, analyzer, true,IndexWriter.MaxFieldLength.LIMITED); while (myred.Read()) { AddDocument(writer, myred["title"].ToString(), myred["url"].ToString(), myred["site"].ToString(), myred["body"].ToString(), myred["publish_time"].ToString()); } myred.Close(); myred.Dispose(); writer.Optimize(); writer.Close(); sresult = "ok"; } catch(Exception ex) { sresult = ex.Message; } Response.Write(sresult); Response.Flush(); Response.End(); }
private Analyzer GetAnalyer(int type) { Analyzer analyzer; string path =System.Configuration.ConfigurationSettings.AppSettings["Data"].ToString(); switch (type) { case 0: analyzer=new StockFooAnalyzer(path); break; case 1: analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT); break; case 2: analyzer = new SimpleAnalyzer(); break; case 3: analyzer = new StopAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT); break; case 4: analyzer = new KeywordAnalyzer(); break; case 5: analyzer = new WhitespaceAnalyzer(); break; default: analyzer = new StockFooAnalyzer(path); break; } return analyzer; }
protected void btnaddindex_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(txttitle.Text)) { txttitle.Focus(); return; } if (string.IsNullOrEmpty(txturl.Text)) { txturl.Focus(); return; } if (string.IsNullOrEmpty(txtsite.Text)) { txtsite.Focus(); return; } if (string.IsNullOrEmpty(txtbody.Text)) { txtbody.Focus(); return; } if (string.IsNullOrEmpty(txtpublishtime.Text)) { txtpublishtime.Focus(); return; } if (string.IsNullOrEmpty(txtboost.Text)) { txtboost.Focus(); return; } try { int temp = Convert.ToInt32(txtboost.Text); } catch { txtboost.Focus(); return; } StockFooAnalyzer analyzer = new StockFooAnalyzer(System.Configuration.ConfigurationManager.AppSettings["AnalyzerPath"].ToString()); FSDirectory dy = FSDirectory.Open(new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["IndexDirectory"].ToString())); IndexWriter writer = new IndexWriter(dy, analyzer, false, IndexWriter.MaxFieldLength.LIMITED); AddDocument(writer, txttitle.Text, txturl.Text, txtsite.Text, txtbody.Text.Length > 200 ? txtbody.Text.Substring(0, 200) : txtbody.Text, txtpublishtime.Text, Convert.ToInt32(txtboost.Text)); writer.Optimize(); writer.Close(); Page.RegisterStartupScript("ok", "<script>alert('新建成功!');</script>"); } catch (Exception ex) { Page.RegisterStartupScript("error","<script>alert('新建失败!"+ex.Message+"');</script>"); } }
private string AnalyzerObject() { string testwords = txtAnalyzer.Text; StockFooAnalyzer sa = new StockFooAnalyzer(System.Configuration.ConfigurationManager.AppSettings["AnalyzerPath"].ToString()); Stopwatch timer = new Stopwatch(); timer.Start(); Lucene.Net.Analysis.TokenStream ts = sa.TokenStream(null, new StringReader(testwords)); timer.Stop(); Token token = ts.Next(); StringBuilder sb = new StringBuilder(); sb.Append("<font color=blue>分词效果如下:</font>(耗时:" + timer.ElapsedMilliseconds + "毫秒)<br/>"); while (token != null) { sb.Append(token.TermText() + " " + token.StartOffset() + " " + token.EndOffset() + "<br/>"); token = ts.Next(); } ts.Close(); return sb.ToString(); }
private int CreateArticleIndex(Article art) { int createcount=0; try { StockFooAnalyzer analyzer = new StockFooAnalyzer(System.Configuration.ConfigurationManager.AppSettings["AnalyzerPath"].ToString()); FSDirectory dy = FSDirectory.Open(new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["IndexDirectory"].ToString())); IndexWriter writer = new IndexWriter(dy, analyzer, false, IndexWriter.MaxFieldLength.LIMITED); AddDocument(writer, art.Title, art.Url, art.Site, art.Body.Length>300?art.Body.Substring(0,300):art.Body, art.PublishTime.ToString()); writer.Optimize(); writer.Close(); createcount = 1; } catch { createcount = 0; } return createcount; }
private void SelectWordResult(string searchStr, int pageIndex) { try { SearchWords = searchStr; DateTime dt1 = DateTime.Now; //Lucene.Net.Analysis.Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT); StockFooAnalyzer analyzer = new StockFooAnalyzer(System.Configuration.ConfigurationManager.AppSettings["AnalyzerPath"].ToString()); FSDirectory dy = FSDirectory.Open(new DirectoryInfo(Server.MapPath("manage/IndexDirectory"))); IndexSearcher searcher = new IndexSearcher(dy, false); //多个索引文件查询 //MultiReader reader = new MultiReader(new IndexReader[] { IndexReader.Open(@"c:\index"), IndexReader.Open(@"\\server\index") }); //IndexSearcher searcher = new IndexSearcher(reader); //Hits hits = searcher.Search(query); //或 //IndexSearcher[] searchers = new IndexSearcher[2]; //searchers[0] = new IndexSearcher(Server.MapPath("manage/IndexDirectory01")); //searchers[1] = new IndexSearcher(Server.MapPath("manage/IndexDirectory02")); //MultiSearcher multiSearcher = new MultiSearcher(searchers); //可以使用 ParallelMultiSearcher 进行多线程并行搜索。 MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, new string[] { "title","body"}, analyzer); Query query = parser.Parse(searchStr); //单个条件排序 //Sort sort = new Sort(); //SortField f = new SortField("publish_time", SortField.STRING, true);//按照publish_time字段排序,true表示降序 //sort.SetSort(f); //多个条件排序 //Sort sort = new Sort(); //SortField f1 = new SortField("title", SortField.SCORE, false); //SortField f2 = new SortField("publish_time", SortField.STRING, true); //sort.SetSort(new SortField[] { f1, f2 }); Hits hits = searcher.Search(query); //TopDocs docs = searcher.Search(query, null, 10000); //进行多条件搜索 //Query query1 = new TermQuery(new Term(FieldValue, "name1")); // 词语搜索 //Query query2 = new WildcardQuery(new Term(FieldName, "name*")); // 通配符 //Query query3 = new PrefixQuery(new Term(FieldName, "name1")); // 字段搜索 Field:Keyword,自动在结尾添加 * //Query query4 = new RangeQuery(new Term(FieldNumber, NumberTools.LongToString(11L)), new Term(FieldNumber, NumberTools.LongToString(13L)), true); // 范围搜索 //Filter filter = new DateFilter(FieldDate, DateTime.Parse("2005-10-1"), DateTime.Parse("2005-10-30")); //Query query5 = new FilteredQuery(query, filter); // 带过滤条件的搜索 //BooleanQuery query = new BooleanQuery(); //query.Add(query1, BooleanClause.Occur.MUST); //query.Add(query2, BooleanClause.Occur.MUST); //IndexSearcher searcher= new IndexSearcher(reader); //Hits hits = searcher.Search(query); int totalCount = 0; if (hits != null && hits.Length() > 0) { ArrayList arr01 = new ArrayList(); ArrayList arr02 = new ArrayList(); for (int i = 0; i < hits.Length(); i++) { Document doc = hits.Doc(i); string url = doc.Get("url").ToString(); string title = doc.Get("title").ToString(); string body = doc.Get("body").ToString(); string publishtime = doc.Get("publish_time").ToString(); string site = doc.Get("site").ToString(); if (searchStr.IndexOf(" ") > -1) { bool isexisit = false; string[] word = searchStr.Split(' '); for (int j = 0; j < word.Length; j++) { if (word[j].Trim().Equals("")) { continue; } if (title.IndexOf(word[j].Trim()) > -1) { isexisit = true; break; } } if (isexisit) { arr01.Add(doc); } else { arr02.Add(doc); } } else { if (title.IndexOf(searchStr) > -1) { arr01.Add(doc); } else { arr02.Add(doc); } } } arr01.Sort(new DateTimeCompare()); arr02.Sort(new DateTimeCompare()); ArrayList arrAll = new ArrayList(); foreach (Document doo in arr01) { arrAll.Add(doo); } foreach (Document doo in arr02) { arrAll.Add(doo); } StringBuilder sb = new StringBuilder(); int begincount = 0; if (pageIndex > 1) begincount = (pageIndex - 1) * 10; int endcount = pageIndex * 10; //if (endcount > hits.Length() || endcount < 10) // endcount = hits.Length(); if (endcount > arrAll.Count || endcount < 10) endcount = arrAll.Count; for (int i = begincount; i < endcount; i++) { //ScoreDoc doc = docs.scoreDocs[i]; //string url = searcher.Doc(doc.doc).Get("url").ToString(); //Document doc = hits.Doc(i); Document doc = (Document)arrAll[i]; string url = doc.Get("url").ToString(); string title = doc.Get("title").ToString(); string body = doc.Get("body").ToString(); body = body.Length > 100 ? body.Substring(0, 100) + "..." : body; if (searchStr.IndexOf(" ") > -1) { title = GetRedWord(title, searchStr); body = GetRedWord(body, searchStr); } else { title = title.Replace(searchStr, "<font color=red>" + searchStr + "</font>"); body = body.Replace(searchStr, "<font color=red>" + searchStr + "</font>"); } string publishtime = doc.Get("publish_time").ToString(); string site = doc.Get("site").ToString(); sb.Append(" <div style=\"margin-top: 20px; margin-bottom: 20px;width:666px;\">"); sb.Append(" <div>"); sb.Append(" <a target=\"_blank\" href=\"" + url + "\"><font color=blue>" +title+ "</font></a>");// <font color=#666699>" + doc.score.ToString("0.00")+"</font> sb.Append(" </div>"); sb.Append(" <div style=\"font-size: 13px;\">"); sb.Append(" <font size=2 >" + body + "</font>"); sb.Append(" </div>"); sb.Append(" <div>"); string datetime = ""; try { datetime = Convert.ToDateTime(publishtime).ToString("yyyy-MM-dd HH:mm"); } catch { datetime = publishtime; } sb.Append(" <font color=#006600>" + (url.Length > 66 ? url.Substring(0, 66) : url) + "</font> <font size=2 color=#006600>" + datetime + "</font> <font size=2 color=#666699>[" + site + "]</font>"); sb.Append(" </div>"); sb.Append(" </div>"); } labResultList.Text = sb.ToString(); //totalCount = hits.Length(); totalCount = arrAll.Count; TotalCount = totalCount.ToString(); int totalPage = totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1; if (totalPage > 1) { StringBuilder sbp = new StringBuilder(); string parame = ""; if (pageIndex > 1) { parame = "search1.aspx?pi=" + (pageIndex - 1).ToString() + "&w=" + System.Web.HttpUtility.UrlEncode(searchStr); sbp.Append("<a href=\"" + parame + "\">上一页</a> "); } int beginindex = 1; int endindex = totalPage; if (totalPage > 20 && pageIndex > 11 && totalPage - pageIndex > 8) { beginindex = pageIndex - 10; } if (totalPage > 20 && pageIndex > 11 && totalPage - pageIndex < 9) { beginindex = pageIndex - (20 - (totalPage - pageIndex) - 1); } if (endindex > 10) { endindex = pageIndex + 9; if (endindex > totalPage) { endindex = totalPage; } } for (int i = beginindex; i <= endindex; i++) { if ((i) == pageIndex) { sbp.Append(" " + pageIndex); } else { parame = "search1.aspx?pi=" + (i).ToString() + "&w=" + System.Web.HttpUtility.UrlEncode(searchStr); sbp.Append("<a href=\"" + parame + "\">[" + (i).ToString() + "]</a>"); } } if (pageIndex < totalPage) { parame = "search1.aspx?pi=" + (pageIndex + 1).ToString() + "&w=" + System.Web.HttpUtility.UrlEncode(searchStr); sbp.Append(" <a href=\"" + parame + "\">下一页</a>"); } PageInfo = sbp.ToString(); } DateTime dt2 = DateTime.Now; TimeSpan ts = dt2 - dt1; double timeuse = Convert.ToInt32(ts.TotalMilliseconds) * 0.001; SearchTime = timeuse.ToString("0.001"); searcher.Close(); } else { SearchWords = searchStr; TotalCount = ""; SearchTime = ""; PageInfo = ""; labResultList.Text = "<br/>抱歉,未找到:“" + SearchWords + "”相关内容。<br/><br/><b>建议您:</b> <br/>1、看看输入的文字是否有误<br/>2、去掉可能不必要的字词,如“的”、“什么”等<br/>3、<a href=\"http://www.haopingba.com/Page1Result.aspx?web=aall&searchword=" + searchStr + "\" target=\"_blank\">去此处查询</a>"; } } catch (Exception ex) { SearchWords = searchStr; TotalCount =""; SearchTime =""; PageInfo=""; labResultList.Text = "<br/>检索异常:" + ex.Message + "<br/><br/><b>建议您:</b> <br/>1、看看输入的文字是否有误<br/>2、去掉可能不必要的字词,如“的”、“什么”等<br/>3、<a href=\"http://www.haopingba.com/Page1Result.aspx?web=aall&searchword=" + searchStr + "\" target=\"_blank\">去此处查询</a>"; } }
private void btnDelete_Click(object sender, EventArgs e) { try { if (MessageBox.Show("确认删除该文档记录吗?", "提示:", MessageBoxButtons.OKCancel) == DialogResult.OK) { string path = System.Configuration.ConfigurationSettings.AppSettings["Data"].ToString(); Analyzer analyzer = new StockFooAnalyzer(path); FSDirectory dy = FSDirectory.Open(new DirectoryInfo(System.Configuration.ConfigurationSettings.AppSettings["IndexDirectory"].ToString())); IndexWriter indexWriter = new IndexWriter(dy, analyzer, false); IOpen open = CurrentIndex.GetCurrentOpendIndex(); Document olddoc = open.Reader.Document(Convert.ToInt32(textBox1.Text)); indexWriter.DeleteDocuments(new Term("url", olddoc.Get("url"))); indexWriter.Optimize(); indexWriter.Close(); FindDocuemnt(0); label9.Text = "当前为文档1"; textBox1.Text = "0"; docNum = docNum - 1; label1.Text = string.Format("共有文档{0}条", docNum); MessageBox.Show("删除成功!"); } } catch (Exception ex) { MessageBox.Show("删除失败!\\n" + ex.Message); } }
private void btnUpdateDoc_Click(object sender, EventArgs e) { try { string path = System.Configuration.ConfigurationSettings.AppSettings["Data"].ToString(); Analyzer analyzer = new StockFooAnalyzer(path); FSDirectory dy = FSDirectory.Open(new DirectoryInfo(System.Configuration.ConfigurationSettings.AppSettings["IndexDirectory"].ToString())); IndexWriter indexWriter = new IndexWriter(dy, analyzer, false); IOpen oldopen = CurrentIndex.GetCurrentOpendIndex(); Document olddoc = oldopen.Reader.Document(Convert.ToInt32(textBox1.Text)); string oldurl = olddoc.Get("url").ToString(); bool oldisnew = false; if (oldurl.Trim().Equals(txturl.Text.Trim())) oldisnew = true; Document document = new Document(); Field ftitle = new Field("title", txttitle.Text, Field.Store.YES, Field.Index.ANALYZED);//存储,索引 document.Add(ftitle); Field furl = new Field("url", txturl.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引 document.Add(furl); Field fsite = new Field("site", txtsite.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引 document.Add(fsite); Field fbody = new Field("body", txtbody.Text, Field.Store.YES, Field.Index.ANALYZED);//存储,索引 document.Add(fbody); Field fpublishtime = new Field("publish_time", txtpublishtime.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引 document.Add(fpublishtime); Term term = new Term("url", txturl.Text); indexWriter.UpdateDocument(term, document, analyzer); indexWriter.Optimize(); indexWriter.Close(); IndexOpen open = new IndexOpen(System.Configuration.ConfigurationSettings.AppSettings["IndexDirectory"].ToString(), false); bool isOpend = open.Open(); if (isOpend) { CurrentIndex.SetCurrentOpendIndex(open); } if (!oldisnew) { FindDocuemnt(0); label9.Text = "当前为文档1"; textBox1.Text = "0"; docNum = docNum + 1; label1.Text = string.Format("共有文档{0}条", docNum); } else { docNum = open.Reader.MaxDoc(); FindDocuemnt(docNum - 1); label9.Text = "当前为文档" + (docNum).ToString(); textBox1.Text = (docNum - 1).ToString(); } MessageBox.Show("更新成功!"); } catch (Exception ex) { MessageBox.Show("更新失败!\\n"+ex.Message); } }