GetSearchHitTask implements Callable<SearchHit> { @Override public SearchHit call() throws IOException { Formatter formatter = new SimpleHTMLFormatter("<strong>", "</strong>"); Scorer scorer = new QueryScorer(query); Highlighter highlighter = new Highlighter(formatter, scorer); highlighter.setTextFragmenter(new SimpleFragmenter(FRAGMENT_SIZE)); highlighter.setEncoder(new SimpleHTMLEncoder()); Document doc = reader.document(docId); String projectName = doc.get(PageIndex.PROJECT); String branchName = doc.get(PageIndex.BRANCH); String path = doc.get(PageIndex.PATH); String title = doc.get(PageIndex.TITLE); String text = doc.get(PageIndex.TEXT); String[] tagsArray = doc.getValues(PageIndex.TAG); List<String> tags = Lists.newArrayList(tagsArray); Collections.sort(tags); TokenStream tokenStream = null; String highlightedText = StringUtils.EMPTY; try { tokenStream = TokenSources.getAnyTokenStream(reader, docId, PageIndex.TEXT, doc, analyzer); String[] fragments = highlighter.getBestFragments(tokenStream, text, NUM_FRAGMENTS); cleanupFragments(fragments); highlightedText = Util.join(fragments, " <strong>...</strong> "); } catch (InvalidTokenOffsetsException e) { } finally { Util.closeQuietly(tokenStream); } return new SearchHit(projectName, branchName, path, title, highlightedText, tags); } GetSearchHitTask(Query query, IndexReader reader, int docId, Analyzer analyzer); @Override SearchHit call();  }
@Test public void call() throws IOException { SearchHit hit = task.call(); assertEquals("project", hit.getProjectName()); assertEquals("branch", hit.getBranchName()); assertEquals("home", hit.getPath()); assertEquals("title", hit.getTitle()); assertEquals("<strong>some</strong> text", hit.getTextHtml()); assertEquals(Lists.newArrayList("tag1", "tag2"), hit.getTags()); }