Created
December 3, 2013 19:31
-
-
Save erikhatcher/7776013 to your computer and use it in GitHub Desktop.
Lucene Examples for The Rich Web Experience 2013
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.TokenStream; | |
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
import org.apache.lucene.codecs.simpletext.SimpleTextCodec; | |
import org.apache.lucene.document.Document; | |
import org.apache.lucene.document.Field; | |
import org.apache.lucene.document.TextField; | |
import org.apache.lucene.index.DirectoryReader; | |
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.Explanation; | |
import org.apache.lucene.search.IndexSearcher; | |
import org.apache.lucene.search.Query; | |
import org.apache.lucene.search.ScoreDoc; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.FSDirectory; | |
import org.apache.lucene.store.RAMDirectory; | |
import org.apache.lucene.util.Version; | |
import java.io.File; | |
import java.io.IOException; | |
public class LuceneExamples { | |
public static void main(String[] args) throws IOException, ParseException { | |
// Adapted from http://lucene.apache.org/core/4_6_0/core/overview-summary.html#overview_description | |
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); | |
Directory directory = new RAMDirectory(); // FSDirectory.open(new File("/path/to/index")); // use FSDirectory to see the SimpleText output | |
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); | |
config.setCodec(new SimpleTextCodec()); | |
config.setUseCompoundFile(false); | |
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); | |
IndexWriter writer = new IndexWriter(directory, config); | |
Document doc = new Document(); | |
String text = "The Rich Web Experience 2013: Lucene Examples"; | |
doc.add(new Field("title", text, TextField.TYPE_STORED)); | |
writer.addDocument(doc); | |
writer.close(); | |
// Now search the index: | |
DirectoryReader reader = DirectoryReader.open(directory); | |
IndexSearcher searcher = new IndexSearcher(reader); | |
// Parse a simple query that searches for "text": | |
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "default_field", analyzer); | |
Query query = parser.parse("title:(experience lucene)"); | |
System.out.println("query (" + query.getClass() + ") = " + query); | |
ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs; | |
System.out.println("hits = " + hits.length); | |
// Iterate through the results: | |
for (ScoreDoc hit : hits) { | |
Document hitDoc = searcher.doc(hit.doc); | |
System.out.println("hitDoc.get(\"title\") = " + hitDoc.get("title")); | |
Explanation explanation = searcher.explain(query, hit.doc); | |
System.out.println(explanation.toString()); | |
} | |
reader.close(); | |
directory.close(); | |
TokenStream ts = analyzer.tokenStream("field_name", "some text goes here"); | |
//CharTermAttribute termAttr = ts.addAttribute(CharTermAttribute.class); | |
try { | |
ts.reset(); | |
while (ts.incrementToken()) { | |
System.out.println("token: " + ts.reflectAsString(false)); | |
} | |
ts.end(); // Perform end-of-stream operations, e.g. set the final offset. | |
} finally { | |
ts.close(); // Release resources associated with this stream. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment