document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public String[] getFragments(int n) {
  2.     String [] fragments = null;
  3.     try {
  4.         // Is IndexReader reachable ?
  5.         if(!(searcher instanceof IndexSearcher))
  6.             return fragments;
  7.        
  8.         IndexReader reader = ((IndexSearcher)searcher).getIndexReader();
  9.        
  10.         if(reader == null)
  11.             return fragments;
  12.        
  13.         String fieldName = "@" + QName.createQName(ContentModel.PROP_CONTENT.getNamespaceURI(), ISO9075.encode(ContentModel.PROP_CONTENT.getLocalName()));
  14.         TermPositionVector termPosVector = (TermPositionVector)reader.getTermFreqVector(hits.id(n), fieldName);
  15.        
  16.         if(termPosVector == null)
  17.             return fragments;
  18.        
  19.         if(analyzer == null)        {
  20.             DictionaryService ds = ((ServiceRegistry)config.getApplicationContext().getBean(ServiceRegistry.SERVICE_REGISTRY)).getDictionaryService();            
  21.             analyzer = new LuceneAnalyser(ds, searchParameters.getMlAnalaysisMode() == null ? config.getDefaultMLSearchAnalysisMode() : searchParameters.getMlAnalaysisMode());
  22.         }
  23.        
  24.         Document doc = hits.doc(n);
  25.         String content = doc.get(fieldName);
  26.         content = content == null ? "" : content;
  27.        
  28.         SpanScorer scorer = new SpanScorer(new QueryParser(fieldName, analyzer).parse(searchParameters.getQuery()),
  29.                 fieldName, new CachingTokenFilter(TokenSources.getTokenStream(termPosVector)));
  30.         Fragmenter fragmenter = new SimpleSpanFragmenter(scorer, 70);
  31.         Highlighter highlighter = new Highlighter(new SpanGradientFormatter(scorer.getMaxTermWeight(), null, null, "#FFFFFF", "#F2FA06"), scorer);
  32.         highlighter.setTextFragmenter(fragmenter);
  33.         highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE);
  34.        
  35.         fragments = highlighter.getBestFragments(TokenSources.getTokenStream(termPosVector), content, 3);
  36.     }
  37.     catch (IOException e)   {
  38.         throw new SearcherException("Query parse Error retrieving hit highlighted fragments", e);
  39.     } catch (ParseException e)  {
  40.         throw new SearcherException("Query parse Error retrieving hit highlighted fragments", e);
  41.     }  
  42.     return fragments;
  43. }
');