Created
May 24, 2013 18:45
-
-
Save makuk66/5645661 to your computer and use it in GitHub Desktop.
Lucene Example
This file contains 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 org.apache.lucene.analysis.Analyzer; | |
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
import org.apache.lucene.document.*; | |
import org.apache.lucene.index.*; | |
import org.apache.lucene.queryparser.flexible.core.QueryNodeException; | |
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; | |
import org.apache.lucene.search.IndexSearcher; | |
import org.apache.lucene.search.Query; | |
import org.apache.lucene.search.ScoreDoc; | |
import org.apache.lucene.search.TopScoreDocCollector; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.FSDirectory; | |
import org.apache.lucene.util.Version; | |
import java.io.File; | |
import java.io.IOException; | |
public class LuceneExample { | |
private final String INDEX_PATH = "the_index"; | |
private static final Version luceneVersion = Version.LUCENE_43; | |
void run() throws IOException, QueryNodeException { | |
Analyzer analyzer = new StandardAnalyzer(luceneVersion); | |
Directory indexDir = FSDirectory.open(new File(INDEX_PATH)); | |
IndexWriterConfig luceneConfig = new IndexWriterConfig(luceneVersion, analyzer); | |
IndexWriter writer = new IndexWriter(indexDir, luceneConfig); | |
Document addDoc = new Document(); | |
addDoc.add(new StringField("id", "a1", Field.Store.YES)); | |
addDoc.add(new TextField("title", "Harry Potter", Field.Store.YES)); | |
addDoc.add(new TextField("author", "J. K. Rowling", Field.Store.YES)); | |
addDoc.add(new TextField("description", "Harry Potter is a series of seven fantasy novels", Field.Store.YES)); | |
Term term = new Term("id", "a1"); | |
writer.updateDocument(term, addDoc); | |
writer.commit(); | |
writer.close(); | |
indexDir.close(); | |
StandardQueryParser queryParser = new StandardQueryParser(); | |
Query query = queryParser.parse("potter", "title"); | |
indexDir = FSDirectory.open(new File(INDEX_PATH)); | |
IndexReader reader = DirectoryReader.open(indexDir); | |
System.out.println("Index numDocs: " + reader.numDocs()); | |
IndexSearcher searcher = new IndexSearcher(reader); | |
int numHits = 100; | |
TopScoreDocCollector collector = TopScoreDocCollector.create(numHits, true); | |
searcher.search(query, collector); | |
ScoreDoc[] docs = collector.topDocs().scoreDocs; | |
System.out.println("Found " + docs.length + " docs."); | |
for (ScoreDoc scoreDoc : docs) { | |
Document foundDoc = searcher.doc(scoreDoc.doc); | |
System.out.println(foundDoc.get("id") + "\t" + foundDoc.get("title")); | |
} | |
} | |
public static void main(String[] args) { | |
LuceneExample luceneExample = new LuceneExample(); | |
try { | |
luceneExample.run(); | |
} catch (Exception e) { | |
e.printStackTrace(System.err); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment