|
34 | 34 | import org.elasticsearch.script.ScriptType;
|
35 | 35 | import org.elasticsearch.search.SearchHit;
|
36 | 36 | import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
| 37 | +import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; |
37 | 38 | import org.elasticsearch.search.sort.FieldSortBuilder;
|
38 | 39 | import org.elasticsearch.search.sort.SortOrder;
|
39 | 40 | import org.hamcrest.Matchers;
|
|
74 | 75 | * @author Chris White
|
75 | 76 | * @author Ilkang Na
|
76 | 77 | * @author Alen Turkovic
|
| 78 | + * @author Sascha Woo |
77 | 79 | */
|
78 | 80 | @RunWith(SpringJUnit4ClassRunner.class)
|
79 | 81 | @ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
@@ -1237,6 +1239,58 @@ public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz,
|
1237 | 1239 | assertThat(sampleEntities.getContent().get(0).getHighlightedMessage(), is(highlightedMessage));
|
1238 | 1240 | }
|
1239 | 1241 |
|
| 1242 | + @Test // DATAES-412 |
| 1243 | + public void shouldReturnMultipleHighlightFields() { |
| 1244 | + |
| 1245 | + // given |
| 1246 | + String documentId = randomNumeric(5); |
| 1247 | + String actualType = "some test type"; |
| 1248 | + String actualMessage = "some test message"; |
| 1249 | + String highlightedType = "some <em>test</em> type"; |
| 1250 | + String highlightedMessage = "some <em>test</em> message"; |
| 1251 | + |
| 1252 | + SampleEntity sampleEntity = SampleEntity.builder() |
| 1253 | + .id(documentId) |
| 1254 | + .type(actualType) |
| 1255 | + .message(actualMessage) |
| 1256 | + .version(System.currentTimeMillis()) |
| 1257 | + .build(); |
| 1258 | + |
| 1259 | + IndexQuery indexQuery = getIndexQuery(sampleEntity); |
| 1260 | + |
| 1261 | + elasticsearchTemplate.index(indexQuery); |
| 1262 | + elasticsearchTemplate.refresh(SampleEntity.class); |
| 1263 | + |
| 1264 | + SearchQuery searchQuery = new NativeSearchQueryBuilder() |
| 1265 | + .withQuery( |
| 1266 | + boolQuery() |
| 1267 | + .must(termQuery("type", "test")) |
| 1268 | + .must(termQuery("message", "test"))) |
| 1269 | + .withHighlightFields( |
| 1270 | + new HighlightBuilder.Field("type"), |
| 1271 | + new HighlightBuilder.Field("message")) |
| 1272 | + .build(); |
| 1273 | + |
| 1274 | + // when |
| 1275 | + elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, new SearchResultMapper() { |
| 1276 | + @Override |
| 1277 | + public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { |
| 1278 | + for (SearchHit searchHit : response.getHits()) { |
| 1279 | + Map<String, HighlightField> highlightFields = searchHit.getHighlightFields(); |
| 1280 | + HighlightField highlightFieldType = highlightFields.get("type"); |
| 1281 | + HighlightField highlightFieldMessage = highlightFields.get("message"); |
| 1282 | + |
| 1283 | + // then |
| 1284 | + assertNotNull(highlightFieldType); |
| 1285 | + assertNotNull(highlightFieldMessage); |
| 1286 | + assertThat(highlightFieldType.fragments()[0].toString(), is(highlightedType)); |
| 1287 | + assertThat(highlightFieldMessage.fragments()[0].toString(), is(highlightedMessage)); |
| 1288 | + } |
| 1289 | + return null; |
| 1290 | + } |
| 1291 | + }); |
| 1292 | + } |
| 1293 | + |
1240 | 1294 | @Test
|
1241 | 1295 | public void shouldDeleteDocumentBySpecifiedTypeUsingDeleteQuery() {
|
1242 | 1296 | // given
|
|
0 commit comments