Lucene学习笔记--简单搜索

方法1:

Date starttime=new Date();
			String queryString = "o";
			String[] fields={"name","content"};
			QueryParser queryParser = new MultiFieldQueryParser(fields,analyzer);
			Query query=queryParser.parse(queryString);
			
			IndexSearcher indexSearcher=new IndexSearcher(INDEX_STORE_PATH);
			Filter filter=null;
			TopDocs topDocs=indexSearcher.search(query,filter,1000);
			Date endtime=new Date();
			System.out.println("共有"+(endtime.getTime()-starttime.getTime())+"ms");
			System.out.println("共有"+topDocs.totalHits+"个匹配结果");
			for(ScoreDoc scoreDoc : topDocs.scoreDocs){
				Document doc =indexSearcher.doc(scoreDoc.doc);
				System.out.println("content:"+doc.get("content"));
				System.out.println("name:"+doc.get("name"));
				System.out.println("path:"+doc.get("path"));
			}
运行结果:
共有143ms
共有14个匹配结果
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt
content:null
name:ModernApplication.txt
path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt

 方法2:

starttime=new Date();
			IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH);
			Term t = new Term("content", "o");
			Query q= new TermQuery(t);
			Hits h= searcher.search(q);
			endtime=new Date();
			long timeOfSearch = endtime.getTime()-starttime.getTime();
			System.out.println("共有"+timeOfSearch+"ms");
			System.out.println("共有"+h.length()+"个匹配结果");
			for(int i=0;i<h.length();i++){
				System.out.println(h.doc(i));
				System.out.println(h.doc(i).get("name"));
				System.out.println(h.score(i));
				System.out.println(h.id(i));
			}
运行结果:
共有8ms
共有14个匹配结果
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
0
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
1
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
2
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
3
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
4
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
5
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
6
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
7
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
8
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
9
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
10
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
11
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
12
Document<stored/uncompressed,indexed,tokenized<name:ModernApplication.txt> stored/uncompressed,indexed,tokenized<path:E:\java\SearchEngine\Lucene03\datasource\ModernApplication.txt>>
ModernApplication.txt
0.05143136
13

相关推荐