lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jerry Stern <>
Subject Highlight the searched word when full-text searching performed
Date Sun, 27 Nov 2005 05:24:01 GMT
Hi all,
  I wonder how to highlight the searched word when full-text searching performed based on
  At the indexing stage, the contents of a original file is regarded as a FIELD of a Lucene
   private static void indexFile(File file, IndexWriter idxWriter)
 throws IOException {
  System.out.print("Indexing " + file.getCanonicalPath() + " ......");
  Document doc = new Document();
  doc.add(Field.Text("path", file.getAbsolutePath()));
  doc.add(Field.Text("contents", new FileReader(file)));

  At the searching stage:
    Highlighter highlighter = new Highlighter(new QueryScorer(query));
  for (int i = 0; i < hits.length(); i++)
   String text = hits.doc(i).get("contents"); // the text = null.

     TokenStream tokenStream = analyzer.tokenStream("path",
     new StringReader(text));
   // Get 3 best fragments and seperate with a "..."
   String result = highlighter.getBestFragments(tokenStream,
     text, 3, "...");

  The 'contents' field is not stored in index file, and it is not reasonable to store it in
index file. So the red line of code can not get the 'contents' field from the index file.
  I think that the 'text' parameter for the Highlighter.getBestFragments(..) method must be
the context string of the searched word. So my question is how can I get the context string
of the searched word?
  Thanks in advance!
  Stern Yi

  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message