|
1: 搜索关键字
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using Lucene.Net.Analysis;
- using Lucene.Net.Documents;
- using Lucene.Net.Index;
- using Lucene.Net.QueryParsers;
- using Lucene.Net.Search;
- using Lucene.Net.Analysis.Cn;
- namespace SearchTest
- {
- class SearchTest
- {
- private IndexSearcher searcher = null;
- private Analyzer analyser = null;
- public SearchTest()
- {
- analyser = new ChineseAnalyzer();
- searcher = new IndexSearcher(IndexReader.Open("c:\\index"));
- }
- public Hits search(String queryString, string field)
- {
- if (searcher != null)
- {
- try
- {
- QueryParser parser = new QueryParser(field,analyser);
- parser.SetOperator(QueryParser.DEFAULT_OPERATOR_AND);
- Query query = parser.Parse(queryString);
- return searcher.Search(query);
- }
- catch { }
- }
- return null;
- }
- static void Main(string[] args)
- {
- SearchTest searcher = new SearchTest();
- DateTime start = DateTime.Now;
- Hits h = searcher.search("中国", "content");
- DateTime end = DateTime.Now;
- long time = end.Ticks - start.Ticks;
- Console.WriteLine("总共耗时:{0}毫秒",Convert.ToString(time));
- Console.WriteLine("总共找到{0}个文件",h.Length());
- }
- }
- }
复制代码
由于搜索是一种注重效率的行为,因此将其构建成一个轻量级的对象能节省不少内存和时间上的开销.在上面的
代码中,内置了一个IndexSearcher对象,并调用其search方法来检索.
在代码最后的main函数中,输入了关键字"中国",搜索的结果是
总共耗时:1406250毫秒
总共找到22个文件
请按任意键继续. . .
2:下面为了性能上的提高,我们来用多线程进行搜索
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Lucene.Net.Analysis.Cn;
- using Lucene.Net.Index;
- using Lucene.Net.QueryParsers;
- using Lucene.Net.Search;
- using System.Threading;
- namespace MuiltiSearch
- {
- class MultiSearch
- {
- static void Main(string[] args)
- {
- for (int i = 0; i < 50; i++)
- {
- SearchThread s = new SearchThread();
- Thread thread=new Thread(s.run);
- thread.Start();
- }
- }
- }
- public class SearchThread
- {
- public void run()
- {
- Thread.Sleep(10);
- IndexSearcher searcher = new IndexSearcher(IndexReader.Open("c:\\index"));
- QueryParser parser = new QueryParser("content", new ChineseAnalyzer());
- parser.SetOperator(QueryParser.DEFAULT_OPERATOR_AND);
- Query query = parser.Parse("中国");
- DateTime start = DateTime.Now;
- Hits h = searcher.Search(query);
- DateTime end = DateTime.Now;
- long time = start.Ticks - end.Ticks;
- Console.WriteLine("用时{0}毫秒", time);
- }
- }
- }
复制代码
|
|