Advertisement
Guest User

Searcher

a guest
Apr 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  7. import org.apache.lucene.document.Document;
  8. import org.apache.lucene.index.CorruptIndexException;
  9. import org.apache.lucene.queryParser.ParseException;
  10. import org.apache.lucene.queryParser.QueryParser;
  11. import org.apache.lucene.search.IndexSearcher;
  12. import org.apache.lucene.search.Query;
  13. import org.apache.lucene.search.ScoreDoc;
  14. import org.apache.lucene.search.TopDocs;
  15. import org.apache.lucene.store.Directory;
  16. import org.apache.lucene.store.FSDirectory;
  17. import org.apache.lucene.util.Version;
  18.  
  19. public class Searcher {
  20.  
  21.     IndexSearcher indexSearcher;
  22.     QueryParser queryParser;
  23.     Query query;
  24.    
  25.     public Searcher(String indexDirectoryPath) throws IOException{
  26.         Directory indexDirectory = FSDirectory.open(new File(indexDirectoryPath));
  27.         indexSearcher = new IndexSearcher(indexDirectory);
  28.         queryParser = new QueryParser(Version.LUCENE_36, LuceneConstants.CONTENTS, new StandardAnalyzer(Version.LUCENE_36));
  29.     }
  30.    
  31.     public TopDocs search(String searchQuery) throws IOException, ParseException {
  32.         query = queryParser.parse(searchQuery);
  33.         return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
  34.     }
  35.    
  36.     public TopDocs search(Query query) throws IOException, ParseException {
  37.         return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
  38.     }
  39.    
  40.     public Document getDocument(ScoreDoc scoreDoc) throws CorruptIndexException, IOException{
  41.         return indexSearcher.doc(scoreDoc.doc);
  42.     }
  43.    
  44.     public void close() throws IOException{
  45.         indexSearcher.close();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement