lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markharw00d <markharw...@yahoo.co.uk>
Subject Re: Please help with Gradient Formatter
Date Wed, 30 Apr 2008 22:21:13 GMT
Here you go:

        Analyzer a=new StandardAnalyzer();
        //open an index
        String textFieldName="contents";
        IndexReader reader=IndexReader.open("E:/indexes/uksites");
        IndexSearcher searcher=new IndexSearcher(reader);       
        QueryParser qp=new QueryParser(textFieldName,a);
        //create a query (note one word is rarer than the other)
        Query q=qp.parse("football club");
               
        //Create a query scorer (passing a reader so that it can weight 
terms based on doc frequency)
        QueryScorer fragScorer = new QueryScorer(q,reader,"contents");
        //callibrate the scoring range based on best (ie rarest) search term
        float topScore=fragScorer.getMaxTermWeight();
        //Create a gradient formatter with appropriate color ranges
        GradientFormatter formatter=new 
GradientFormatter(topScore,"#888888","#ff8888","#ffffff",
        "#ffffff");
        //create the highlighter
        Highlighter highlighter=new Highlighter(formatter,fragScorer);
        //run the query and highlight the results - graded by color to 
reflect value of match
        Hits hits = searcher.search(q);
        int numToShow=Math.min(10,hits.length());
        for (int i = 0; i < numToShow; i++)
        {
            String text=hits.doc(i).get(textFieldName);
             TokenStream tokenStream = a.tokenStream(textFieldName, new 
StringReader(text));
             String highlightText = 
highlighter.getBestFragments(tokenStream, text, 2, "...");
             System.out.println("["+i+"="+hits.score(i)+"]"+highlightText);
        }
        

Cheers,
Mark


Mohammad Hasan wrote:
> Dear all,
>
>
>
> I need a working example of Gradient Formatter. I want to highlight a
> searched word after it is found in the database. I am using NHibernate
> Search & Lucene. But I am an entrly level programmer, so I do not know how
> to use Gradient Formatter. There are plenty of examples of HTML Formatter
> but no example for Gradient Formatter. Gradient Formatter asks for 5
> arguments, that is where I run into troubles. I do not understand these 5
> arguments. Instead of giving explanation, please post a working example of
> Gradient Formatter of Highlighter which will highlight a searched word after
> it is found in a database. Thanks.
>
> Hasan
>
>   
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG. 
> Version: 7.5.524 / Virus Database: 269.23.6/1403 - Release Date: 29/04/2008 07:26
>   



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


Mime
View raw message