@Override public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields) throws IOException { final SolrParams params = req.getParams(); // if highlighting isnt enabled, then why call doHighlighting? if (!isHighlightingEnabled(params)) return null; SolrIndexSearcher searcher = req.getSearcher(); int[] docIDs = toDocIDs(docs); // fetch the unique keys String[] keys = getUniqueKeys(searcher, docIDs); // query-time parameters String[] fieldNames = getHighlightFields(query, req, defaultFields); int maxPassages[] = new int[fieldNames.length]; for (int i = 0; i < fieldNames.length; i++) { maxPassages[i] = params.getFieldInt(fieldNames[i], HighlightParams.SNIPPETS, 1); } PostingsHighlighter highlighter = getHighlighter(req); Map<String,String[]> snippets = highlighter.highlightFields(fieldNames, query, searcher, docIDs, maxPassages); return encodeSnippets(keys, fieldNames, snippets); }
@Test public void testHighlightChapter5() throws IOException { // searching for "gas" didn't work on the Safari site InputStream ch5stream = getClass().getResourceAsStream("ch5.txt"); String ch5 = IOUtils.toString(ch5stream); // add a single document to the index // 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 text = new Field("text", ch5, offsetsType); Document doc = new Document(); doc.add(new StringField("id", "ch5", Store.YES)); doc.add(text); iw.addDocument(doc); iw.commit(); DirectoryReader reader = DirectoryReader.open(iw, true); IndexSearcher searcher = new IndexSearcher(reader); // retrieve highlights at query time PostingsHighlighter highlighter = new PostingsHighlighter(100000); Query query = new TermQuery(new Term("text", "gas")); TopDocs topDocs = searcher.search(query, 1); String highlights[] = highlighter.highlight("text", query, searcher, topDocs); assertEquals (1, highlights.length); assertNotNull ("PH returns null highlight", highlights[0]); assertTrue (highlights[0] + " \n does not contain <b>gas</b>", highlights[0].contains("<b>gas</b>")); }
@Override public void init(PluginInfo info) { Map<String,String> attributes = info.attributes; BreakIterator breakIterator = BreakIterator.getSentenceInstance(Locale.ROOT); PassageScorer scorer = new PassageScorer(); // formatter parameters: preTag/postTag/ellipsis String preTag = attributes.get("preTag"); if (preTag == null) { preTag = "<em>"; } String postTag = attributes.get("postTag"); if (postTag == null) { postTag = "</em>"; } String ellipsis = attributes.get("ellipsis"); if (ellipsis == null) { ellipsis = "... "; } PassageFormatter formatter = new PassageFormatter(preTag, postTag, ellipsis); // maximum content size to process int maxLength = PostingsHighlighter.DEFAULT_MAX_LENGTH; if (attributes.containsKey("maxLength")) { maxLength = Integer.parseInt(attributes.get("maxLength")); } highlighter = new PostingsHighlighter(maxLength, breakIterator, scorer, formatter); }
public LindenSnippetGenerator() { highlighter = new PostingsHighlighter(); }
/** Creates an instance of the Lucene PostingsHighlighter. Provided for subclass extension so that * a subclass can return a subclass of {@link PostingsSolrHighlighter.SolrExtendedPostingsHighlighter}. */ protected PostingsHighlighter getHighlighter(SolrQueryRequest req) { return new SolrExtendedPostingsHighlighter(req); }
public SolrExtendedPostingsHighlighter(SolrQueryRequest req) { super(req.getParams().getInt(HighlightParams.MAX_CHARS, PostingsHighlighter.DEFAULT_MAX_LENGTH)); this.params = req.getParams(); this.schema = req.getSchema(); }
/** Creates an instance of the Lucene PostingsHighlighter. Provided for subclass extension so that * a subclass can return a subclass of {@link PostingsSolrHighlighter.SolrExtendedPostingsHighlighter}. */ @Override protected PostingsHighlighter getHighlighter(SolrQueryRequest req) { return new SafariPostingsHighlighter(req); }