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;
}
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;
}
Aggregations