zoukankan      html  css  js  c++  java
  • 用dotLucene为数据库内容建立索引

    //建立索引的类        
    public class Indexer
             {
                 private IndexWriter writer;
                 Document doc = new Document();

                 public Indexer(string Directory)
                 {
                     InitializeIndex(Directory);
                 }

                 public void InitializeIndex(string Directory)
                 {
                     writer = new IndexWriter(Directory, new StandardAnalyzer(), true);
                     writer.SetUseCompoundFile(true);
                 }

                 public void AddDocument(string DateAdded , string Description , string URL , string Title )
                 {
                     doc.Add(Field.Keyword("date", DateAdded));
                     doc.Add(Field.Text("description", Description));
                     doc.Add(Field.Text("url", URL));            
                     doc.Add(Field.Text("title", Title));
                     doc.Add(Field.Keyword("sortdate", ReturnSortDate(DateTime.Parse(DateAdded)).ToString()));
                     writer.AddDocument(doc);
                 }
                             

                 private int ReturnSortDate(DateTime DateAdded)
                 {
                     string thisMonth = DateAdded.Month.ToString();

                     if (thisMonth.Length == 1)
                     {
                         thisMonth = "0" + thisMonth;
                     }

                     string thisYear   = DateAdded.Year.ToString();
                     string thisDay = DateAdded.Day.ToString();

                     if( thisDay.Length == 1 )
                     {
                         thisDay = "0" + thisDay;
                     }

                     int time = int.Parse(thisYear+thisMonth +thisDay);
                    

                     return time;
                 }


                 public void Close()
                 {
                     writer.Optimize();
                     writer.Close();
                 }
             }

    //搜索的类
    public class Searcher
             {
                 private IndexSearcher searcher;

                 public Searcher(string Directory)
                 {
                     searcher = new IndexSearcher(Directory);
                 }


                 public DataTable Search(string Query, string SortBy)
                 {
                     DataTable Results = new DataTable();
                     Results.Columns.Add("Title");
                     Results.Columns.Add("Description");
                     Results.Columns.Add("URL");
                     Results.Columns.Add("Published");
                
                     Query MyQuery   = QueryParser.Parse(Query, "description", new StandardAnalyzer());
                     Sort   sort = new Sort(SortBy, true);
                     Hits Hits = searcher.Search(MyQuery, sort);

                     int mTotalRecs = Hits.Length();
                     int iCount = 0;
                     while (iCount < mTotalRecs)
                     {
                         Document doc = Hits.Doc(iCount);
                         DataRow row = Results.NewRow();
                         row["url"] = doc.Get("url");
                         row["Title"] = doc.Get("title");
                         row["Description"] = doc.Get("description");
                         row["Published"] = doc.Get("date");
                         Results.Rows.Add(row);

                         iCount++;
                     }

                     searcher.Close();
                     return Results;
                 }
             }

  • 相关阅读:
    html 6 border border-width border-style border-color CSS三角形
    html 5 marign top right bottom left
    html 布局
    python学习——生成列表并修改其元素
    python学习——读取染色体长度(七:for循环对染色体序列进行反向互补)
    python学习——读取染色体长度(七:读取fasta文件)
    python学习——读取染色体长度(六:读取含有染色体长度的文件)
    python学习——读取染色体长度(五:从命令行输入染色体长度)
    python学习——读取染色体长度(四:获取最长染色体的编号)
    python学习——读取染色体长度(三、用循环或者函数求总长并获取最长染色体长度)
  • 原文地址:https://www.cnblogs.com/88223100/p/1270630.html
Copyright © 2011-2022 走看看