Skip to content

Commit 6c4d101

Browse files
JKatzwinkelsothawo
authored andcommitted
Fix inner hits metadata mapping.
Original Pull Request #2522 Closes #2521 (cherry picked from commit 1f7fa77) (cherry picked from commit e1da43c)
1 parent 48d7779 commit 6c4d101

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> {
@@ -152,7 +153,8 @@ private Map<String, List<String>> getHighlightsAndRemapFieldNames(SearchDocument
152153
}
153154

154155
return highlightFields.entrySet().stream().collect(Collectors.toMap(entry -> {
155-
ElasticsearchPersistentProperty property = persistentEntity.getPersistentPropertyWithFieldName(entry.getKey());
156+
ElasticsearchPersistentProperty property = persistentEntity
157+
.getPersistentPropertyWithFieldName(entry.getKey());
156158
return property != null ? property.getName() : entry.getKey();
157159
}, Map.Entry::getValue));
158160
}
@@ -197,9 +199,10 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
197199
}
198200

199201
try {
202+
ElasticsearchPersistentEntity<?> persistentEntityForType = mappingContext.getPersistentEntity(type);
200203
NestedMetaData nestedMetaData = searchHits.getSearchHit(0).getContent().getNestedMetaData();
201204
ElasticsearchPersistentEntityWithNestedMetaData persistentEntityWithNestedMetaData = getPersistentEntity(
202-
mappingContext.getPersistentEntity(type), nestedMetaData);
205+
persistentEntityForType, nestedMetaData);
203206

204207
if (persistentEntityWithNestedMetaData.entity != null) {
205208
List<SearchHit<Object>> convertedSearchHits = new ArrayList<>();
@@ -217,7 +220,8 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
217220
searchDocument.getSortValues(), //
218221
searchDocument.getHighlightFields(), //
219222
searchHit.getInnerHits(), //
220-
persistentEntityWithNestedMetaData.nestedMetaData, //
223+
getPersistentEntity(persistentEntityForType, //
224+
searchHit.getContent().getNestedMetaData()).nestedMetaData, //
221225
searchHit.getExplanation(), //
222226
searchHit.getMatchedQueries(), //
223227
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)