List of usage examples for org.apache.lucene.search.highlight QueryTermScorer QueryTermScorer
public QueryTermScorer(WeightedTerm[] weightedTerms)
From source file:com._4dconcept.lucene.highlighter.GenericHighlighter.java
License:Apache License
public void highlight(String toHighlight, String field) throws IOException, ParseException { TokenStream tokenStream = analyzer.reusableTokenStream(field, new StringReader(toHighlight)); QueryTermScorer queryTermScorer = new QueryTermScorer(query); TokenStream newStream = queryTermScorer.init(tokenStream); if (newStream != null) { tokenStream = newStream;/*w w w.j a va 2 s . c om*/ } //tokenStream.addAttribute(PositionIncrementAttribute.class); tokenStream.reset(); queryTermScorer.startFragment(null); int lastEndOffset = 0; TokenGroup tokenGroup = new TokenGroup(tokenStream); for (boolean next = tokenStream.incrementToken(); next; next = tokenStream.incrementToken()) { if ((tokenGroup.numTokens > 0) && tokenGroup.isDistinct()) { lastEndOffset = extractText(tokenGroup, toHighlight, lastEndOffset); } tokenGroup.addToken(queryTermScorer.getTokenScore()); } if (tokenGroup.numTokens > 0) { lastEndOffset = extractText(tokenGroup, toHighlight, lastEndOffset); } //Test what remains of the original text beyond the point where we stopped analyzing if ((lastEndOffset < toHighlight.length())) { //append it to the last fragment callback.terms(toHighlight.substring(lastEndOffset), lastEndOffset, tokenGroup.getTotalScore()); } }
From source file:de.innovationgate.wgpublisher.lucene.LuceneManager.java
License:Open Source License
public Highlighter createHighlighter(String itemOrMeta, Query query, Formatter formatter) { if (itemOrMeta == null) { return null; }/* w ww . j a v a 2s . c om*/ if (query == null) { return null; } // create scorer // use only terms for this item and terms for allcontent for scoring List terms = new ArrayList(); WeightedTerm[] termsForThisItem = QueryTermExtractor.getTerms(query, false, itemOrMeta); if (termsForThisItem != null) { for (int i = 0; i < termsForThisItem.length; i++) { terms.add(termsForThisItem[i]); } } WeightedTerm[] termsForAllContent = QueryTermExtractor.getTerms(query, false, LuceneManager.INDEXFIELD_ALLCONTENT); if (termsForAllContent != null) { for (int i = 0; i < termsForAllContent.length; i++) { terms.add(termsForAllContent[i]); } } WeightedTerm[] termsArray = new WeightedTerm[terms.size()]; for (int i = 0; i < terms.size(); i++) { WeightedTerm term = (WeightedTerm) terms.get(i); termsArray[i] = term; } QueryTermScorer scorer = new QueryTermScorer(termsArray); // create highlighter Highlighter highlighter = new Highlighter(formatter, scorer); return highlighter; }
From source file:drakkar.mast.retrieval.LuceneContext.java
/** * Para la sumarizacin/*w w w . j a v a 2 s. c o m*/ * * @return */ private String getHighlighter(Query q, Analyzer a, String text, String field) { String summary = null; this.hg = new Highlighter(new QueryTermScorer(q)); this.hg.setTextFragmenter(new SimpleFragmenter(20)); this.hg.setMaxDocCharsToAnalyze(600); try { try { this.tokens = TokenSources.getTokenStream(field, text, a); summary = this.hg.getBestFragments(this.tokens, text, 20, "..."); // summary = this.hg.getBestFragments(this.tokens, text, 10).toString(); } catch (IOException ex) { OutputMonitor.printStream("IO", ex); } } catch (InvalidTokenOffsetsException ex) { OutputMonitor.printStream("", ex); } if (summary == null) { summary = " "; } return summary; }