Source:
public void testHighlight() throws Exception { startTransaction(); DocumentModel doc = session.createDocumentModel("/", "highlight", "File"); doc.setPropertyValue("dc:title", "Search me"); BlobHolder holder = doc.getAdapter(BlobHolder.class); holder.setBlob(new StringBlob("you know for search")); session.createDocument(doc); session.save(); DocumentModel doc2 = session.createDocumentModel("/", "highlight2", "File"); BlobHolder holder2 = doc2.getAdapter(BlobHolder.class); holder2.setBlob(new StringBlob("test my search with highlight")); session.createDocument(doc2); session.save(); TransactionHelper.commitOrRollbackTransaction(); waitForCompletion(); startTransaction(); SearchRequestBuilder request = esa.getClient().prepareSearch("nxutest").setTypes("doc"); List<String> highlightFields = new ArrayList<>(); highlightFields.add("dc:title"); highlightFields.add("ecm:binarytext"); NxQueryBuilder queryBuilder = new NxQueryBuilder(session) .nxql("SELECT * FROM Document WHERE ecm:fulltext='search'") .highlight(highlightFields); queryBuilder.updateRequest(request); DocumentModelList ret = ess.query(queryBuilder); assertEquals(2, ret.totalSize()); Map<String, List<String>> highlights = (Map<String, List<String>>) ret.get(0).getContextData("highlight"); assertEquals(2, highlights.size()); assertTrue(highlights.containsKey("dc:title")); assertTrue(highlights.containsKey("ecm:binarytext")); assertEquals("<em>Search</em> me", highlights.get("dc:title").get(0)); assertEquals("you know for <em>search</em>", highlights.get("ecm:binarytext").get(0)); Map<String, List<String>> highlights2 = (Map<String, List<String>>) ret.get(1).getContextData("highlight"); assertEquals("test my <em>search</em> with highlight", highlights2.get("ecm:binarytext").get(0)); }

Target:
public void testHighlight() throws Exception { DocumentModel doc = session.createDocumentModel("/", "highlight", "File"); doc.setPropertyValue("dc:title", "Search me"); BlobHolder holder = doc.getAdapter(BlobHolder.class); holder.setBlob(new StringBlob("you know for search")); session.createDocument(doc); session.save(); DocumentModel doc2 = session.createDocumentModel("/", "highlight2", "File"); BlobHolder holder2 = doc2.getAdapter(BlobHolder.class); holder2.setBlob(new StringBlob("test my search with highlight")); session.createDocument(doc2); session.save(); TransactionHelper.commitOrRollbackTransaction(); waitForCompletion(); TransactionHelper.startTransaction(); SearchRequestBuilder request = esa.getClient().prepareSearch("nxutest").setTypes("doc"); List<String> highlightFields = Arrays.asList("dc:title", "ecm:binarytext"); NxQueryBuilder queryBuilder = new NxQueryBuilder(session) .nxql("SELECT * FROM Document WHERE ecm:fulltext='search'") .highlight(highlightFields); queryBuilder.updateRequest(request); DocumentModelList ret = ess.query(queryBuilder); assertEquals(2, ret.totalSize()); Map<String, List<String>> highlights = (Map<String, List<String>>) ret.get(0).getContextData(HIGHLIGHT_CTX_DATA); assertEquals(2, highlights.size()); assertTrue(highlights.containsKey("dc:title")); assertTrue(highlights.containsKey("ecm:binarytext")); assertEquals("<em>Search</em> me", highlights.get("dc:title").get(0)); assertEquals("you know for <em>search</em>", highlights.get("ecm:binarytext").get(0)); Map<String, List<String>> highlights2 = (Map<String, List<String>>) ret.get(1).getContextData(HIGHLIGHT_CTX_DATA); assertEquals("test my <em>search</em> with highlight", highlights2.get("ecm:binarytext").get(0)); }