Results 1 to 2 of 2
  1. #1
    zee is offline Member
    Join Date
    Jul 2010
    Rep Power

    Default Lucene highlighter returns null fragments always

    Hey folks,
    I was trying to implement highlighter in my search results on my search engine. I was able to create the index and successfully search the index but i was not able to implement highlighting chunks in the search result. Im using lucene version 2.92 and highlighter package version 2.92.
    The code below returns null fragments always..not able to figure out y

    IndexSearcher is = new IndexSearcher("luceneIndex");
    Analyzer analyzer = new StandardAnalyzer();
    QueryParser parser = new QueryParser("content", analyzer);
    Query query = parser.parse("audit");
    Hits hits =;

    QueryScorer scorer=new QueryScorer(query,indexReader,"storage");
    Highlighter highlighter=new Highlighter(scorer);

    for (int i=0; i<hits.length(); i++) {
    Document doc = hits.doc(i);

    int id =;

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

    TokenStream tokenStream = analyzer.tokenStream("storage", new StringReader(text));

    String fragments = highlighter.getBestFragment(tokenStream, text);

    System.out.println((i + 1) + ". " + doc.get("path"));

    Thanks in advance

  2. #2
    looserdgr8 is offline Member
    Join Date
    Aug 2010
    Rep Power

    Post I have implemented highlighter using lucene 3.02

    I have used lucene-highlighter-3.01.jar file.

    The code is

    Java Code:
    //here text is the search result text where the highlighter will be implemented
    public String highlightText(String text, Query luceneQuery){
            String fragments;
            fragments= "";
            try {
                 //here simpleHTMLFormatter highlights the query words in bold and italic
                SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b><i>", "</i></b>");
                QueryScorer scorer = new QueryScorer(luceneQuery);
                Highlighter lighter = new Highlighter(formatter, scorer);
                Fragmenter fragmenter = new SimpleFragmenter(40);  //40 means every fragments length will be 40
                TokenStream stream = new WhitespaceAnalyzer().tokenStream(null, new StringReader(text));
                fragments = lighter.getBestFragments(stream, text, 6, ".....");  //maximum 6 fragments....can be less then 6
                System.out.println("Fragments= " + fragments);   //we should display the fragments
            } catch (IOException ex) {
            } catch (InvalidTokenOffsetsException ex) {
            return fragments;

Similar Threads

  1. helpset findHelpSet returns null
    By kmm1977 in forum AWT / Swing
    Replies: 23
    Last Post: 06-22-2010, 04:53 PM
  2. IndexSearch.docfreq returns 0.0
    By noir in forum Lucene
    Replies: 0
    Last Post: 04-26-2010, 10:32 AM
  3. Carriage Returns in JTEXTAREA
    By in forum Advanced Java
    Replies: 8
    Last Post: 04-17-2010, 08:34 PM
  4. NetworkInterface returns only ports that are up
    By dogbert in forum Networking
    Replies: 0
    Last Post: 10-13-2009, 06:12 PM
  5. What's the need for co-variant returns ?
    By ajeeb in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 10:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts