Lucene.NET开发——信息检索与索引记录删除

信息检索类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

using Lucene.Net.Index;

using Lucene.Net.Search;

using Lucene.Net.Documents;

using Lucene.Net.Store;

using Lucene.Net.Analysis.Standard;

 

namespace NewBeeServer

{

   static class Searcher

   {

      static Hits hits;

     

      //开始检索,返回检索到的结果数

      public static int SearchKeyWords(string encryptedKeyWord, string searchType)

      {

        //索引器建立

        IndexSearcher searcher = new IndexSearcher(Application.StartupPath + "\\users\\" + UserName.userName + "\\index", false);

        PhraseQuery totalQuery = new PhraseQuery();

        char[] seperator = { ' ' };

        string[] keywords = encryptedKeyWord.Split(seperator);

        foreach (string keyword in keywords)

        {

           totalQuery.Add(new Term(searchType, keyword));

        }

        totalQuery.SetSlop(5);

 

        //获得搜索结果对象

        hits = searcher.Search(totalQuery);

 

        //获取查询到的结果数

        return hits.Length();

      }

      //开始手机语句检索

      public static string PGetResultByIndex(int i)

      {

        StringBuilder sb = new StringBuilder();

        try

        {

           Document doc = hits.Doc(i);

           //获得filename

           Field field = doc.GetField("filename");

           string filename = field.StringValue();

           sb.Append("NAME#" + filename + "#\n");

 

           //获得filedate

           field = doc.GetField("filedate");

           string filedate = field.StringValue();

           sb.Append("DATE#" + filedate + "#\n");

 

           /*//获得filetag

           field = doc.GetField("filesize");

           string filesize = field.StringValue();

           sb.Append("SIZE#" + filesize + "#");*/

        }

        catch (Exception ee)

        {

           MessageBox.Show(ee.Message);

        }

        return sb.ToString();

      }

      //开始语句检索(catch置空)

      public static string GetResultByIndex(int i)

      {

        StringBuilder sb = new StringBuilder();

        try

        {

           Document doc = hits.Doc(i);

           //获得filename

           Field field = doc.GetField("filename");

           string filename = field.StringValue();

           sb.Append("NAME#" + filename + "#");

 

           //获得digest

           field = doc.GetField("digest");

           string digest = field.StringValue();

           sb.Append("DIGEST#" + digest + "#");

 

           //获得filedate

           field = doc.GetField("filedate");

           string filedate = field.StringValue();

           sb.Append("DATE#" + filedate + "#");

 

           //获得filesize

           field = doc.GetField("filesize");

           string filesize = field.StringValue();

           sb.Append("SIZE#" + filesize + "#");

 

           //获得filetag

           field = doc.GetField("filetag");

           string filetag = field.StringValue();

           sb.Append("TAG#" + filetag + "#");

        }

        catch (Exception ee)

        {

           //MessageBox.Show(ee.Message);

        }

        return sb.ToString();

      }

 

      //删除索引(catch置空)

      public static bool DeleteIndexByFilename(string fileName)

      {

        string noExtFilename = fileName.Substring(0, fileName.Length - 4);

        try

        {

           string path = Application.StartupPath + "\\users\\" + UserName.userName + "\\index";

           Directory directory = FSDirectory.GetDirectory(path, false);

           IndexReader reader = IndexReader.Open(directory);

           Term term = new Term("filename", noExtFilename);

           int r = reader.DeleteDocuments(term);

           reader.Close();

           directory.Close();

           IndexWriter indexWriter = new IndexWriter(path, new StandardAnalyzer(), false);

           indexWriter.Optimize();

           indexWriter.Close();

           return true;

        }

        catch (Exception ee)

        {

           //MessageBox.Show(ee.Message);

        }

        return false;

      }

   }

}

posted @ 2011-08-29 11:56  Erebus_NET  阅读(252)  评论(0编辑  收藏  举报