Throw exception on index creation error.

Closes #1056 
Original Pull Request #1637
This commit is contained in:
Peter-Josef Meisch 2021-01-10 12:23:50 +01:00 committed by GitHub
parent 6913d8045b
commit 00dcee0a5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 24 deletions

View File

@ -90,13 +90,9 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
this.entityClass = this.entityInformation.getJavaType();
this.indexOperations = operations.indexOps(this.entityClass);
try {
if (shouldCreateIndexAndMapping() && !indexOperations.exists()) {
indexOperations.create();
indexOperations.putMapping(entityClass);
}
} catch (Exception exception) {
LOGGER.warn("Cannot create index: {}", exception.getMessage());
if (shouldCreateIndexAndMapping() && !indexOperations.exists()) {
indexOperations.create();
indexOperations.putMapping(entityClass);
}
}

View File

@ -15,20 +15,17 @@
*/
package org.springframework.data.elasticsearch.repository.support;
import org.elasticsearch.index.query.IdsQueryBuilder;
import org.springframework.data.elasticsearch.core.RefreshPolicy;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.elasticsearch.index.query.IdsQueryBuilder;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ReactiveIndexOperations;
import org.springframework.data.elasticsearch.core.RefreshPolicy;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
@ -46,14 +43,12 @@ import org.springframework.util.Assert;
*/
public class SimpleReactiveElasticsearchRepository<T, ID> implements ReactiveElasticsearchRepository<T, ID> {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleElasticsearchRepository.class);
private final ElasticsearchEntityInformation<T, ID> entityInformation;
private final ReactiveElasticsearchOperations operations;
private final ReactiveIndexOperations indexOperations;
public SimpleReactiveElasticsearchRepository(ElasticsearchEntityInformation<T, ID> entityInformation,
ReactiveElasticsearchOperations operations) {
ReactiveElasticsearchOperations operations) {
Assert.notNull(entityInformation, "EntityInformation must not be null!");
Assert.notNull(operations, "ElasticsearchOperations must not be null!");
@ -66,16 +61,12 @@ public class SimpleReactiveElasticsearchRepository<T, ID> implements ReactiveEla
}
private void createIndexAndMappingIfNeeded() {
try {
if (shouldCreateIndexAndMapping()) {
indexOperations.exists() //
.flatMap(exists -> exists ? Mono.empty() : indexOperations.create()) //
.flatMap(success -> success ? indexOperations.putMapping() : Mono.empty()) //
.block();
}
} catch (Exception exception) {
LOGGER.warn("Cannot create index: {}", exception.getMessage());
if (shouldCreateIndexAndMapping()) {
indexOperations.exists() //
.flatMap(exists -> exists ? Mono.empty() : indexOperations.create()) //
.flatMap(success -> success ? indexOperations.putMapping() : Mono.empty()) //
.block();
}
}