I have a problem with Lucene Highlighter. I found some code on Stackoverflow and on other, but this code does not work in my program. This is a method where I try search and higlight words, but when I search something, program gives me exception.
Method:
private static void useIndex(String query, String field, String option)
throws ParseException, CorruptIndexException, IOException, InvalidTokenOffsetsException {
// StandardAnalyzer analyzer = new StandardAnalyzer();
Query q = new QueryParser(field, analyzer).parse(query);
int hitsPerPage = 5;
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(q));
// display results
System.out.println("Found " + hits.length + " hits for " + query);
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
String docURL = d.get("url");
String docContent = d.get("content");
TokenStream tokenStream = TokenSources.getAnyTokenStream(reader, docId, "content", analyzer);
TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, docContent, false, 4);
String docFrag="";
if ((frag[0] != null) && (frag[0].getScore() > 0)) {
docFrag=frag[0].toString();
}
model.addRow(new Object[] { docURL, findSilimar(docId), docFrag });
}
reader.close();
}
Exception:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/lucene/index/memory/MemoryIndex
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.index.memory.MemoryIndex
I tried everything, but I don't know what is wrong.
P.S. Sorry for my English.
A NoClassDefFoundError
means that class isn't in your classpath, so you should figure out what jar you need to add to get it. MemoryIndex
is in: lucene-memory-x.x.x.jar
By the way, at a glance, it doesn't appear that this exception would be thrown in the code you've provided.