Fix inner hits metadata mapping.

Original Pull Request #2522
Closes #2521
This commit is contained in:
JKatzwinkel 2023-04-10 11:18:26 +02:00 committed by GitHub
parent 699c0ef265
commit 1f7fa77c15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View File

@ -45,6 +45,7 @@ import org.springframework.util.Assert;
* @author Roman Puchkovskiy
* @author Matt Gilene
* @author Sascha Woo
* @author Jakob Hoeper
* @since 4.0
*/
public class SearchHitMapping<T> {
@ -154,7 +155,8 @@ public class SearchHitMapping<T> {
}
return highlightFields.entrySet().stream().collect(Collectors.toMap(entry -> {
ElasticsearchPersistentProperty property = persistentEntity.getPersistentPropertyWithFieldName(entry.getKey());
ElasticsearchPersistentProperty property = persistentEntity
.getPersistentPropertyWithFieldName(entry.getKey());
return property != null ? property.getName() : entry.getKey();
}, Map.Entry::getValue));
}
@ -199,9 +201,10 @@ public class SearchHitMapping<T> {
}
try {
ElasticsearchPersistentEntity<?> persistentEntityForType = mappingContext.getPersistentEntity(type);
NestedMetaData nestedMetaData = searchHits.getSearchHit(0).getContent().getNestedMetaData();
ElasticsearchPersistentEntityWithNestedMetaData persistentEntityWithNestedMetaData = getPersistentEntity(
mappingContext.getPersistentEntity(type), nestedMetaData);
persistentEntityForType, nestedMetaData);
if (persistentEntityWithNestedMetaData.entity != null) {
List<SearchHit<Object>> convertedSearchHits = new ArrayList<>();
@ -219,7 +222,8 @@ public class SearchHitMapping<T> {
searchDocument.getSortValues(), //
searchDocument.getHighlightFields(), //
searchHit.getInnerHits(), //
persistentEntityWithNestedMetaData.nestedMetaData, //
getPersistentEntity(persistentEntityForType, //
searchHit.getContent().getNestedMetaData()).nestedMetaData, //
searchHit.getExplanation(), //
searchHit.getMatchedQueries(), //
targetObject));

View File

@ -42,6 +42,7 @@ import org.springframework.lang.Nullable;
* Testing the querying and parsing of inner_hits.
*
* @author Peter-Josef Meisch
* @author Jakob Hoeper
*/
@SpringIntegrationTest
public abstract class InnerHitsIntegrationTests {
@ -58,8 +59,9 @@ public abstract class InnerHitsIntegrationTests {
indexOps.createWithMapping();
Inhabitant john = new Inhabitant("John", "Smith");
Inhabitant carla = new Inhabitant("Carla", "Miller");
House cornerHouse = new House("Round the corner", "7", Arrays.asList(john, carla));
Inhabitant carla1 = new Inhabitant("Carla", "Miller");
Inhabitant carla2 = new Inhabitant("Carla", "Nguyen");
House cornerHouse = new House("Round the corner", "7", Arrays.asList(john, carla1, carla2));
City metropole = new City("Metropole", Arrays.asList(cornerHouse));
Inhabitant jack = new Inhabitant("Jack", "Wayne");
@ -76,7 +78,7 @@ public abstract class InnerHitsIntegrationTests {
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
}
@Test
@Test // #2521
void shouldReturnInnerHits() {
Query query = buildQueryForInnerHits("inner_hit_name", "hou-ses.in-habi-tants", "hou-ses.in-habi-tants.first-name",
@ -91,7 +93,7 @@ public abstract class InnerHitsIntegrationTests {
softly.assertThat(searchHit.getInnerHits()).hasSize(1);
SearchHits<?> innerHits = searchHit.getInnerHits("inner_hit_name");
softly.assertThat(innerHits).hasSize(1);
softly.assertThat(innerHits).hasSize(2);
SearchHit<?> innerHit = innerHits.getSearchHit(0);
Object content = innerHit.getContent();
@ -106,6 +108,10 @@ public abstract class InnerHitsIntegrationTests {
softly.assertThat(nestedMetaData.getChild().getField()).isEqualTo("inhabitants");
softly.assertThat(nestedMetaData.getChild().getOffset()).isEqualTo(1);
innerHit = innerHits.getSearchHit(1);
softly.assertThat(((Inhabitant) innerHit.getContent()).getLastName()).isEqualTo("Nguyen");
softly.assertThat(innerHit.getNestedMetaData().getChild().getOffset()).isEqualTo(2);
softly.assertAll();
}