public final class PostingsHighlighter extends Object
FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS
.
PostingsHighlighter treats the single original document as the whole corpus, and then scores individual
passages as if they were documents in this corpus. It uses a BreakIterator
to find
passages in the text; by default it breaks using getSentenceInstance(Locale.ROOT)
. It then iterates in parallel (merge sorting by offset) through
the positions of all terms from the query, coalescing those hits that occur in a single passage
into a Passage
, and then scores each Passage using a separate PassageScorer
.
Passages are finally formatted into highlighted snippets with a PassageFormatter
.
WARNING: The code is very new and may still have some exciting bugs!
Example usage:
// configure field with offsets at index time FieldType offsetsType = new FieldType(TextField.TYPE_STORED); offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); Field body = new Field("body", "foobar", offsetsType); // retrieve highlights at query time PostingsHighlighter highlighter = new PostingsHighlighter(); Query query = new TermQuery(new Term("body", "highlighting")); TopDocs topDocs = searcher.search(query, n); String highlights[] = highlighter.highlight("body", query, searcher, topDocs);
This is thread-safe, and can be used across different readers.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_LENGTH
Default maximum content size to process.
|
Constructor and Description |
---|
PostingsHighlighter()
Creates a new highlighter with default parameters.
|
PostingsHighlighter(int maxLength)
Creates a new highlighter, specifying maximum content length.
|
PostingsHighlighter(int maxLength,
BreakIterator breakIterator,
PassageScorer scorer,
PassageFormatter formatter)
Creates a new highlighter with custom parameters.
|
Modifier and Type | Method and Description |
---|---|
String[] |
highlight(String field,
Query query,
IndexSearcher searcher,
TopDocs topDocs)
Highlights the top passages from a single field.
|
String[] |
highlight(String field,
Query query,
IndexSearcher searcher,
TopDocs topDocs,
int maxPassages)
Highlights the top-N passages from a single field.
|
Map<String,String[]> |
highlightFields(String[] fields,
Query query,
IndexSearcher searcher,
TopDocs topDocs)
Highlights the top passages from multiple fields.
|
Map<String,String[]> |
highlightFields(String[] fields,
Query query,
IndexSearcher searcher,
TopDocs topDocs,
int maxPassages)
Highlights the top-N passages from multiple fields.
|
public static final int DEFAULT_MAX_LENGTH
public PostingsHighlighter()
public PostingsHighlighter(int maxLength)
maxLength
- maximum content size to process.IllegalArgumentException
- if maxLength
is negative or Integer.MAX_VALUE
public PostingsHighlighter(int maxLength, BreakIterator breakIterator, PassageScorer scorer, PassageFormatter formatter)
maxLength
- maximum content size to process.breakIterator
- used for finding passage boundaries.scorer
- used for ranking passages.formatter
- used for formatting passages into highlighted snippets.IllegalArgumentException
- if maxLength
is negative or Integer.MAX_VALUE
public String[] highlight(String field, Query query, IndexSearcher searcher, TopDocs topDocs) throws IOException
field
- field name to highlight.
Must have a stored string value and also be indexed with offsets.query
- query to highlight.searcher
- searcher that was previously used to execute the query.topDocs
- TopDocs containing the summary result documents to highlight.topDocs
.
If no highlights were found for a document, its value is null
.IOException
- if an I/O error occurred during processingIllegalArgumentException
- if field
was indexed without
FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS
public String[] highlight(String field, Query query, IndexSearcher searcher, TopDocs topDocs, int maxPassages) throws IOException
field
- field name to highlight.
Must have a stored string value and also be indexed with offsets.query
- query to highlight.searcher
- searcher that was previously used to execute the query.topDocs
- TopDocs containing the summary result documents to highlight.maxPassages
- The maximum number of top-N ranked passages used to
form the highlighted snippets.topDocs
.
If no highlights were found for a document, its value is null
.IOException
- if an I/O error occurred during processingIllegalArgumentException
- if field
was indexed without
FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS
public Map<String,String[]> highlightFields(String[] fields, Query query, IndexSearcher searcher, TopDocs topDocs) throws IOException
Conceptually, this behaves as a more efficient form of:
Map m = new HashMap(); for (String field : fields) { m.put(field, highlight(field, query, searcher, topDocs)); } return m;
fields
- field names to highlight.
Must have a stored string value and also be indexed with offsets.query
- query to highlight.searcher
- searcher that was previously used to execute the query.topDocs
- TopDocs containing the summary result documents to highlight.topDocs
.
If no highlights were found for a document, its value is null
.IOException
- if an I/O error occurred during processingIllegalArgumentException
- if field
was indexed without
FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS
public Map<String,String[]> highlightFields(String[] fields, Query query, IndexSearcher searcher, TopDocs topDocs, int maxPassages) throws IOException
Conceptually, this behaves as a more efficient form of:
Map m = new HashMap(); for (String field : fields) { m.put(field, highlight(field, query, searcher, topDocs, maxPassages)); } return m;
fields
- field names to highlight.
Must have a stored string value and also be indexed with offsets.query
- query to highlight.searcher
- searcher that was previously used to execute the query.topDocs
- TopDocs containing the summary result documents to highlight.maxPassages
- The maximum number of top-N ranked passages per-field used to
form the highlighted snippets.topDocs
.
If no highlights were found for a document, its value is null
.IOException
- if an I/O error occurred during processingIllegalArgumentException
- if field
was indexed without
FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS
Copyright © 2000-2013 Apache Software Foundation. All Rights Reserved.