⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 lucene的一个简单的测试程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
			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 + -