lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Li Leon <leon800...@gmail.com>
Subject Highlight the whole sentence instead of the partial matching terms
Date Mon, 11 Jan 2010 07:00:17 GMT
*Hi all,*
**
*I wanted to use to following code to highlight the whole sentence with
"\"something\"~n" to be parsed.*
**
*The QueryParser part worked well, but when integrated with Highlighter, it
ended up with exception. *
**
*Does anyone have any clue as I'm investigating this.*
**
**
*Thanks,*
**



Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/lucene/index/memory/MemoryIndex

      at
org.apache.lucene.search.highlight.WeightedSpanTermExtractor.getReaderForField(
*WeightedSpanTermExtractor.java:361*)

      at
org.apache.lucene.search.highlight.WeightedSpanTermExtractor.extractWeightedSpanTerms(
*WeightedSpanTermExtractor.java:282*)

      at
org.apache.lucene.search.highlight.WeightedSpanTermExtractor.extract(*
WeightedSpanTermExtractor.java:149*)

      at
org.apache.lucene.search.highlight.WeightedSpanTermExtractor.getWeightedSpanTerms(
*WeightedSpanTermExtractor.java:414*)

      at org.apache.lucene.search.highlight.QueryScorer.initExtractor(*
QueryScorer.java:216*)

      at org.apache.lucene.search.highlight.QueryScorer.init(*
QueryScorer.java:184*)

      at
org.apache.lucene.search.highlight.Highlighter.getBestTextFragments(*
Highlighter.java:226*)

     at org.apache.lucene.search.highlight.Highlighter.getBestFragments(*
Highlighter.java:184*)

     at org.apache.lucene.search.highlight.Highlighter.getBestFragments(*
Highlighter.java:488*)

      at Hightlight.main(*Hightlight.java:182*)


**
*public* *class* Hightlight {

  *private* *static* *final* String *text* =

    "Giving end users some context around hits from their searches is
friendly and, more important, useful. An useful example is friendly Google
search context.";



  *private* *static* *final* String[] *stopwords* = {};

  *private* *static* IndexSearcher *searcher*;

  *public* *static* *void* main(String[] args) *throws* Exception {



    String filename = args[0];



    String searchText = "\"Giving and\"~11";
#1

    QueryParser parser = *new* QueryParser("f", *new*

            StandardAnalyzer(*stopwords*));

    Query query = parser.parse(searchText);



    SimpleHTMLFormatter formatter =

      *new* SimpleHTMLFormatter("<span class=\"highlight\">",

                              "</span>");



    TokenStream tokens = *new* StandardAnalyzer(*stopwords*)

        .tokenStream("f", *new* StringReader(*text*));



    QueryScorer scorer = *new* QueryScorer(query, "f");



    Highlighter highlighter = *new* Highlighter(formatter, scorer);

    highlighter.setTextFragmenter(

                  *new* NullFragmenter());



    String result =

        highlighter.getBestFragments(tokens, *text*, 3, "...");



    FileWriter writer = *new* FileWriter(filename);

    writer.write("<html>");

    writer.write("<style>\n" +

        ".highlight {\n" +

        " background: yellow;\n" +

        "}\n" +

        "</style>");

    writer.write("<body>");

    writer.write(result);

    writer.write("</body></html>");

    writer.close();

  }

}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message