lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Miller <>
Subject Highlighter that works with phrase and span queries
Date Tue, 19 Jun 2007 22:39:27 GMT
I have been working on extending the Highlighter with a new Scorer that 
correctly scores phrase and span queries. The highlighter is working 
great for me, but could really use some more banging on.

If you have a need or an interest in a more accurate Highlighter, please 
give it a whirl and let me know how it went. Unlike most of the other 
alternate Lucene Highlighters, this one builds off the original contrib 
Highlighter so as to retain all of its goodness.

Example Usage

    IndexSearcher searcher = new IndexSearcher(ramDir);
    Query query = QueryParser.parse("Kenne*", FIELD_NAME, analyzer);
    query = query.rewrite(reader); //required to expand search terms
    Hits hits =;

    for (int i = 0; i < hits.length(); i++)
        String text = hits.doc(i).get(FIELD_NAME);
        CachingTokenFilter tokenStream = new 
                        FIELD_NAME, new StringReader(text)));
        Highlighter highlighter = new Highlighter(new SpanScorer(query, 
FIELD_NAME, tokenStream));
        // Get 3 best fragments and seperate with a "..."
        String result = highlighter.getBestFragments(tokenStream, text, 
3, "...");

If you make a call to any of the getBestFragments() methods more than 
once, you must call reset() on the SpanScorer between each call.

Pass null as the FIELD_NAME to ignore fields.

If you want to Highlight the whole document, use a NullFragmenter.

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

View raw message