mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-13 05:43:26 +00:00
DATAES-412 - only the last highlight field is added to SearchRequest
This commit is contained in:
parent
ef1dca31e4
commit
9c235cd53c
@ -110,6 +110,7 @@ import org.springframework.util.Assert;
|
|||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
* @author Ilkang Na
|
* @author Ilkang Na
|
||||||
* @author Alen Turkovic
|
* @author Alen Turkovic
|
||||||
|
* @author Sascha Woo
|
||||||
*/
|
*/
|
||||||
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
|
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
|
||||||
|
|
||||||
@ -882,9 +883,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (searchQuery.getHighlightFields() != null) {
|
if (searchQuery.getHighlightFields() != null) {
|
||||||
|
HighlightBuilder highlightBuilder = new HighlightBuilder();
|
||||||
for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {
|
for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {
|
||||||
searchRequest.highlighter(new HighlightBuilder().field(highlightField));
|
highlightBuilder.field(highlightField);
|
||||||
}
|
}
|
||||||
|
searchRequest.highlighter(highlightBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isEmpty(searchQuery.getIndicesBoost())) {
|
if (!isEmpty(searchQuery.getIndicesBoost())) {
|
||||||
|
@ -34,6 +34,7 @@ import org.elasticsearch.script.Script;
|
|||||||
import org.elasticsearch.script.ScriptType;
|
import org.elasticsearch.script.ScriptType;
|
||||||
import org.elasticsearch.search.SearchHit;
|
import org.elasticsearch.search.SearchHit;
|
||||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||||
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
||||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
@ -74,6 +75,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
|
|||||||
* @author Chris White
|
* @author Chris White
|
||||||
* @author Ilkang Na
|
* @author Ilkang Na
|
||||||
* @author Alen Turkovic
|
* @author Alen Turkovic
|
||||||
|
* @author Sascha Woo
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||||
@ -1237,6 +1239,58 @@ public class ElasticsearchTemplateTests {
|
|||||||
assertThat(sampleEntities.getContent().get(0).getHighlightedMessage(), is(highlightedMessage));
|
assertThat(sampleEntities.getContent().get(0).getHighlightedMessage(), is(highlightedMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // DATAES-412
|
||||||
|
public void shouldReturnMultipleHighlightFields() {
|
||||||
|
|
||||||
|
// given
|
||||||
|
String documentId = randomNumeric(5);
|
||||||
|
String actualType = "some test type";
|
||||||
|
String actualMessage = "some test message";
|
||||||
|
String highlightedType = "some <em>test</em> type";
|
||||||
|
String highlightedMessage = "some <em>test</em> message";
|
||||||
|
|
||||||
|
SampleEntity sampleEntity = SampleEntity.builder()
|
||||||
|
.id(documentId)
|
||||||
|
.type(actualType)
|
||||||
|
.message(actualMessage)
|
||||||
|
.version(System.currentTimeMillis())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
IndexQuery indexQuery = getIndexQuery(sampleEntity);
|
||||||
|
|
||||||
|
elasticsearchTemplate.index(indexQuery);
|
||||||
|
elasticsearchTemplate.refresh(SampleEntity.class);
|
||||||
|
|
||||||
|
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(
|
||||||
|
boolQuery()
|
||||||
|
.must(termQuery("type", "test"))
|
||||||
|
.must(termQuery("message", "test")))
|
||||||
|
.withHighlightFields(
|
||||||
|
new HighlightBuilder.Field("type"),
|
||||||
|
new HighlightBuilder.Field("message"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// when
|
||||||
|
elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, new SearchResultMapper() {
|
||||||
|
@Override
|
||||||
|
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||||
|
for (SearchHit searchHit : response.getHits()) {
|
||||||
|
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
|
||||||
|
HighlightField highlightFieldType = highlightFields.get("type");
|
||||||
|
HighlightField highlightFieldMessage = highlightFields.get("message");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertNotNull(highlightFieldType);
|
||||||
|
assertNotNull(highlightFieldMessage);
|
||||||
|
assertThat(highlightFieldType.fragments()[0].toString(), is(highlightedType));
|
||||||
|
assertThat(highlightFieldMessage.fragments()[0].toString(), is(highlightedMessage));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldDeleteDocumentBySpecifiedTypeUsingDeleteQuery() {
|
public void shouldDeleteDocumentBySpecifiedTypeUsingDeleteQuery() {
|
||||||
// given
|
// given
|
||||||
|
Loading…
x
Reference in New Issue
Block a user