lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicola Buso <nb...@ebi.ac.uk>
Subject Re: lucene highlighter
Date Tue, 04 Dec 2012 13:07:19 GMT
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


Mime
View raw message