lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jerry Stern <yibing...@yahoo.com.cn>
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
Lucene.
  At the indexing stage, the contents of a original file is regarded as a FIELD of a Lucene
document:
   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)));
  
  idxWriter.addDocument(doc);
  
  System.out.println("indexed.");
 }

  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, "...");
   System.out.println(result);
  }

  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

		
---------------------------------
 雅虎免费G邮箱-中国第一绝无垃圾邮件骚扰超大邮箱
 雅虎助手¨D搜索、杀毒、防骚扰  
Mime
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message