Search in sources :

Example 1 with NestedMetaData

use of org.springframework.data.elasticsearch.core.document.NestedMetaData in project spring-data-elasticsearch by spring-projects.

the class SearchHitMapping method mapInnerDocuments.

/**
 * try to convert the SearchDocument instances to instances of the inner property class.
 *
 * @param searchHits {@link SearchHits} containing {@link Document} instances
 * @param type the class of the containing class
 * @return a new {@link SearchHits} instance containing the mapped objects or the original inout if any error occurs
 */
private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, Class<T> type) {
    if (searchHits.isEmpty()) {
        return searchHits;
    }
    try {
        NestedMetaData nestedMetaData = searchHits.getSearchHit(0).getContent().getNestedMetaData();
        ElasticsearchPersistentEntityWithNestedMetaData persistentEntityWithNestedMetaData = getPersistentEntity(mappingContext.getPersistentEntity(type), nestedMetaData);
        if (persistentEntityWithNestedMetaData.entity != null) {
            List<SearchHit<Object>> convertedSearchHits = new ArrayList<>();
            Class<?> targetType = persistentEntityWithNestedMetaData.entity.getType();
            // convert the list of SearchHit<SearchDocument> to list of SearchHit<Object>
            searchHits.getSearchHits().forEach(searchHit -> {
                SearchDocument searchDocument = searchHit.getContent();
                Object targetObject = converter.read(targetType, searchDocument);
                convertedSearchHits.add(new // 
                SearchHit<Object>(// 
                searchDocument.getIndex(), // 
                searchDocument.getId(), // 
                searchDocument.getRouting(), // 
                searchDocument.getScore(), // 
                searchDocument.getSortValues(), // 
                searchDocument.getHighlightFields(), // 
                searchHit.getInnerHits(), // 
                persistentEntityWithNestedMetaData.nestedMetaData, // 
                searchHit.getExplanation(), // 
                searchHit.getMatchedQueries(), targetObject));
            });
            String scrollId = null;
            if (searchHits instanceof SearchHitsImpl) {
                scrollId = ((SearchHitsImpl<?>) searchHits).getScrollId();
            }
            return new // 
            SearchHitsImpl<>(// 
            searchHits.getTotalHits(), // 
            searchHits.getTotalHitsRelation(), // 
            searchHits.getMaxScore(), // 
            scrollId, // 
            convertedSearchHits, // 
            searchHits.getAggregations(), searchHits.getSuggest());
        }
    } catch (Exception e) {
        throw new UncategorizedElasticsearchException("Unable to convert inner hits.", e);
    }
    return searchHits;
}
Also used : NestedMetaData(org.springframework.data.elasticsearch.core.document.NestedMetaData) ArrayList(java.util.ArrayList) UncategorizedElasticsearchException(org.springframework.data.elasticsearch.UncategorizedElasticsearchException) UncategorizedElasticsearchException(org.springframework.data.elasticsearch.UncategorizedElasticsearchException) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument)

Example 2 with NestedMetaData

use of org.springframework.data.elasticsearch.core.document.NestedMetaData in project spring-data-elasticsearch by spring-projects.

the class InnerHitsIntegrationTests method shouldReturnInnerHits.

@Test
void shouldReturnInnerHits() {
    String innerHitName = "inner_hit_name";
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    NestedQueryBuilder nestedQueryBuilder = nestedQuery("hou-ses.in-habi-tants", matchQuery("hou-ses.in-habi-tants.first-name", "Carla"), ScoreMode.Avg);
    nestedQueryBuilder.innerHit(new InnerHitBuilder(innerHitName));
    queryBuilder.withQuery(nestedQueryBuilder);
    NativeSearchQuery query = queryBuilder.build();
    SoftAssertions softly = new SoftAssertions();
    SearchHits<City> searchHits = operations.search(query, City.class);
    softly.assertThat(searchHits.getTotalHits()).isEqualTo(1);
    SearchHit<City> searchHit = searchHits.getSearchHit(0);
    softly.assertThat(searchHit.getInnerHits()).hasSize(1);
    SearchHits<?> innerHits = searchHit.getInnerHits(innerHitName);
    softly.assertThat(innerHits).hasSize(1);
    SearchHit<?> innerHit = innerHits.getSearchHit(0);
    Object content = innerHit.getContent();
    assertThat(content).isInstanceOf(Inhabitant.class);
    Inhabitant inhabitant = (Inhabitant) content;
    softly.assertThat(inhabitant.getFirstName()).isEqualTo("Carla");
    softly.assertThat(inhabitant.getLastName()).isEqualTo("Miller");
    NestedMetaData nestedMetaData = innerHit.getNestedMetaData();
    softly.assertThat(nestedMetaData.getField()).isEqualTo("houses");
    softly.assertThat(nestedMetaData.getOffset()).isEqualTo(0);
    softly.assertThat(nestedMetaData.getChild().getField()).isEqualTo("inhabitants");
    softly.assertThat(nestedMetaData.getChild().getOffset()).isEqualTo(1);
    softly.assertAll();
}
Also used : SoftAssertions(org.assertj.core.api.SoftAssertions) NestedMetaData(org.springframework.data.elasticsearch.core.document.NestedMetaData) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) NestedQueryBuilder(org.elasticsearch.index.query.NestedQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 3 with NestedMetaData

use of org.springframework.data.elasticsearch.core.document.NestedMetaData in project spring-data-elasticsearch by spring-projects.

the class SearchHitMapping method getPersistentEntity.

/**
 * find a {@link ElasticsearchPersistentEntity} following the property chain defined by the nested metadata
 *
 * @param persistentEntity base entity
 * @param nestedMetaData nested metadata
 * @return A {@link ElasticsearchPersistentEntityWithNestedMetaData} containing the found entity or null together with
 *         the {@link NestedMetaData} that has mapped field names.
 */
private ElasticsearchPersistentEntityWithNestedMetaData getPersistentEntity(@Nullable ElasticsearchPersistentEntity<?> persistentEntity, @Nullable NestedMetaData nestedMetaData) {
    NestedMetaData currentMetaData = nestedMetaData;
    List<NestedMetaData> mappedNestedMetaDatas = new LinkedList<>();
    while (persistentEntity != null && currentMetaData != null) {
        ElasticsearchPersistentProperty persistentProperty = persistentEntity.getPersistentPropertyWithFieldName(currentMetaData.getField());
        if (persistentProperty == null) {
            persistentEntity = null;
        } else {
            persistentEntity = mappingContext.getPersistentEntity(persistentProperty.getActualType());
            mappedNestedMetaDatas.add(0, NestedMetaData.of(persistentProperty.getName(), currentMetaData.getOffset(), null));
            currentMetaData = currentMetaData.getChild();
        }
    }
    NestedMetaData mappedNestedMetaData = mappedNestedMetaDatas.stream().reduce(null, (result, nmd) -> NestedMetaData.of(nmd.getField(), nmd.getOffset(), result));
    return new ElasticsearchPersistentEntityWithNestedMetaData(persistentEntity, mappedNestedMetaData);
}
Also used : NestedMetaData(org.springframework.data.elasticsearch.core.document.NestedMetaData) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty) LinkedList(java.util.LinkedList)

Aggregations

NestedMetaData (org.springframework.data.elasticsearch.core.document.NestedMetaData)3 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 SoftAssertions (org.assertj.core.api.SoftAssertions)1 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)1 NestedQueryBuilder (org.elasticsearch.index.query.NestedQueryBuilder)1 Test (org.junit.jupiter.api.Test)1 UncategorizedElasticsearchException (org.springframework.data.elasticsearch.UncategorizedElasticsearchException)1 SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)1 ElasticsearchPersistentProperty (org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)1 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)1 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)1 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)1