Search in sources :

Example 1 with UncategorizedElasticsearchException

use of org.springframework.data.elasticsearch.UncategorizedElasticsearchException 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 UncategorizedElasticsearchException

use of org.springframework.data.elasticsearch.UncategorizedElasticsearchException in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchExceptionTranslator method translateExceptionIfPossible.

@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
    if (isSeqNoConflict(ex)) {
        return new OptimisticLockingFailureException("Cannot index a document due to seq_no+primary_term conflict", ex);
    }
    if (ex instanceof ElasticsearchException) {
        ElasticsearchException elasticsearchException = (ElasticsearchException) ex;
        if (!indexAvailable(elasticsearchException)) {
            return new NoSuchIndexException(ObjectUtils.nullSafeToString(elasticsearchException.getMetadata("es.index")), ex);
        }
        if (elasticsearchException instanceof ElasticsearchStatusException) {
            ElasticsearchStatusException elasticsearchStatusException = (ElasticsearchStatusException) elasticsearchException;
            return new RestStatusException(elasticsearchStatusException.status().getStatus(), elasticsearchStatusException.getMessage(), elasticsearchStatusException);
        }
        return new UncategorizedElasticsearchException(ex.getMessage(), ex);
    }
    if (ex instanceof RestStatusException) {
        RestStatusException restStatusException = (RestStatusException) ex;
        Throwable cause = restStatusException.getCause();
        if (cause instanceof ElasticsearchException) {
            ElasticsearchException elasticsearchException = (ElasticsearchException) cause;
            if (!indexAvailable(elasticsearchException)) {
                return new NoSuchIndexException(ObjectUtils.nullSafeToString(elasticsearchException.getMetadata("es.index")), ex);
            }
        }
    }
    if (ex instanceof ValidationException) {
        return new DataIntegrityViolationException(ex.getMessage(), ex);
    }
    Throwable cause = ex.getCause();
    if (cause instanceof IOException) {
        return new DataAccessResourceFailureException(ex.getMessage(), ex);
    }
    return null;
}
Also used : OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ValidationException(org.elasticsearch.common.ValidationException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) NoSuchIndexException(org.springframework.data.elasticsearch.NoSuchIndexException) UncategorizedElasticsearchException(org.springframework.data.elasticsearch.UncategorizedElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) UncategorizedElasticsearchException(org.springframework.data.elasticsearch.UncategorizedElasticsearchException) IOException(java.io.IOException) RestStatusException(org.springframework.data.elasticsearch.RestStatusException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

UncategorizedElasticsearchException (org.springframework.data.elasticsearch.UncategorizedElasticsearchException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)1 ValidationException (org.elasticsearch.common.ValidationException)1 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)1 NoSuchIndexException (org.springframework.data.elasticsearch.NoSuchIndexException)1 RestStatusException (org.springframework.data.elasticsearch.RestStatusException)1 NestedMetaData (org.springframework.data.elasticsearch.core.document.NestedMetaData)1 SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)1