Class CustomUnifiedHighlighter
java.lang.Object
org.apache.lucene.search.uhighlight.UnifiedHighlighter
org.elasticsearch.lucene.search.uhighlight.CustomUnifiedHighlighter
public class CustomUnifiedHighlighter
extends org.apache.lucene.search.uhighlight.UnifiedHighlighter
Subclass of the
UnifiedHighlighter
that works for a single field in a single document.
Uses a custom PassageFormatter
. Accepts field content as a constructor
argument, given that loadings field value can be done reading from _source field.
Supports using different BreakIterator
to break the text into fragments. Considers every distinct field
value as a discrete passage for highlighting (unless the whole content needs to be highlighted).
Supports both returning empty snippets and non highlighted snippets when no highlighting can be performed.-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.lucene.search.uhighlight.UnifiedHighlighter
org.apache.lucene.search.uhighlight.UnifiedHighlighter.HighlightFlag, org.apache.lucene.search.uhighlight.UnifiedHighlighter.LimitedStoredFieldVisitor, org.apache.lucene.search.uhighlight.UnifiedHighlighter.OffsetSource
-
Field Summary
Fields inherited from class org.apache.lucene.search.uhighlight.UnifiedHighlighter
DEFAULT_CACHE_CHARS_THRESHOLD, DEFAULT_MAX_LENGTH, fieldInfos, indexAnalyzer, searcher, ZERO_LEN_AUTOMATA_ARRAY
-
Constructor Summary
ConstructorDescriptionCustomUnifiedHighlighter
(org.apache.lucene.search.IndexSearcher searcher, org.apache.lucene.analysis.Analyzer analyzer, org.apache.lucene.search.uhighlight.UnifiedHighlighter.OffsetSource offsetSource, org.apache.lucene.search.uhighlight.PassageFormatter passageFormatter, Locale breakIteratorLocale, BreakIterator breakIterator, String index, String field, org.apache.lucene.search.Query query, int noMatchSize, int maxPassages, Predicate<String> fieldMatcher, int maxAnalyzedOffset, Integer queryMaxAnalyzedOffset) Creates a new instance ofCustomUnifiedHighlighter
-
Method Summary
Modifier and TypeMethodDescriptionprotected BreakIterator
getBreakIterator
(String field) protected org.elasticsearch.lucene.search.uhighlight.CustomFieldHighlighter
getFieldHighlighter
(String field, org.apache.lucene.search.Query query, Set<org.apache.lucene.index.Term> allTerms, int maxPassages) protected Set<org.apache.lucene.search.uhighlight.UnifiedHighlighter.HighlightFlag>
org.apache.lucene.search.uhighlight.PassageFormatter
protected org.apache.lucene.search.uhighlight.PassageFormatter
getFormatter
(String field) protected org.apache.lucene.search.uhighlight.UnifiedHighlighter.OffsetSource
getOffsetSource
(String field) Forces the offset source for this highlighterSnippet[]
highlightField
(org.apache.lucene.index.LeafReader reader, int docId, CheckedSupplier<String, IOException> loadFieldValue) Highlights the field value.protected Collection<org.apache.lucene.search.Query>
preSpanQueryRewrite
(org.apache.lucene.search.Query query) Methods inherited from class org.apache.lucene.search.uhighlight.UnifiedHighlighter
extractTerms, filterExtractedTerms, getAutomata, getCacheFieldValCharsThreshold, getFieldInfo, getFieldMatcher, getHighlightComponents, getIndexAnalyzer, getIndexSearcher, getMaxLength, getMaxNoHighlightPassages, getOffsetStrategy, getOptimizedOffsetSource, getPhraseHelper, getScorer, hasUnrecognizedQuery, highlight, highlight, highlightFields, highlightFields, highlightFields, highlightFieldsAsObjects, highlightWithoutSearcher, loadFieldValues, newLimitedStoredFieldsVisitor, requiresRewrite, setBreakIterator, setCacheFieldValCharsThreshold, setFieldMatcher, setFormatter, setHandleMultiTermQuery, setHighlightPhrasesStrictly, setMaxLength, setMaxNoHighlightPassages, setScorer, shouldHandleMultiTermQuery, shouldHighlightPhrasesStrictly, shouldPreferPassageRelevancyOverSpeed
-
Field Details
-
MULTIVAL_SEP_CHAR
public static final char MULTIVAL_SEP_CHAR- See Also:
-
-
Constructor Details
-
CustomUnifiedHighlighter
public CustomUnifiedHighlighter(org.apache.lucene.search.IndexSearcher searcher, org.apache.lucene.analysis.Analyzer analyzer, org.apache.lucene.search.uhighlight.UnifiedHighlighter.OffsetSource offsetSource, org.apache.lucene.search.uhighlight.PassageFormatter passageFormatter, @Nullable Locale breakIteratorLocale, @Nullable BreakIterator breakIterator, String index, String field, org.apache.lucene.search.Query query, int noMatchSize, int maxPassages, Predicate<String> fieldMatcher, int maxAnalyzedOffset, Integer queryMaxAnalyzedOffset) throws IOException Creates a new instance ofCustomUnifiedHighlighter
- Parameters:
analyzer
- the analyzer used for the field at index time, used for multi term queries internally.offsetSource
- theUnifiedHighlighter.OffsetSource
to used for offsets retrieval.passageFormatter
- our ownCustomPassageFormatter
which generates snippets in forms ofSnippet
objects.breakIteratorLocale
- theLocale
to use for dividing text into passages. If nullLocale.ROOT
is used.breakIterator
- theBreakIterator
to use for dividing text into passages. If nullBreakIterator.getSentenceInstance(Locale)
is used.index
- the index we're highlighting, mostly used for error messagesfield
- the name of the field we're highlightingquery
- the query we're highlightingnoMatchSize
- The size of the text that should be returned when no highlighting can be performed.maxPassages
- the maximum number of passes to highlightfieldMatcher
- decides which terms should be highlightedmaxAnalyzedOffset
- if the field is more than this long we'll refuse to use the ANALYZED offset source for it because it'd be super slow- Throws:
IOException
-
-
Method Details
-
highlightField
public Snippet[] highlightField(org.apache.lucene.index.LeafReader reader, int docId, CheckedSupplier<String, IOException> loadFieldValue) throws IOExceptionHighlights the field value.- Throws:
IOException
-
getBreakIterator
- Overrides:
getBreakIterator
in classorg.apache.lucene.search.uhighlight.UnifiedHighlighter
-
getFormatter
public org.apache.lucene.search.uhighlight.PassageFormatter getFormatter() -
getFormatter
- Overrides:
getFormatter
in classorg.apache.lucene.search.uhighlight.UnifiedHighlighter
-
getFieldHighlighter
protected org.elasticsearch.lucene.search.uhighlight.CustomFieldHighlighter getFieldHighlighter(String field, org.apache.lucene.search.Query query, Set<org.apache.lucene.index.Term> allTerms, int maxPassages) - Overrides:
getFieldHighlighter
in classorg.apache.lucene.search.uhighlight.UnifiedHighlighter
-
getFlags
protected Set<org.apache.lucene.search.uhighlight.UnifiedHighlighter.HighlightFlag> getFlags(String field) - Overrides:
getFlags
in classorg.apache.lucene.search.uhighlight.UnifiedHighlighter
-
preSpanQueryRewrite
protected Collection<org.apache.lucene.search.Query> preSpanQueryRewrite(org.apache.lucene.search.Query query) - Overrides:
preSpanQueryRewrite
in classorg.apache.lucene.search.uhighlight.UnifiedHighlighter
-
getOffsetSource
protected org.apache.lucene.search.uhighlight.UnifiedHighlighter.OffsetSource getOffsetSource(String field) Forces the offset source for this highlighter- Overrides:
getOffsetSource
in classorg.apache.lucene.search.uhighlight.UnifiedHighlighter
-