本文整理汇总了Java中org.apache.lucene.search.highlight.Highlighter.getBestTextFragments方法的典型用法代码示例。如果您正苦于以下问题:Java Highlighter.getBestTextFragments方法的具体用法?Java Highlighter.getBestTextFragments怎么用?Java Highlighter.getBestTextFragments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.search.highlight.Highlighter
的用法示例。
在下文中一共展示了Highlighter.getBestTextFragments方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: performHighlighting
import org.apache.lucene.search.highlight.Highlighter; //导入方法依赖的package包/类
/**
* Performs highlighting for a given query and a given document.
*
* @param indexSearcher the IndexSearcher performing the query
* @param query the Tripod LuceneQuery
* @param scoreDoc the Lucene ScoreDoc
* @param doc the Lucene Document
* @param highlighter the Highlighter to use
* @param result the QueryResult to add the highlights to
* @throws IOException if an error occurs performing the highlighting
* @throws InvalidTokenOffsetsException if an error occurs performing the highlighting
*/
protected void performHighlighting(final IndexSearcher indexSearcher, final Query query, final ScoreDoc scoreDoc,
final Document doc, final Highlighter highlighter, final QR result)
throws IOException, InvalidTokenOffsetsException {
if (query.getHighlightFields() == null || query.getHighlightFields().isEmpty()) {
return;
}
final List<Highlight> highlights = new ArrayList<>();
final List<String> hlFieldNames = getHighlightFieldNames(query, doc);
// process each field to highlight on
for (String hlField : hlFieldNames) {
final String text = doc.get(hlField);
if (StringUtils.isEmpty(text)) {
continue;
}
final List<String> snippets = new ArrayList<>();
final Fields tvFields = indexSearcher.getIndexReader().getTermVectors(scoreDoc.doc);
final int maxStartOffset = highlighter.getMaxDocCharsToAnalyze() -1;
// get the snippets for the given field
final TokenStream tokenStream = TokenSources.getTokenStream(hlField, tvFields, text, analyzer, maxStartOffset);
final TextFragment[] textFragments = highlighter.getBestTextFragments(tokenStream, text, false, 10);
for (TextFragment textFragment : textFragments) {
if (textFragment != null && textFragment.getScore() > 0) {
snippets.add(textFragment.toString());
}
}
// if we have snippets then add a highlight result to the QueryResult
if (snippets.size() > 0) {
highlights.add(new Highlight(hlField, snippets));
}
}
result.setHighlights(highlights);
}
示例2: getBenchmarkHighlighter
import org.apache.lucene.search.highlight.Highlighter; //导入方法依赖的package包/类
@Override
protected BenchmarkHighlighter getBenchmarkHighlighter(Query q){
highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(q));
highlighter.setMaxDocCharsToAnalyze(maxDocCharsToAnalyze);
return new BenchmarkHighlighter(){
@Override
public int doHighlight(IndexReader reader, int doc, String field,
Document document, Analyzer analyzer, String text) throws Exception {
TokenStream ts = TokenSources.getAnyTokenStream(reader, doc, field, document, analyzer);
TextFragment[] frag = highlighter.getBestTextFragments(ts, text, mergeContiguous, maxFrags);
return frag != null ? frag.length : 0;
}
};
}
示例3: getBenchmarkHighlighter
import org.apache.lucene.search.highlight.Highlighter; //导入方法依赖的package包/类
@Override
public BenchmarkHighlighter getBenchmarkHighlighter(Query q) {
highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(q));
return new BenchmarkHighlighter() {
@Override
public int doHighlight(IndexReader reader, int doc, String field, Document document, Analyzer analyzer, String text) throws Exception {
TokenStream ts = TokenSources.getAnyTokenStream(reader, doc, field, document, analyzer);
TextFragment[] frag = highlighter.getBestTextFragments(ts, text, mergeContiguous, maxFrags);
numHighlightedResults += frag != null ? frag.length : 0;
return frag != null ? frag.length : 0;
}
};
}
示例4: highlight
import org.apache.lucene.search.highlight.Highlighter; //导入方法依赖的package包/类
/**
* NOTE: This method will not preserve the correct field types.
*
* @param preTag
* @param postTag
*/
public static Document highlight(int docId, Document document, Query query, FieldManager fieldManager,
IndexReader reader, String preTag, String postTag) throws IOException, InvalidTokenOffsetsException {
String fieldLessFieldName = fieldManager.getFieldLessFieldName();
Query fixedQuery = fixSuperQuery(query, null, fieldLessFieldName);
Analyzer analyzer = fieldManager.getAnalyzerForQuery();
SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(preTag, postTag);
Document result = new Document();
for (IndexableField f : document) {
String name = f.name();
if (fieldLessFieldName.equals(name) || FIELDS_NOT_TO_HIGHLIGHT.contains(name)) {
result.add(f);
continue;
}
String text = f.stringValue();
Number numericValue = f.numericValue();
Query fieldFixedQuery;
if (fieldManager.isFieldLessIndexed(name)) {
fieldFixedQuery = fixSuperQuery(query, name, fieldLessFieldName);
} else {
fieldFixedQuery = fixedQuery;
}
if (numericValue != null) {
if (shouldNumberBeHighlighted(name, numericValue, fieldFixedQuery)) {
String numberHighlight = preTag + text + postTag;
result.add(new StringField(name, numberHighlight, Store.YES));
}
} else {
Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(fieldFixedQuery, name));
TokenStream tokenStream = TokenSources.getAnyTokenStream(reader, docId, name, analyzer);
TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);
for (int j = 0; j < frag.length; j++) {
if ((frag[j] != null) && (frag[j].getScore() > 0)) {
result.add(new StringField(name, frag[j].toString(), Store.YES));
}
}
}
}
return result;
}