lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Karthik N S" <>
Subject Using Highlighter in web Demo
Date Tue, 29 Jun 2004 13:20:01 GMT

Hello Developer's

   I am NOT able to get the API for the same [1.3-final or 1.4rc4 ] for
import  details.
    QueryScorer scorer = new QueryScorer(query);

just was curious to Compile and execute the same...:)


-----Original Message-----
From: Erik Hatcher []
Sent: Tuesday, June 29, 2004 5:52 PM
To: Lucene Users List
Subject: Re: Using Highlighter in web Demo

On Jun 28, 2004, at 5:18 PM, Hetan Shah wrote:
> Is it possible to use highlighter successfully in the demos the web
> demo to be specific. Has any one tried out there? If so can they
> explain me how to go about it any code sample is really  very
> appreciated.

Straight from Lucene in Action:

public class HighlightIt {
   private static final String text =
       "Contrary to popular belief, Lorem Ipsum is" +
       " not simply random text. It has roots in a piece of" +
       " classical Latin literature from 45 BC, making it over" +
       " 2000 years old. Richard McClintock, a Latin professor" +
       " at Hampden-Sydney College in Virginia, looked up one" +
       " of the more obscure Latin words, consectetur, from" +
       " a Lorem Ipsum passage, and going through the cites" +
       " of the word in classical literature, discovered the" +
       " undoubtable source. Lorem Ipsum comes from sections" +
       " 1.10.32 and 1.10.33 of \"de Finibus Bonorum et" +
       " Malorum\" (The Extremes of Good and Evil) by Cicero," +
       " written in 45 BC. This book is a treatise on the" +
       " theory of ethics, very popular during the" +
       " Renaissance. The first line of Lorem Ipsum, \"Lorem" +
       " ipsum dolor sit amet..\", comes from a line in" +
       " section 1.10.32.";  // from

   public static void main(String[] args) throws IOException {
     String filename = args[0];

     if (filename == null) {
       System.err.println("Usage: HighlightIt <filename>");

//    TermQuery query = new TermQuery(new Term("f", "ipsum"));
     PhraseQuery query = new PhraseQuery();
     query.add(new Term("f", "lorem"));
     query.add(new Term("f", "ipsum"));
     QueryScorer scorer = new QueryScorer(query);
     SimpleHTMLFormatter formatter =
         new SimpleHTMLFormatter("<span class=\"highlight\">",
     Highlighter highlighter = new Highlighter(formatter, scorer);
     Fragmenter fragmenter = new SimpleFragmenter(50);

     TokenStream tokenStream = new StandardAnalyzer()
         .tokenStream("f", new StringReader(text));

     String result =
         highlighter.getBestFragments(tokenStream, text, 5, "...");

     FileWriter writer = new FileWriter(filename);
     writer.write("<style>\n" +
         ".highlight {\n" +
         " background: yellow;\n" +
         "}\n" +

I just added the PhraseQuery in there instead of the TermQuery that is
commented out.  Highlighter works well with phrases also (although
highlights each term individually, not the breadth of the phrase by
itself).  The above code runs like it says in the usage statement, give
it a filename to save an HTML file that shows the terms highlighted in


To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message