12.7. Searching for a Specific Term in a Document Index

Problem

You need to identify which documents in a Lucene index contain specific terms or phrases.

Solution

Use an IndexSearcher to search a Lucene index created with IndexWriter. This recipe assumes that you have created a Lucene index using the techniques shown in the previous recipe. The constructor of IndexSearcher takes the name of a directory that contains a Lucene index. A Query object can be created by passing a String query, a default search field, and an Analyzer to QueryParser.parse() . The following example searches the Lucene index created in the previous recipe for all speeches containing the term “Ophelia”:

import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.Searcher; logger.info("Searching for Ophelia"); Searcher searcher = new IndexSearcher("index"); Analyzer analyzer = new SimpleAnalyzer( ); Query query = QueryParser.parse("Ophelia", "speech", analyzer); Hits hits = searcher.search(query); logger.info( "Searching Done, hit: " + hits.length( ) ); System.out.println( "Score | Play | Act | Scene | Speaker" ); for( int i = 0; i < hits.length( ); i++ ) { Document doc = hits.doc(i); System.out.print( (int) (hits.score(i) * 100 ...

Get Jakarta Commons Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.