return highlightFields(fields, query, searcher, docids, maxPassages);
PostingsHighlighter highlighter2 = new PostingsHighlighter(); TopDocs docs = indexSearcher.search(myQuery, 10); String[] highlightedResults = highlighter.highlight( CONTENT_FIELD_NAME, myQuery, indexSearcher, docs );
@Override protected BreakIterator getBreakIterator(String field) { if (breakIterator == null) { return super.getBreakIterator(field); } return breakIterator; }
private Map<Integer,Object> highlightField(String field, String contents[], BreakIterator bi, BytesRef terms[], int[] docids, List<LeafReaderContext> leaves, int maxPassages, Query query) throws IOException { Map<Integer,Object> highlights = new HashMap<>(); PassageFormatter fieldFormatter = getFormatter(field); if (fieldFormatter == null) { throw new NullPointerException("PassageFormatter cannot be null"); Analyzer analyzer = getIndexAnalyzer(field); CharacterRunAutomaton automata[] = new CharacterRunAutomaton[0]; if (analyzer != null) { Passage passages[] = highlightDoc(field, terms, content.length(), bi, doc - subContext.docBase, termsEnum, postings, maxPassages); passages = getEmptyHighlight(field, bi, maxPassages);
PostingsHighlighter highlighter = new PostingsHighlighter(1000000, BreakIterator.getSentenceInstance(Locale.ROOT), new PassageScorer(), new PassageFormatter() { @Override public String format(Passage passages[], String content) { Map<String, String[]> highlights = highlighter.highlightFields(new String[] { "title", "description" }, query, searcher, topDocs, 3);
String[][] contents = loadFieldValues(searcher, fields, docids, maxLength); terms[termUpto++] = term.bytes(); Map<Integer,Object> fieldHighlights = highlightField(field, contents[i], getBreakIterator(field), terms, docids, leaves, numPassages, query);
/** * 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 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; }
/** Loads the String values for each field X docID to be * highlighted. By default this loads from stored * fields, but a subclass can change the source. This * method should allocate the String[fields.length][docids.length] * and fill all values. The returned Strings must be * identical to what was indexed. */ protected String[][] loadFieldValues(IndexSearcher searcher, String[] fields, int[] docids, int maxLength) throws IOException { String contents[][] = new String[fields.length][docids.length]; char valueSeparators[] = new char[fields.length]; for (int i = 0; i < fields.length; i++) { valueSeparators[i] = getMultiValuedSeparator(fields[i]); } LimitedStoredFieldVisitor visitor = new LimitedStoredFieldVisitor(fields, valueSeparators, maxLength); for (int i = 0; i < docids.length; i++) { searcher.doc(docids[i], visitor); for (int j = 0; j < fields.length; j++) { contents[j][i] = visitor.getValue(j).toString(); } visitor.reset(); } return contents; }
private Passage[] highlightDoc(String field, BytesRef terms[], int contentLength, BreakIterator bi, int doc, TermsEnum termsEnum, PostingsEnum[] postings, int n) throws IOException { PassageScorer scorer = getScorer(field); if (scorer == null) { throw new NullPointerException("PassageScorer cannot be null");
/** * 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 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; }
/** * Highlights the top-N 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. * @param maxPassages The maximum number of top-N ranked passages used to * form the highlighted snippets. * @return Array of formatted snippets corresponding to the documents in <code>topDocs</code>. * If no highlights were found for a document, the * first {@code maxPassages} sentences from 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, int maxPassages) throws IOException { Map<String,String[]> res = highlightFields(new String[] { field }, query, searcher, topDocs, new int[] { maxPassages }); return res.get(field); }
for(Map.Entry<String,Object[]> ent : highlightFieldsAsObjects(fieldsIn, query, searcher, docidsIn, maxPassagesIn).entrySet()) { Object[] snippetObjects = ent.getValue(); String[] snippetStrings = new String[snippetObjects.length];
@Override protected BreakIterator getBreakIterator(String field) { if (breakIterator == null) { return super.getBreakIterator(field); } return breakIterator; }
int maxPassages[] = new int[fields.length]; Arrays.fill(maxPassages, 1); return highlightFields(fields, query, searcher, topDocs, maxPassages);
Map<String, String[]> stringMap = postingsHighlighter.highlightFields(names.toArray(new String[names.size()]), query, searcher, new int[]{docID}, maxPassages); for (Map.Entry<String, String[]> entry : stringMap.entrySet()) {
Map<String, String[]> stringMap = postingsHighlighter.highlightFields(names.toArray(new String[names.size()]), query, searcher, new int[]{docID}, maxPassages); for (Map.Entry<String, String[]> entry : stringMap.entrySet()) {