/*****************************************************************/ /* Copyright 2013 Code Strategies */ /* This code may be freely used and distributed in any project. */ /* However, please do not remove this credit if you publish this */ /* code in paper or electronic form, such as on a web site. */ /*****************************************************************/ package avajava; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.Iterator; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hit; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; public class LucenePhraseQueryDemo { public static final String FILES_TO_INDEX_DIRECTORY = "filesToIndex"; public static final String INDEX_DIRECTORY = "indexDirectory"; public static final String FIELD_PATH = "path"; public static final String FIELD_CONTENTS = "contents"; public static void main(String[] args) throws Exception { createIndex(); searchIndexWithPhraseQuery("french", "fries", 0); searchIndexWithPhraseQuery("hamburger", "steak", 0); searchIndexWithPhraseQuery("hamburger", "steak", 1); searchIndexWithPhraseQuery("hamburger", "steak", 2); searchIndexWithPhraseQuery("hamburger", "steak", 3); searchIndexWithQueryParser("french fries"); // BooleanQuery searchIndexWithQueryParser("\"french fries\""); // PhaseQuery searchIndexWithQueryParser("\"hamburger steak\"~1"); // PhaseQuery searchIndexWithQueryParser("\"hamburger steak\"~2"); // PhaseQuery } public static void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException { Analyzer analyzer = new StandardAnalyzer(); boolean recreateIndexIfExists = true; IndexWriter indexWriter = new IndexWriter(INDEX_DIRECTORY, analyzer, recreateIndexIfExists); File dir = new File(FILES_TO_INDEX_DIRECTORY); File[] files = dir.listFiles(); for (File file : files) { Document document = new Document(); String path = file.getCanonicalPath(); document.add(new Field(FIELD_PATH, path, Field.Store.YES, Field.Index.UN_TOKENIZED)); Reader reader = new FileReader(file); document.add(new Field(FIELD_CONTENTS, reader)); indexWriter.addDocument(document); } indexWriter.optimize(); indexWriter.close(); } public static void searchIndexWithQueryParser(String searchString) throws IOException, ParseException { System.out.println("Searching for '" + searchString + "' using QueryParser"); Directory directory = FSDirectory.getDirectory(INDEX_DIRECTORY); IndexSearcher indexSearcher = new IndexSearcher(directory); QueryParser queryParser = new QueryParser(FIELD_CONTENTS, new StandardAnalyzer()); Query query = queryParser.parse(searchString); System.out.println("Type of query: " + query.getClass().getSimpleName()); displayQuery(query); Hits hits = indexSearcher.search(query); displayHits(hits); } public static void searchIndexWithPhraseQuery(String string1, String string2, int slop) throws IOException, ParseException { Directory directory = FSDirectory.getDirectory(INDEX_DIRECTORY); IndexSearcher indexSearcher = new IndexSearcher(directory); Term term1 = new Term(FIELD_CONTENTS, string1); Term term2 = new Term(FIELD_CONTENTS, string2); PhraseQuery phraseQuery = new PhraseQuery(); phraseQuery.add(term1); phraseQuery.add(term2); phraseQuery.setSlop(slop); displayQuery(phraseQuery); Hits hits = indexSearcher.search(phraseQuery); displayHits(hits); } public static void displayHits(Hits hits) throws CorruptIndexException, IOException { System.out.println("Number of hits: " + hits.length()); Iterator it = hits.iterator(); while (it.hasNext()) { Hit hit = it.next(); Document document = hit.getDocument(); String path = document.get(FIELD_PATH); System.out.println("Hit: " + path); } System.out.println(); } public static void displayQuery(Query query) { System.out.println("Query: " + query.toString()); } }