Lucene 3.4クエリー基本手順(ソースコードあり)


public class Searcher {

	 * main( )   
	public static void main(String[] args) throws IOException, ParseException {
		String indexDir = "F:/lucene/indexDir";
		String queryString = "patent AND freedom";
		
		search(indexDir, queryString);
	}
	
	public static void search( String indexDir, String queryString ) throws IOException, ParseException {
		Directory dir = FSDirectory.open( new File( indexDir ) );
		IndexSearcher indexSearcher = new IndexSearcher( dir );
		QueryParser queryParser = new QueryParser( Version.LUCENE_34, "contents", new StandardAnalyzer( Version.LUCENE_34 ) );
		Query query = queryParser.parse( queryString );
		
		long beginQueryTime = System.currentTimeMillis();
		TopDocs hits = indexSearcher.search( query, 10 );
		long endQueryTime = System.currentTimeMillis();
		
		System.err.println( "Found " + hits.totalHits + " document(s) in " + (endQueryTime - beginQueryTime) +
				" milliseconds that matched query '" + queryString + "':");
		
		for (ScoreDoc scoreDoc : hits.scoreDocs) {
			Document doc = indexSearcher.doc( scoreDoc.doc );
			System.out.println( doc.get("fullpath") );
		}
		
		indexSearcher.close();
	}
}