?? form1.cs
字號:
this.statusBar1.Text = "準備";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem14});
//
// menuItem14
//
this.menuItem14.Index = 0;
this.menuItem14.Text = "清空輸出";
this.menuItem14.Click += new System.EventHandler(this.menuItem14_Click);
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(40, 80);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(112, 112);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox2.Location = new System.Drawing.Point(248, 96);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(144, 50);
this.pictureBox2.TabIndex = 2;
this.pictureBox2.TabStop = false;
//
// label2
//
this.label2.Location = new System.Drawing.Point(32, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(136, 23);
this.label2.TabIndex = 3;
this.label2.Text = "這是俺GF,漂亮吧:P";
//
// label3
//
this.label3.Location = new System.Drawing.Point(248, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(160, 23);
this.label3.TabIndex = 4;
this.label3.Text = "您瞅準了,Wawasoft商標!";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 315);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.tabControl1);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(512, 349);
this.Menu = this.mainMenu1;
this.MinimumSize = new System.Drawing.Size(512, 349);
this.Name = "Form1";
this.Text = "Lucene.Net實驗室";
this.Load += new System.EventHandler(this.Form1_Load);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
#endregion
#region 索引
delegate void AsyncIndexDirectoryCaller(IndexWriter writer, FileInfo file);
public void IndexDirectory(IndexWriter writer, FileInfo file)
{
if (Directory.Exists(file.FullName))
{
String[] files = Directory.GetFileSystemEntries(file.FullName);
// an IO error could occur
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
IndexDirectory(writer, new FileInfo(files[i])); //這里是一個遞歸
}
}
}
else if (file.Extension == ".txt"||file.Extension == ".htm"||file.Extension == ".html")
{
IndexFile(file, writer);
}
}
private void IndexFile(FileInfo file, IndexWriter writer)
{
try
{
Document doc = new Document();
output("正在建立索引"+file.FullName);
doc.Add(Field.Keyword("filename", file.FullName));
//這里一定要設置響應的編碼格式,否則建立索引的時候不能正確讀取內容并分詞
doc.Add(Field.Text("contents", new StreamReader(file.FullName,System.Text.Encoding.Default)));
writer.AddDocument(doc);
}
catch (FileNotFoundException fnfe)
{
output(fnfe.Message);
}
}
void searchCallback(IAsyncResult ar)
{
IndexWriter writer = (IndexWriter)ar.AsyncState;
writer.Optimize();
writer.Close();
TimeSpan s = DateTime.Now - start;
MessageBox.Show("索引完成,共用時"+s.Milliseconds+"毫秒","提示");
}
#endregion
#region 搜索
void printResult(Hits h)
{
if (h.Length() == 0)
{
output("對不起,沒有搜索到你要的結果。");
}
else
{
for (int i = 0; i < h.Length(); i++)
{
try
{
Document doc = h.Doc(i);
output("這是第"+i+"個搜索結果,文件名為"+doc.Get("filename"));
}
catch(Exception ex)
{
output(ex.Message);
}
}
}
output("---------------------------");
}
Hits Search(string key)
{
output("正在檢索關鍵字"+key);
try
{
Query query = QueryParser.Parse(key, "contents", new ChineseAnalyzer());
start = DateTime.Now;
Hits hits = searcher.Search(query);
TimeSpan s = DateTime.Now - start;
output("搜索到"+hits.Length()+"個結果,共用時:"+s.Milliseconds +"毫秒");
return hits;
}
catch(Exception ex)
{
output(ex.Message);
return null;
}
}
#endregion
#region 幫助方法
void output(string s)
{
richTextBox1.AppendText(s +"\n");
this.statusBar1.Text = s;
}
#endregion
#region 實驗測試代碼
public void Test1()
{
//建立一個內存目錄
Lucene.Net.Store.RAMDirectory ramDir = new Lucene.Net.Store.RAMDirectory();
//建立一個索引書寫器
IndexWriter ramWriter = new IndexWriter(ramDir,new ChineseAnalyzer(), true);
//要索引的詞,這就相當于一個個的要索引的文件
string[] words = {"中華人民共和國", "人民共和國", "人民","共和國"};
//循環數組,創建文檔,給文檔添加字段,并把文檔添加到索引書寫器里
Document doc = null;
for (int i = 0; i < words.Length; i++)
{
doc = new Document();
doc.Add(Field.Text("contents", words[i]));
ramWriter.AddDocument(doc);
}
//索引優化
ramWriter.Optimize();
//關閉索引讀寫器,一定要關哦,按理說應該把上面的代碼用try括主,在finally里關閉索引書寫器
ramWriter.Close();
//構建一個索引搜索器
IndexSearcher searcher = new IndexSearcher(ramDir);
//用QueryParser.Parse方法實例化一個查詢
Query query = QueryParser.Parse("中華人民","contents",new ChineseAnalyzer());
//獲取搜索結果
Hits hits = searcher.Search(query);
//判斷是否有搜索到的結果,當然你也可以遍歷結果集并輸出
if (hits.Length() != 0)
MessageBox.Show("有");
else
MessageBox.Show("沒有");
}
#endregion
#region 事件處理代碼
private void textBox1_Enter(object sender, System.EventArgs e)
{
this.textBox1.Text = "";
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
searcher = new IndexSearcher(INDEX_STORE_PATH);
Hits h = Search(textBox1.Text);
printResult(h);
statusBar1.Text = "準備";
}
catch(Exception ex)
{
output(ex.Message);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
// this.Test1();
}
private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if ( result == DialogResult.OK )
{
IndexWriter writer = null;
try
{
writer = new IndexWriter(INDEX_STORE_PATH, new ChineseAnalyzer(), true);
start = DateTime.Now;
#region 同步索引
// IndexDirectory(writer,new FileInfo(this.folderBrowserDialog1.SelectedPath));
//如果是同步索引的話,調用優化索引的函數可以對索引優化,如果是異步就不行了。
//異步的話,最好把writer也當做參數傳遞給回調函數里,在回調函數里優化,這里
//傳遞了一個時間參數進去,你可以傳遞一個包含start和writer的自定義對象進去。
//為了讓示例簡單我沒有這個做。
// writer.Optimize();
#endregion
#region 異步索引
AsyncIndexDirectoryCaller caller = new AsyncIndexDirectoryCaller(IndexDirectory);
IAsyncResult ar = caller.BeginInvoke(writer,new FileInfo(this.folderBrowserDialog1.SelectedPath),new AsyncCallback(searchCallback),writer);
#endregion
statusBar1.Text = "準備";
}
catch(Exception ex)
{
output(ex.Message);
}
}
}
private void menuItem14_Click(object sender, System.EventArgs e)
{
this.richTextBox1.Clear();
}
#endregion
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -