Lucene実戦:簡単な検索を開始します。

957 ワード

インデックス参照の作成:http://blog.csdn.net/ol_beta/articale/detail/6840619
        /**
	 * 
	 *   
	 * 
	 * @throws IOException
	 * @throws ParseException
	 */
	@Test
	public void search() throws IOException, ParseException{
		//     
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);
		//   
		Directory dir = FSDirectory.open(new File(indexPath));
		//IndexSearcher
		IndexSearcher searcher = new IndexSearcher(dir);
		//    
		QueryParser parser = new QueryParser(Version.LUCENE_34, "content", analyzer);
		Query query = parser.parse("WYSIWYG");
		//  
		TopDocs topDocs = searcher.search(query, 100);
		System.out.println("    :"+topDocs.totalHits+"   ");
		//    
		for(ScoreDoc doc : topDocs.scoreDocs){
			Document document = searcher.doc(doc.doc);
			System.out.println(document.get("filepath"));
		}
		searcher.close();
	}
インデックスを作成する分詞器は、クエリーの分詞器と同じであり、分詞が一致することが保証されます。