You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by madan mp <ma...@gmail.com> on 2012/12/04 13:00:18 UTC

lucene highlighter

I am working with lucene 3.6 ,I am trying to use the highlighter 3.6 in my
search engine code, can you pls provide me full code for highlighting the
 terms....pls..pls


regards
    madan

Re: lucene highlighter

Posted by Nicola Buso <nb...@ebi.ac.uk>.
On Tue, 2012-12-04 at 17:30 +0530, madan mp wrote:
> I am working with lucene 3.6 ,I am trying to use the highlighter 3.6 in my
> search engine code, can you pls provide me full code for highlighting the
>  terms....pls..pls
> 
> 
> regards
>     madan

Hi,

not a complete example, but you can play around it...

final String preTag = "<strong>";
final String postTag = "</strong>";
final SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(preTag,
postTag);
final Analyzer a = <your analyzer>

final int fragSize = 10;
final int fragNum = 3;

QueryScorer scorer = null;
try {
	Query q = p.parse("query");
		scorer = new QueryScorer(q, fieldName);
} catch (final Exception e) {
	logger.debug("Error creating scorer (problem parsing query terms?)",
e);
	return orig; // ?? error
}
final Highlighter h = new Highlighter(formatter, scorer);

Fragmenter fragmenter = new SimpleFragmenter(fragSize);
h.setTextFragmenter(fragmenter);
String[] fragments;
try {
	final CachingTokenFilter tokenStream = new
CachingTokenFilter(a.tokenStream(fieldName, new StringReader(orig)));
	fragments = h.getBestFragments(tokenStream, orig, fragNum);
	if (fragments.length > 0) {
		StringBuilder sb = new StringBuilder();
		for(int idx = 0; idx < fragments.length; idx++) {
			String fragment = fragments[idx];
			sb.append(fragment);
		}
		return sb.toString();
	}
} catch (IOException e) {
	logger.error("Error obtaining best fragment", e);
} catch (InvalidTokenOffsetsException e) {
	logger.error("Error obtaining best fragment", e);
}



Nicola.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org