|
25 | 25 | import java.util.LinkedList;
|
26 | 26 | import java.util.List;
|
27 | 27 |
|
| 28 | +import com.fasterxml.jackson.core.JsonEncoding; |
| 29 | +import com.fasterxml.jackson.core.JsonFactory; |
| 30 | +import com.fasterxml.jackson.core.JsonGenerator; |
28 | 31 | import org.apache.commons.lang.StringUtils;
|
29 | 32 | import org.elasticsearch.action.get.GetResponse;
|
30 | 33 | import org.elasticsearch.action.get.MultiGetItemResponse;
|
31 | 34 | import org.elasticsearch.action.get.MultiGetResponse;
|
32 | 35 | import org.elasticsearch.action.search.SearchResponse;
|
33 |
| -import com.fasterxml.jackson.core.JsonEncoding; |
34 |
| -import com.fasterxml.jackson.core.JsonFactory; |
35 |
| -import com.fasterxml.jackson.core.JsonGenerator; |
36 | 36 | import org.elasticsearch.search.SearchHit;
|
37 | 37 | import org.elasticsearch.search.SearchHitField;
|
38 | 38 | import org.springframework.data.domain.Page;
|
39 |
| -import org.springframework.data.domain.PageImpl; |
40 | 39 | import org.springframework.data.domain.Pageable;
|
41 | 40 | import org.springframework.data.elasticsearch.ElasticsearchException;
|
42 | 41 | import org.springframework.data.elasticsearch.annotations.Document;
|
43 | 42 | import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
| 43 | +import org.springframework.data.elasticsearch.core.domain.impl.AggregatedPageImpl; |
44 | 44 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
45 | 45 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
46 | 46 | import org.springframework.data.mapping.PersistentProperty;
|
47 | 47 | import org.springframework.data.mapping.context.MappingContext;
|
48 | 48 |
|
49 | 49 | /**
|
50 | 50 | * @author Artur Konczak
|
| 51 | + * @author Petar Tahchiev |
51 | 52 | */
|
52 | 53 | public class DefaultResultMapper extends AbstractResultMapper {
|
53 | 54 |
|
@@ -86,38 +87,39 @@ public <T> Page<T> mapResults(SearchResponse response, Class<T> clazz, Pageable
|
86 | 87 | result = mapEntity(hit.getFields().values(), clazz);
|
87 | 88 | }
|
88 | 89 | setPersistentEntityId(result, hit.getId(), clazz);
|
89 |
| - populateScriptFields(result, hit); |
| 90 | + populateScriptFields(result, hit); |
90 | 91 | results.add(result);
|
91 | 92 | }
|
92 | 93 | }
|
93 |
| - return new PageImpl<T>(results, pageable, totalHits); |
| 94 | + |
| 95 | + return new AggregatedPageImpl<T>(results, pageable, totalHits, response.getAggregations()); |
| 96 | + } |
| 97 | + |
| 98 | + private <T> void populateScriptFields(T result, SearchHit hit) { |
| 99 | + if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) { |
| 100 | + for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { |
| 101 | + ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); |
| 102 | + if (scriptedField != null) { |
| 103 | + String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); |
| 104 | + SearchHitField searchHitField = hit.getFields().get(name); |
| 105 | + if (searchHitField != null) { |
| 106 | + field.setAccessible(true); |
| 107 | + try { |
| 108 | + field.set(result, searchHitField.getValue()); |
| 109 | + } catch (IllegalArgumentException e) { |
| 110 | + throw new ElasticsearchException("failed to set scripted field: " + name + " with value: " |
| 111 | + + searchHitField.getValue(), e); |
| 112 | + } catch (IllegalAccessException e) { |
| 113 | + throw new ElasticsearchException("failed to access scripted field: " + name, e); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
94 | 119 | }
|
95 | 120 |
|
96 |
| - private <T> void populateScriptFields(T result, SearchHit hit) { |
97 |
| - if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) { |
98 |
| - for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { |
99 |
| - ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); |
100 |
| - if (scriptedField != null) { |
101 |
| - String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); |
102 |
| - SearchHitField searchHitField = hit.getFields().get(name); |
103 |
| - if (searchHitField != null) { |
104 |
| - field.setAccessible(true); |
105 |
| - try { |
106 |
| - field.set(result, searchHitField.getValue()); |
107 |
| - } catch (IllegalArgumentException e) { |
108 |
| - throw new ElasticsearchException("failed to set scripted field: " + name + " with value: " |
109 |
| - + searchHitField.getValue(), e); |
110 |
| - } catch (IllegalAccessException e) { |
111 |
| - throw new ElasticsearchException("failed to access scripted field: " + name, e); |
112 |
| - } |
113 |
| - } |
114 |
| - } |
115 |
| - } |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - |
120 |
| - private <T> T mapEntity(Collection<SearchHitField> values, Class<T> clazz) { |
| 121 | + |
| 122 | + private <T> T mapEntity(Collection<SearchHitField> values, Class<T> clazz) { |
121 | 123 | return mapEntity(buildJSONFromFields(values), clazz);
|
122 | 124 | }
|
123 | 125 |
|
|
0 commit comments