I am using Lucene Highlighter 2.4.1 for my application. I use the highlighter to get the best matching fragments, and display them. I make a call to a function String[] getFragmentsWithHighlightedTerms(Analyzer analyzer, Query query, String fieldName, String fieldContents, int fragmentsNumber, int fragmentSize). For example :
String text = doc.get("MetaData");
getFragmentsWithHighlightedTerms(analyzer, query, "MetaData", Text, 5, 100);
The function getFragmentsWithHighlightedTerms() is defined as follows
private static String[] getFragmentsWithHighlightedTerms( argument list here)
{
TokenStream stream = TokenSources.getTokenStream(fieldName, fieldContents, analyzer);
SpanScorer scorer = new SpanScorer(query, fieldName, new CachingTokenFilter(stream));
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer, fragmentSize);
Highlighter highlighter = new Highlighter(scorer);
highlighter.setTextFragmenter(fragmenter);
highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE);
String[] fragments = highlighter.getBestFragments(stream, fieldContents, fragmentNumber);
return fragments;
}
Now my trouble is that the highlighter.getBestFragments() method is returning duplicates. i.e, If i display say the first 5 fragments, no. 1 and 3 are same. I do not quite understand what is causing this. Is there a problem with the code?
I dont have the code in front of me, but I think you are getting an array of arrays. So you would need to do this:
or just get 1 item out the fragments array.