@Override protected BreakIterator getBreakIterator(String field) { if (breakIterator == null) { return super.getBreakIterator(field); } return breakIterator; }
@Override protected Passage[] getEmptyHighlight(String fieldName, BreakIterator bi, int maxPassages) { if (returnNonHighlightedSnippets) { //we want to return the first sentence of the first snippet only return super.getEmptyHighlight(fieldName, bi, 1); } return EMPTY_PASSAGE; }
@Override public int next() { if (current == end) { return DONE; } else { return last(); } }
PostingsHighlighter highlighter2 = new PostingsHighlighter(); TopDocs docs = indexSearcher.search(myQuery, 10); String[] highlightedResults = highlighter.highlight( CONTENT_FIELD_NAME, myQuery, indexSearcher, docs );
/** Returns the {@link PassageScorer} to use for * ranking passages. This * returns a new {@code PassageScorer} by default; * subclasses can override to customize. */ protected PassageScorer getScorer(String field) { if (defaultScorer == null) { defaultScorer = new PassageScorer(); } return defaultScorer; }
/** * Highlights terms extracted from the provided query within the content of the provided field name */ public Snippet[] highlightField(String field, Query query, IndexSearcher searcher, int docId, int maxPassages) throws IOException { Map<String, Object[]> fieldsAsObjects = super.highlightFieldsAsObjects(new String[]{field}, query, searcher, new int[]{docId}, new int[]{maxPassages}); Object[] snippetObjects = fieldsAsObjects.get(field); if (snippetObjects != null) { //one single document at a time assert snippetObjects.length == 1; Object snippetObject = snippetObjects[0]; if (snippetObject != null && snippetObject instanceof Snippet[]) { return (Snippet[]) snippetObject; } } return EMPTY_SNIPPET; }
@Override public int previous() { if (current == start) { return DONE; } else { return first(); } }
/** Returns the {@link PassageFormatter} to use for * formatting passages into highlighted snippets. This * returns a new {@code PassageFormatter} by default; * subclasses can override to customize. */ protected PassageFormatter getFormatter(String field) { if (defaultFormatter == null) { defaultFormatter = new DefaultPassageFormatter(); } return defaultFormatter; }
/** * Highlights the top passages from a single field. * * @param field field name to highlight. * Must have a stored string value and also be indexed with offsets. * @param query query to highlight. * @param searcher searcher that was previously used to execute the query. * @param topDocs TopDocs containing the summary result documents to highlight. * @return Array of formatted snippets corresponding to the documents in <code>topDocs</code>. * If no highlights were found for a document, the * first sentence for the field will be returned. * @throws IOException if an I/O error occurred during processing * @throws IllegalArgumentException if <code>field</code> was indexed without * {@link IndexOptions#DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS} */ public String[] highlight(String field, Query query, IndexSearcher searcher, TopDocs topDocs) throws IOException { return highlight(field, query, searcher, topDocs, 1); }
void sort() { final int starts[] = matchStarts; final int ends[] = matchEnds; final BytesRef terms[] = matchTerms; new InPlaceMergeSorter() { @Override protected void swap(int i, int j) { int temp = starts[i]; starts[i] = starts[j]; starts[j] = temp; temp = ends[i]; ends[i] = ends[j]; ends[j] = temp; BytesRef tempTerm = terms[i]; terms[i] = terms[j]; terms[j] = tempTerm; } @Override protected int compare(int i, int j) { return Integer.compare(starts[i], starts[j]); } }.sort(0, numMatches); }
@Override public int previous() { if (text.getIndex() == text.getBeginIndex()) { return DONE; } else { return advanceBackward(); } }
@Override public int next() { if (text.getIndex() == text.getEndIndex()) { return DONE; } else { return advanceForward(); } }
@Override protected BreakIterator getBreakIterator(String field) { if (definition.breakIterator == null) return new WholeBreakIterator(); switch (definition.breakIterator.type) { case character: return BreakIterator.getCharacterInstance(locale); case word: return BreakIterator.getWordInstance(locale); case line: return BreakIterator.getLineInstance(locale); default: case sentence: return BreakIterator.getSentenceInstance(locale); } }
@Override protected BreakIterator getBreakIterator(String field) { if (breakIterator == null) { return super.getBreakIterator(field); } return breakIterator; }
@Override protected Passage[] getEmptyHighlight(String fieldName, BreakIterator bi, int maxPassages) { if (returnNonHighlightedSnippets) { //we want to return the first sentence of the first snippet only return super.getEmptyHighlight(fieldName, bi, 1); } return EMPTY_PASSAGE; }
@Override public int following(int pos) { if (pos < start || pos > end) { throw new IllegalArgumentException("offset out of bounds"); } else if (pos == end) { // this conflicts with the javadocs, but matches actual behavior (Oracle has a bug in something) // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=9000909 current = end; return DONE; } else { return last(); } }
@Override public int preceding(int pos) { if (pos < start || pos > end) { throw new IllegalArgumentException("offset out of bounds"); } else if (pos == start) { // this conflicts with the javadocs, but matches actual behavior (Oracle has a bug in something) // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=9000909 current = start; return DONE; } else { return first(); } }