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

    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 = is.search(query);
    IndexReader indexReader=IndexReader.open("luceneIndex");

    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 = hits.id(i);

    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"));
    }
    is.close();

    Thanks in advance

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

    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
                lighter.setTextFragmenter(fragmenter);
                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) {
                ex.printStackTrace();
            } catch (InvalidTokenOffsetsException ex) {
                ex.printStackTrace();
            }
            return fragments;
        }

Similar Threads

  1. helpset findHelpSet returns null
    By kmm1977 in forum AWT / Swing
    Replies: 23
    Last Post: 06-22-2010, 03:53 PM
  2. IndexSearch.docfreq returns 0.0
    By noir in forum Lucene
    Replies: 0
    Last Post: 04-26-2010, 09:32 AM
  3. Carriage Returns in JTEXTAREA
    By AJArmstron@aol.com in forum Advanced Java
    Replies: 8
    Last Post: 04-17-2010, 07:34 PM
  4. NetworkInterface returns only ports that are up
    By dogbert in forum Networking
    Replies: 0
    Last Post: 10-13-2009, 05: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, 09: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
  •