Created
June 21, 2013 14:54
-
-
Save gtke/5831738 to your computer and use it in GitHub Desktop.
Simple Searcher class for Lucene
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.io.IOException; | |
| import java.util.Scanner; | |
| import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
| import org.apache.lucene.document.Document; | |
| import org.apache.lucene.index.DirectoryReader; | |
| import org.apache.lucene.index.IndexReader; | |
| import org.apache.lucene.index.IndexWriter; | |
| import org.apache.lucene.index.IndexWriterConfig; | |
| import org.apache.lucene.queryparser.classic.ParseException; | |
| import org.apache.lucene.queryparser.classic.QueryParser; | |
| import org.apache.lucene.search.IndexSearcher; | |
| import org.apache.lucene.search.Query; | |
| import org.apache.lucene.search.ScoreDoc; | |
| import org.apache.lucene.search.TopDocs; | |
| import org.apache.lucene.store.Directory; | |
| import org.apache.lucene.store.FSDirectory; | |
| import org.apache.lucene.util.Version; | |
| public class Searcher { | |
| private static String indexDir = "pathToIndexedFilesDir"; | |
| public static void main(String [] args) throws IOException, ParseException{ | |
| Scanner in = new Scanner(System.in); | |
| System.out.println("Input your query: "); | |
| String s = in.nextLine(); | |
| search(indexDir,s); | |
| } | |
| public static void search(String indexDir, String s) throws IOException, ParseException{ | |
| StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_43); | |
| System.out.println("Trying to search: "); | |
| Directory dir = FSDirectory.open(new File(indexDir)); | |
| System.out.println(dir); | |
| IndexReader reader = DirectoryReader.open(dir); | |
| IndexSearcher searcher = new IndexSearcher(reader); | |
| Query q = new QueryParser(Version.LUCENE_43, "pageTitle", analyzer).parse(s); | |
| Query qr = new QueryParser(Version.LUCENE_43,"articleTitle", analyzer).parse(s); | |
| Query qrr = new QueryParser(Version.LUCENE_43, "content", analyzer).parse(s); | |
| TopDocs hitsInPageTitles = searcher.search(q, 10); | |
| TopDocs hitsInArticleTitles = searcher.search(q, 10); | |
| TopDocs hitsInContent = searcher.search(q, 10); | |
| System.out.println("# of matches found in page titles: " + hitsInPageTitles.totalHits); | |
| System.out.println("# of matches found in article titles: " + hitsInArticleTitles.totalHits); | |
| System.out.println("# of matches found in content: " + hitsInContent.totalHits); | |
| int total = hitsInPageTitles.totalHits + hitsInArticleTitles.totalHits + hitsInContent.totalHits; | |
| for(ScoreDoc scoreDoc : hitsInPageTitles.scoreDocs){ | |
| Document doc = searcher.doc(scoreDoc.doc); | |
| System.out.println("Page Title in which the result was found: " + doc.get("pageTitle")); | |
| } | |
| for(ScoreDoc scoreDoc : hitsInArticleTitles.scoreDocs){ | |
| Document doc = searcher.doc(scoreDoc.doc); | |
| System.out.println("Page Title in which the result was found: " + doc.get("pageTitle") + " >>> " + doc.get("articleTitle")); | |
| } | |
| for(ScoreDoc scoreDoc : hitsInContent.scoreDocs){ | |
| Document doc = searcher.doc(scoreDoc.doc); | |
| System.out.println("Page Title in which the result was found: " + doc.get("pageTitle") + " >>> " + doc.get("articleTitle")); | |
| } | |
| reader.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment