// We transform this summary into
// a) a summary matching the search terms (highlighting)
// b) and a shortend summary (200 characters)
// int docId = hitScoreDocs[index].doc;
Document document = getHitDocument(index);
byte[] compressedFieldValue = document.getBinaryValue("summary");
String text = null;
if (compressedFieldValue != null) {
text = CompressionTools.decompressString(compressedFieldValue);
}
if (text != null) {
// Overwrite the content with a shortend summary
String resSummary = RegainToolkit.createSummaryFromContent(text, 200);
document.removeField("summary");
if (resSummary != null) {
//System.out.println("resSummary " + resSummary);
document.add(new Field("summary", resSummary, Field.Store.NO, Field.Index.NOT_ANALYZED));
document.add(new Field("summary", CompressionTools.compressString(resSummary), Field.Store.YES));
}
String resHighlSummary = null;
// Remove 'html', this works the same way as PageResponse.printNoHTML()
text = RegainToolkit.replace(text, "<", "<");
text = RegainToolkit.replace(text, ">", ">");
TokenStream tokenStream = mAnalyzer.tokenStream("content",
new StringReader(text));
// Get 3 best fragments and seperate with a " ... "
resHighlSummary = highlighter.getBestFragments(tokenStream, text, 3, " ... ");
if (resHighlSummary != null) {
//System.out.println("Highlighted summary: " + resHighlSummary);
// write the result back to the document in a new field
document.add(new Field("highlightedSummary", resHighlSummary, Field.Store.NO, Field.Index.NOT_ANALYZED));
document.add(new Field("highlightedSummary", CompressionTools.compressString(resHighlSummary), Field.Store.YES));
}
}
// Highlight the title
text = document.get("title");
String resHighlTitle = null;
if (text != null) {
TokenStream tokenStream = mAnalyzer.tokenStream("content",
new StringReader(text));
// Get the best fragment
resHighlTitle = highlighter.getBestFragment(tokenStream, text);
}
if (resHighlTitle != null) {
// write the result back to the document in a new field
//System.out.println("Highlighted title: " + resHighlTitle);
document.add(new Field("highlightedTitle", resHighlTitle,
Field.Store.YES, Field.Index.NOT_ANALYZED));
}
// write back the transformed document
setHitDocument(index, document);