Skip to content

Commit e1129e1

Browse files
committed
Fix inner hits metadata mapping.
Closes spring-projects#2521
1 parent 9771b9c commit e1129e1

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* @author Roman Puchkovskiy
4646
* @author Matt Gilene
4747
* @author Sascha Woo
48+
* @author Jakob Hoeper
4849
* @since 4.0
4950
*/
5051
public class SearchHitMapping<T> {
@@ -154,7 +155,8 @@ private Map<String, List<String>> getHighlightsAndRemapFieldNames(SearchDocument
154155
}
155156

156157
return highlightFields.entrySet().stream().collect(Collectors.toMap(entry -> {
157-
ElasticsearchPersistentProperty property = persistentEntity.getPersistentPropertyWithFieldName(entry.getKey());
158+
ElasticsearchPersistentProperty property = persistentEntity
159+
.getPersistentPropertyWithFieldName(entry.getKey());
158160
return property != null ? property.getName() : entry.getKey();
159161
}, Map.Entry::getValue));
160162
}
@@ -199,9 +201,10 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
199201
}
200202

201203
try {
204+
ElasticsearchPersistentEntity<?> persistentEntityForType = mappingContext.getPersistentEntity(type);
202205
NestedMetaData nestedMetaData = searchHits.getSearchHit(0).getContent().getNestedMetaData();
203206
ElasticsearchPersistentEntityWithNestedMetaData persistentEntityWithNestedMetaData = getPersistentEntity(
204-
mappingContext.getPersistentEntity(type), nestedMetaData);
207+
persistentEntityForType, nestedMetaData);
205208

206209
if (persistentEntityWithNestedMetaData.entity != null) {
207210
List<SearchHit<Object>> convertedSearchHits = new ArrayList<>();
@@ -219,7 +222,8 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
219222
searchDocument.getSortValues(), //
220223
searchDocument.getHighlightFields(), //
221224
searchHit.getInnerHits(), //
222-
persistentEntityWithNestedMetaData.nestedMetaData, //
225+
getPersistentEntity(persistentEntityForType, //
226+
searchHit.getContent().getNestedMetaData()).nestedMetaData, //
223227
searchHit.getExplanation(), //
224228
searchHit.getMatchedQueries(), //
225229
targetObject));

src/test/java/org/springframework/data/elasticsearch/core/InnerHitsIntegrationTests.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* Testing the querying and parsing of inner_hits.
4343
*
4444
* @author Peter-Josef Meisch
45+
* @author Jakob Hoeper
4546
*/
4647
@SpringIntegrationTest
4748
public abstract class InnerHitsIntegrationTests {
@@ -58,8 +59,9 @@ void setUp() {
5859
indexOps.createWithMapping();
5960

6061
Inhabitant john = new Inhabitant("John", "Smith");
61-
Inhabitant carla = new Inhabitant("Carla", "Miller");
62-
House cornerHouse = new House("Round the corner", "7", Arrays.asList(john, carla));
62+
Inhabitant carla1 = new Inhabitant("Carla", "Miller");
63+
Inhabitant carla2 = new Inhabitant("Carla", "Nguyen");
64+
House cornerHouse = new House("Round the corner", "7", Arrays.asList(john, carla1, carla2));
6365
City metropole = new City("Metropole", Arrays.asList(cornerHouse));
6466

6567
Inhabitant jack = new Inhabitant("Jack", "Wayne");
@@ -76,7 +78,7 @@ void cleanup() {
7678
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
7779
}
7880

79-
@Test
81+
@Test // #2521
8082
void shouldReturnInnerHits() {
8183

8284
Query query = buildQueryForInnerHits("inner_hit_name", "hou-ses.in-habi-tants", "hou-ses.in-habi-tants.first-name",
@@ -91,7 +93,7 @@ void shouldReturnInnerHits() {
9193
softly.assertThat(searchHit.getInnerHits()).hasSize(1);
9294

9395
SearchHits<?> innerHits = searchHit.getInnerHits("inner_hit_name");
94-
softly.assertThat(innerHits).hasSize(1);
96+
softly.assertThat(innerHits).hasSize(2);
9597

9698
SearchHit<?> innerHit = innerHits.getSearchHit(0);
9799
Object content = innerHit.getContent();
@@ -106,6 +108,10 @@ void shouldReturnInnerHits() {
106108
softly.assertThat(nestedMetaData.getChild().getField()).isEqualTo("inhabitants");
107109
softly.assertThat(nestedMetaData.getChild().getOffset()).isEqualTo(1);
108110

111+
innerHit = innerHits.getSearchHit(1);
112+
softly.assertThat(((Inhabitant) innerHit.getContent()).getLastName()).isEqualTo("Nguyen");
113+
softly.assertThat(innerHit.getNestedMetaData().getChild().getOffset()).isEqualTo(2);
114+
109115
softly.assertAll();
110116
}
111117

0 commit comments

Comments
 (0)