• 用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;
                 }
             }

  • 相关阅读:
    Linux(Debian、Ubuntu、Deepin等)安装最新版Chrome Unstable
    JavaScript根据经纬度获取距离信息
    CSS滚动条样式定制
    Linux下 Apache Vhost 配置 防止403
    Unity减小安装包的体积(210MB减小到7MB)
    Ubuntu17.04配置LNMP(Nginx+PHP7+MySQL)简单教程 快速 易学 简单易懂
    Yii2项目实现Markdown功能 在线Markdown编辑器
    Gradle全局代理配置
    Angular4.0从入门到实战打造在线竞拍网站学习笔记之三依赖注入
    Python使用PyMysql操作数据库
  • 原文地址:https://www.cnblogs.com/88223100/p/1270630.html
Copyright © 2020-2023  润新知