lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Otis Gospodnetic <otis_gospodne...@yahoo.com>
Subject Re: How to use Highlighter concretly ?
Date Wed, 23 Jun 2004 15:27:19 GMT
Hello Olivier,

You already have your Document instance from Hits.
Use Documents's get(String) method to get the content of the given
field.  Note that the Field MUST be stored, not just indexed.
For example

String text = doc.get("myStoredTextField");

See:
http://jakarta.apache.org/lucene/docs/api/org/apache/lucene/document/Document.html#get(java.lang.String)

I suggest you comment out all Highligher-related code from your
application, and first make sure you know how to, and really can get
the contents of fields whose content you later want to highlight.

Add the Highligher code only after you get this first step working.

Otis

--- Olivier Catteau <ocatteau_lucene@hotmail.com> wrote:
> Hi !
> 
> I'd like to use the Highlighter class to show a summury highlighted
> after a search. But I don't know how to use correctly the Highlighter
> class.
> I found this piece of code which works well.
> 
> 
>
--------------------------------------------------------------------------------
> 
> public class TestHighlighter {
> 
> public static void main(String[] args) {
> 
> try {
> 
> Analyzer a = new StandardAnalyzer();
> 
> Query q = QueryParser.parse("jennifer lopez", "cached", a);
> 
> String s =
> 
> "the unofficial home page Britney Spears Elizabeth Hurley Kirsten
> Dunst "
> 
> + "Anna Kournikova Katie Holmes Katherine Heigl Jessica Alba Alyson
> Hannigan Jennifer "
> 
> + "Lopez Sarah Michelle Gellar";
> 
> Highlighter highlighter = new Highlighter(new QueryScorer(q));
> 
> TokenStream tokenstream =
> 
> a.tokenStream("cached", new java.io.StringReader(s));
> 
> String summary = highlighter.getBestFragments(tokenstream, s, 2,
> "...");
> 
> 
> System.out.println("summary : " + summary);
> 
> } catch(Exception e) {
> 
> e.printStackTrace();
> 
> }
> 
> }
> 
> }
> 
> 
>
--------------------------------------------------------------------------------
> 
> 
> But I don't know how to adapt it. In fact, I've made a search and I
> get a Hits instance. And now, I want to give a highlighted summury of
> each documents of the hits. So it must looks like this :
> 
>
--------------------------------------------------------------------------------
> 
> Highlighter highlighter;
> 
> TokenStream tokenstream;
> 
> 
> for (int i = 0; i < hits.length(); i++) {
> 
> Document doc = hits.doc(i);
> 
> 
> String contents = I DON'T KNOW HOW TO GET THE CONTENTS OF MY DOC
> 
> 
> highlighter = new Highlighter(new QueryScorer(query));
> 
> tokenstream = analyzer.tokenStream("contents", new
> java.io.StringReader(contents));
> 
> String summary = highlighter.getBestFragments(tokenstream, contents,
> 2, "...");
> 
> System.out.println("summary : " + summary);
> 
> }
> 
> 
>
--------------------------------------------------------------------------------
> 
> 
> Here is my questions. First, is it the good method to get a
> highlighted summury ? And if it is, how is the best way to get the
> contents of my document (the same way that I used to index their
> contents or another way ?) ?
> 
> (To be more precise, I use Lucene to index PDF, DOC, TXT. The size of
> these document could be about 5Mo.)
> 
> Thanks.
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Mime
View raw message