Search code examples
lucenequery-parserlucene-highlighterfast-vector-highlighter

Lucene MultiFieldQueryParser and Highlighter


I am indexing articles in lucene index through different fields i.e. title, description, link, publishDate

I query the index using MultiFieldQueryParser like

+(title:[text]^5.0 description:[text]^4.0 link:[text]^3.0) +publishDate:[20150101 TO 20150531]

and then i show the articles as the search results. So far all is good. Now I want to highlight the search text in the title,description

How shall i go about this? The normal Highlighter gives me NullPointerException while generrating fragments. and PostingHighlighter gives me a Map with results grouped together according to the field.. but i don't want it that way. I was the entire document to be returned together with highlighting of search text in title and description.

Any help or suggestion or code snippet is appreciated..


Solution

  • I got it working by using the FieldType for all the fields that I wanted highlighted:

    FieldType ft = new FieldType();
    ft.setIndexed(true);         
    ft.setIndexOptionsFieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    ft.setStored(true);
    ft.setStoreTermVectors(true);
    ft.setStoreTermVectorOffsets(true);
    ft.setTokenized(true);
    ft.stored();
    
    QueryScorer qs = new QueryScorer(q);
    Highlighter h = new Highlighter(qs);
    highlighter.setTextFragmenter(new SimpleFragmenter(300));           
    String highlighted = h.getBestFragment(new StandardAnalyzer(),f,Text);