private void append(StringBuilder dest, String content, int start, int end) { dest.append(encoder.encodeText(content.substring(start, end))); } }
protected void extract(Query query, float boost, Map<String, WeightedSpanTerm> terms) throws IOException { if (isChildOrParentQuery(query.getClass())) { // skip has_child or has_parent queries, see: https://github.com/elastic/elasticsearch/issues/14999 return; } else if (query instanceof FunctionScoreQuery) { super.extract(((FunctionScoreQuery) query).getSubQuery(), boost, terms); } else if (query instanceof ESToParentBlockJoinQuery) { super.extract(((ESToParentBlockJoinQuery) query).getChildQuery(), boost, terms); } else { super.extract(query, boost, terms); } }
QueryScorer queryScorer = new CustomQueryScorer(highlighterContext.query, field.fieldOptions().requireFieldMatch() ? fieldType.name() : null); queryScorer.setExpandMultiTermQuery(true); Fragmenter fragmenter; if (field.fieldOptions().numberOfFragments() == 0) { fragmenter = new NullFragmenter(); } else if (field.fieldOptions().fragmenter() == null) { fragmenter = new SimpleSpanFragmenter(queryScorer, field.fieldOptions().fragmentCharSize()); } else if ("simple".equals(field.fieldOptions().fragmenter())) { fragmenter = new SimpleFragmenter(field.fieldOptions().fragmentCharSize()); } else if ("span".equals(field.fieldOptions().fragmenter())) { fragmenter = new SimpleSpanFragmenter(queryScorer, field.fieldOptions().fragmentCharSize()); } else { throw new IllegalArgumentException("unknown fragmenter option [" + field.fieldOptions().fragmenter() + "] for the field [" + highlighterContext.fieldName + "]"); Formatter formatter = new SimpleHTMLFormatter(field.fieldOptions().preTags()[0], field.fieldOptions().postTags()[0]); entry = new org.apache.lucene.search.highlight.Highlighter(formatter, encoder, queryScorer); entry.setTextFragmenter(fragmenter); entry.setMaxDocCharsToAnalyze(Integer.MAX_VALUE); TextFragment[] bestTextFragments = entry.getBestTextFragments(tokenStream, text, false, numberOfFragments); for (TextFragment bestTextFragment : bestTextFragments) { if (bestTextFragment != null && bestTextFragment.getScore() > 0) { fragsList.add(bestTextFragment); fragments = new String[fragsList.size()];
@Override @Deprecated public void highlight(URI property) { Formatter formatter = new SimpleHTMLFormatter(SearchFields.HIGHLIGHTER_PRE_TAG, SearchFields.HIGHLIGHTER_POST_TAG); highlighter = new Highlighter(formatter, new QueryScorer(query)); } }
Scorer scorer = new QueryScorer(textQuery); Highlighter highlighter = new Highlighter(formatter, scorer); Analyzer analyzer = em.getSearchFactory().getAnalyzer(ResourceTable.class); highlighter.getBestFragments(analyzer.tokenStream("myContentTextPhonetic", nextValue), nextValue, 10); highlighter.getBestFragments(analyzer.tokenStream("myContentTextNGram", nextValue), nextValue, 10); highlighter.getBestFragments(analyzer.tokenStream("myContentTextEdgeNGram", nextValue), nextValue, 10);
private void resetHighlighter() { if (formatter != null) { this.highlighter = new Highlighter(formatter, new QueryScorer(query)); this.highlighter.setEncoder(new DefaultEncoder()); } else { this.highlighter = null; } } }
/** * The constructor. * * @param searchAnalyzer is the {@link Analyzer} used by the * {@link LuceneSearchEngine search-engine}. * @param formatter is the formatter used to highlight terms. * @param searchQuery is the {@link Query} of the search. Matching terms of * this query shall be highlighted. */ public LuceneSearchHighlighter(Analyzer searchAnalyzer, Formatter formatter, Query searchQuery) { super(); this.highlighter = new Highlighter(formatter, new QueryScorer(searchQuery)); this.analyzer = searchAnalyzer; }
StandardHLImpl(boolean termVecs) { highlighter.setEncoder(new DefaultEncoder()); highlighter.setMaxDocCharsToAnalyze(maxDocCharsToAnalyze); this.termVecs = termVecs; }
@Override public String highlightTerm(String theOriginalText, TokenGroup theTokenGroup) { ourLog.debug("{} Found {} with score {}", myAnalyzer, theOriginalText, theTokenGroup.getTotalScore()); if (theTokenGroup.getTotalScore() > 0) { float score = theTokenGroup.getTotalScore(); if (theOriginalText.equalsIgnoreCase(myOriginalSearch)) { score = score + 1.0f; } mySuggestions.add(new Suggestion(theOriginalText, score)); } else if (myPartialMatchPhrases != null) { if (theOriginalText.length() < 100) { for (int i = 0; i < myPartialMatchPhrases.size(); i++) { if (theOriginalText.contains(myPartialMatchPhrases.get(i))) { mySuggestions.add(new Suggestion(theOriginalText, myPartialMatchScores.get(i) - 0.5f)); } } } } return null; }
/** * Extracts all terms texts of a given Query into an array of WeightedTerms * * @param query Query to extract term texts from * @return an array of the terms used in a query, plus their weights. */ public static final WeightedTerm[] getTerms(Query query) { return getTerms(query,false); }
if (savedInstanceState == null) { getSupportFragmentManager() .beginTransaction() .add(R.id.main_container_1, new OpenGLES20ActivityFrag()) .commit(); getSupportFragmentManager() .beginTransaction() .add(R.id.main_container_2, new TextFragment()) .commit(); }
/** * @param query Query to use for highlighting */ public QueryScorer(Query query) { init(query, null, null, true); }
@Override public Fields getTermVectors(int docID) throws IOException { if (docID != 0) { return null; } return fields(); }
@Override public String encodeText(String originalText) { return htmlEncode(originalText); }
@Override @Deprecated public void highlight(IRI property) { Formatter formatter = new SimpleHTMLFormatter(SearchFields.HIGHLIGHTER_PRE_TAG, SearchFields.HIGHLIGHTER_POST_TAG); highlighter = new Highlighter(formatter, new QueryScorer(query)); } }
@Override public String highlightTerm(String term, TokenGroup group) { if (group.getTotalScore() > 0) { return "<span class=\"im-highlighted-search-term\">" + term + "</span>"; } return term; } };
/** * * @param query a Lucene query (ideally rewritten using query.rewrite before * being passed to this class and the searcher) * @param fieldName the Field name which is used to match Query terms */ public QueryTermScorer(Query query, String fieldName) { this(QueryTermExtractor.getTerms(query, false, fieldName)); }
/** * 构建org.apache.lucene.search.highlight.Highlighter对象 * * @return */ public org.apache.lucene.search.highlight.Highlighter buildHighlighter(Query query) { String perfix = getPerfix(); String suffix = getSuffix(); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(perfix, suffix); Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query)); return highlighter; }
@Override public String highlightTerm(String term, TokenGroup group) { if (group.getTotalScore() > 0) { return "<span class=\"im-highlighted-search-term\">" + term + "</span>"; } return term; } };