DATAES-976 - Polishing.

Use deleteAllById(…) from deleteAll(…). Simplify implementation in SimpleReactiveElasticsearchRepository.

Original pull request: #554.
This commit is contained in:
Mark Paluch 2020-11-25 12:10:19 +01:00
parent 749270ba25
commit 9bf6b6a984
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
5 changed files with 126 additions and 141 deletions

View File

@ -166,7 +166,7 @@ public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {
public <T> List<T> multiGet(Query query, Class<T> clazz, IndexCoordinates index) { public <T> List<T> multiGet(Query query, Class<T> clazz, IndexCoordinates index) {
Assert.notNull(index, "index must not be null"); Assert.notNull(index, "index must not be null");
Assert.notEmpty(query.getIds(), "No Id define for Query"); Assert.notEmpty(query.getIds(), "No Id defined for Query");
MultiGetRequest request = requestFactory.multiGetRequest(query, clazz, index); MultiGetRequest request = requestFactory.multiGetRequest(query, clazz, index);
MultiGetResponse result = execute(client -> client.mget(request, RequestOptions.DEFAULT)); MultiGetResponse result = execute(client -> client.mget(request, RequestOptions.DEFAULT));

View File

@ -27,6 +27,7 @@ import org.elasticsearch.index.query.IdsQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@ -37,7 +38,6 @@ import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHitSupport; import org.springframework.data.elasticsearch.core.SearchHitSupport;
import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
@ -50,6 +50,7 @@ import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.Streamable; import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/** /**
* Elasticsearch specific repository implementation. Likely to be used as target within * Elasticsearch specific repository implementation. Likely to be used as target within
@ -151,14 +152,12 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
Assert.notNull(ids, "ids can't be null."); Assert.notNull(ids, "ids can't be null.");
List<T> result = new ArrayList<>(); List<T> result = new ArrayList<>();
List<String> stringIds = stringIdsRepresentation(ids); Query idQuery = getIdQuery(ids);
if (CollectionUtils.isEmpty(idQuery.getIds())) {
if (stringIds.isEmpty()) {
return result; return result;
} }
NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(stringIds).build(); List<T> multiGetEntities = execute(operations -> operations.multiGet(idQuery, entityClass, getIndexCoordinates()));
List<T> multiGetEntities = execute(operations -> operations.multiGet(query, entityClass, getIndexCoordinates()));
if (multiGetEntities != null) { if (multiGetEntities != null) {
multiGetEntities.forEach(entity -> { multiGetEntities.forEach(entity -> {
@ -290,32 +289,6 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
doDelete(extractIdFromBean(entity), operations.getEntityRouting(entity), getIndexCoordinates()); doDelete(extractIdFromBean(entity), operations.getEntityRouting(entity), getIndexCoordinates());
} }
@Override
public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "Cannot delete 'null' list.");
IndexCoordinates indexCoordinates = getIndexCoordinates();
IdsQueryBuilder idsQueryBuilder = idsQuery();
for (T entity : entities) {
ID id = extractIdFromBean(entity);
if (id != null) {
idsQueryBuilder.addIds(stringIdRepresentation(id));
}
}
if (idsQueryBuilder.ids().isEmpty()) {
return;
}
Query query = new NativeSearchQueryBuilder().withQuery(idsQueryBuilder).build();
executeAndRefresh((OperationsCallback<Void>) operations -> {
operations.delete(query, entityClass, indexCoordinates);
return null;
});
}
@Override @Override
public void deleteAllById(Iterable<? extends ID> ids) { public void deleteAllById(Iterable<? extends ID> ids) {
@ -324,23 +297,36 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
IndexCoordinates indexCoordinates = getIndexCoordinates(); IndexCoordinates indexCoordinates = getIndexCoordinates();
IdsQueryBuilder idsQueryBuilder = idsQuery(); IdsQueryBuilder idsQueryBuilder = idsQuery();
for (ID id : ids) { for (ID id : ids) {
if (id != null) { idsQueryBuilder.addIds(stringIdRepresentation(id));
idsQueryBuilder.addIds(stringIdRepresentation(id));
}
} }
if (idsQueryBuilder.ids().isEmpty()) { if (idsQueryBuilder.ids().isEmpty()) {
return; return;
} }
Query query = new NativeSearchQueryBuilder().withQuery(idsQueryBuilder).build();
executeAndRefresh((OperationsCallback<Void>) operations -> { executeAndRefresh((OperationsCallback<Void>) operations -> {
operations.delete(query, entityClass, indexCoordinates); operations.delete(new NativeSearchQueryBuilder().withQuery(idsQueryBuilder).build(), entityClass,
indexCoordinates);
return null; return null;
}); });
} }
@Override
public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "Cannot delete 'null' list.");
List<ID> ids = new ArrayList<>();
for (T entity : entities) {
ID id = extractIdFromBean(entity);
if (id != null) {
ids.add(id);
}
}
deleteAllById(ids);
}
private void doDelete(@Nullable ID id, @Nullable String routing, IndexCoordinates indexCoordinates) { private void doDelete(@Nullable ID id, @Nullable String routing, IndexCoordinates indexCoordinates) {
if (id != null) { if (id != null) {
@ -369,7 +355,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
return entityInformation.getId(entity); return entityInformation.getId(entity);
} }
private List<String> stringIdsRepresentation(Iterable<ID> ids) { private List<String> stringIdsRepresentation(Iterable<? extends ID> ids) {
Assert.notNull(ids, "ids can't be null."); Assert.notNull(ids, "ids can't be null.");
@ -385,6 +371,12 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
return operations.getIndexCoordinatesFor(entityClass); return operations.getIndexCoordinatesFor(entityClass);
} }
private Query getIdQuery(Iterable<? extends ID> ids) {
List<String> stringIds = stringIdsRepresentation(ids);
return new NativeSearchQueryBuilder().withIds(stringIds).build();
}
// region operations callback // region operations callback
@FunctionalInterface @FunctionalInterface
public interface OperationsCallback<R> { public interface OperationsCallback<R> {

View File

@ -15,14 +15,15 @@
*/ */
package org.springframework.data.elasticsearch.repository.support; package org.springframework.data.elasticsearch.repository.support;
import org.elasticsearch.index.query.IdsQueryBuilder;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.elasticsearch.action.support.WriteRequest.RefreshPolicy; import org.elasticsearch.action.support.WriteRequest.RefreshPolicy;
import org.elasticsearch.index.query.QueryBuilders;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
@ -33,9 +34,7 @@ import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersiste
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository; import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository;
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -167,7 +166,7 @@ public class SimpleReactiveElasticsearchRepository<T, ID> implements ReactiveEla
Assert.notNull(idStream, "IdStream must not be null!"); Assert.notNull(idStream, "IdStream must not be null!");
return Flux.from(idStream) // return Flux.from(idStream) //
.map(ID::toString) // .map(this::convertId) //
.collectList() // .collectList() //
.map(ids -> new NativeSearchQueryBuilder().withIds(ids).build()) // .map(ids -> new NativeSearchQueryBuilder().withIds(ids).build()) //
.flatMapMany(query -> { .flatMapMany(query -> {
@ -206,6 +205,20 @@ public class SimpleReactiveElasticsearchRepository<T, ID> implements ReactiveEla
.then(doRefresh()); .then(doRefresh());
} }
@Override
public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {
Assert.notNull(ids, "Ids must not be null!");
return Flux.fromIterable(ids) //
.map(this::convertId) //
.collectList() //
.map(it -> new NativeSearchQueryBuilder().withQuery(new IdsQueryBuilder().addIds(it.toArray(new String[0])))
.build())
.flatMap(it -> operations.delete(it, entityInformation.getJavaType(), entityInformation.getIndexCoordinates())) //
.then(doRefresh());
}
@Override @Override
public Mono<Void> deleteAll(Iterable<? extends T> entities) { public Mono<Void> deleteAll(Iterable<? extends T> entities) {
@ -213,36 +226,16 @@ public class SimpleReactiveElasticsearchRepository<T, ID> implements ReactiveEla
return deleteAll(Flux.fromIterable(entities)); return deleteAll(Flux.fromIterable(entities));
} }
@Override
public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {
Assert.notNull(ids, "Ids must not be null!");
return Mono.just(Streamable.of(ids) //
.map(this::convertId).toList() //
).map(objects -> new StringQuery(QueryBuilders.idsQuery() //
.addIds(objects.toArray(new String[0])) //
.toString()) //
).flatMap(
query -> operations.delete(query, entityInformation.getJavaType(), entityInformation.getIndexCoordinates())) //
.then(doRefresh());
}
@Override @Override
public Mono<Void> deleteAll(Publisher<? extends T> entityStream) { public Mono<Void> deleteAll(Publisher<? extends T> entityStream) {
Assert.notNull(entityStream, "EntityStream must not be null!"); Assert.notNull(entityStream, "EntityStream must not be null!");
return Flux.from(entityStream) // return Flux.from(entityStream) //
.map(entity -> { .map(entityInformation::getRequiredId) //
.map(this::convertId) //
ID id = entityInformation.getId(entity); .collectList() //
if (id == null) { .map(it -> new NativeSearchQueryBuilder().withQuery(new IdsQueryBuilder().addIds(it.toArray(new String[0])))
throw new IllegalStateException("Entity id must not be null!"); .build())
}
return convertId(id);
}).collectList().map(objects -> new StringQuery(QueryBuilders.idsQuery() //
.addIds(objects.toArray(new String[0])) //
.toString())) //
.flatMap( .flatMap(
query -> operations.delete(query, entityInformation.getJavaType(), entityInformation.getIndexCoordinates())) // query -> operations.delete(query, entityInformation.getJavaType(), entityInformation.getIndexCoordinates())) //
.then(doRefresh()); .then(doRefresh());

View File

@ -15,7 +15,6 @@
*/ */
package org.springframework.data.elasticsearch.repository.support; package org.springframework.data.elasticsearch.repository.support;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.elasticsearch.annotations.FieldType.*; import static org.springframework.data.elasticsearch.annotations.FieldType.*;
import static org.springframework.data.elasticsearch.core.query.Query.*; import static org.springframework.data.elasticsearch.core.query.Query.*;
@ -31,6 +30,7 @@ import reactor.test.StepVerifier;
import java.lang.Boolean; import java.lang.Boolean;
import java.lang.Long; import java.lang.Long;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -69,7 +69,7 @@ import org.springframework.test.context.ContextConfiguration;
*/ */
@SpringIntegrationTest @SpringIntegrationTest
@ContextConfiguration(classes = { SimpleReactiveElasticsearchRepositoryTests.Config.class }) @ContextConfiguration(classes = { SimpleReactiveElasticsearchRepositoryTests.Config.class })
public class SimpleReactiveElasticsearchRepositoryTests { class SimpleReactiveElasticsearchRepositoryTests {
@Configuration @Configuration
@Import({ ReactiveElasticsearchRestTemplateConfiguration.class }) @Import({ ReactiveElasticsearchRestTemplateConfiguration.class })
@ -82,7 +82,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @Autowired ReactiveSampleEntityRepository repository; @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @Autowired ReactiveSampleEntityRepository repository;
@BeforeEach @BeforeEach
public void setUp() { void setUp() {
operations.indexOps(IndexCoordinates.of(INDEX)).delete().block(); operations.indexOps(IndexCoordinates.of(INDEX)).delete().block();
} }
@ -92,7 +92,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void saveShouldSaveSingleEntity() { void saveShouldSaveSingleEntity() {
repository.save(SampleEntity.builder().build()) // repository.save(SampleEntity.builder().build()) //
.map(SampleEntity::getId) // .map(SampleEntity::getId) //
@ -106,10 +106,10 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void saveShouldComputeMultipleEntities() { void saveShouldComputeMultipleEntities() {
repository repository
.saveAll(asList(SampleEntity.builder().build(), SampleEntity.builder().build(), .saveAll(Arrays.asList(SampleEntity.builder().build(), SampleEntity.builder().build(),
SampleEntity.builder().build())) // SampleEntity.builder().build())) //
.map(SampleEntity::getId) // .map(SampleEntity::getId) //
.flatMap(this::documentWithIdExistsInIndex) // .flatMap(this::documentWithIdExistsInIndex) //
@ -121,14 +121,14 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519, DATAES-767, DATAES-822 @Test // DATAES-519, DATAES-767, DATAES-822
public void findByIdShouldErrorIfIndexDoesNotExist() { void findByIdShouldErrorIfIndexDoesNotExist() {
repository.findById("id-two") // repository.findById("id-two") //
.as(StepVerifier::create) // .as(StepVerifier::create) //
.expectError(ElasticsearchStatusException.class); .expectError(ElasticsearchStatusException.class);
} }
@Test // DATAES-519 @Test // DATAES-519
public void findShouldRetrieveSingleEntityById() { void findShouldRetrieveSingleEntityById() {
bulkIndex(SampleEntity.builder().id("id-one").build(), // bulkIndex(SampleEntity.builder().id("id-one").build(), //
SampleEntity.builder().id("id-two").build(), // SampleEntity.builder().id("id-two").build(), //
@ -141,7 +141,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void findByIdShouldCompleteIfNothingFound() { void findByIdShouldCompleteIfNothingFound() {
bulkIndex(SampleEntity.builder().id("id-one").build(), // bulkIndex(SampleEntity.builder().id("id-one").build(), //
SampleEntity.builder().id("id-two").build(), // SampleEntity.builder().id("id-two").build(), //
@ -153,7 +153,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-720 @Test // DATAES-720
public void findAllShouldReturnAllElements() { void findAllShouldReturnAllElements() {
// make sure to be above the default page size of the Query interface // make sure to be above the default page size of the Query interface
int count = DEFAULT_PAGE_SIZE * 2; int count = DEFAULT_PAGE_SIZE * 2;
bulkIndex(IntStream.range(1, count + 1) // bulkIndex(IntStream.range(1, count + 1) //
@ -168,19 +168,19 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void findAllByIdByIdShouldCompleteIfIndexDoesNotExist() { void findAllByIdByIdShouldCompleteIfIndexDoesNotExist() {
repository.findAllById(asList("id-two", "id-two")).as(StepVerifier::create).verifyComplete(); repository.findAllById(Arrays.asList("id-two", "id-two")).as(StepVerifier::create).verifyComplete();
} }
@Test // DATAES-519 @Test // DATAES-519
public void findAllByIdShouldRetrieveMatchingDocuments() { void findAllByIdShouldRetrieveMatchingDocuments() {
bulkIndex(SampleEntity.builder().id("id-one").build(), // bulkIndex(SampleEntity.builder().id("id-one").build(), //
SampleEntity.builder().id("id-two").build(), // SampleEntity.builder().id("id-two").build(), //
SampleEntity.builder().id("id-three").build()) // SampleEntity.builder().id("id-three").build()) //
.block(); .block();
repository.findAllById(asList("id-one", "id-two")) // repository.findAllById(Arrays.asList("id-one", "id-two")) //
.as(StepVerifier::create)// .as(StepVerifier::create)//
.expectNextMatches(entity -> entity.getId().equals("id-one") || entity.getId().equals("id-two")) // .expectNextMatches(entity -> entity.getId().equals("id-one") || entity.getId().equals("id-two")) //
.expectNextMatches(entity -> entity.getId().equals("id-one") || entity.getId().equals("id-two")) // .expectNextMatches(entity -> entity.getId().equals("id-one") || entity.getId().equals("id-two")) //
@ -188,14 +188,14 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void findAllByIdShouldCompleteWhenNothingFound() { void findAllByIdShouldCompleteWhenNothingFound() {
bulkIndex(SampleEntity.builder().id("id-one").build(), // bulkIndex(SampleEntity.builder().id("id-one").build(), //
SampleEntity.builder().id("id-two").build(), // SampleEntity.builder().id("id-two").build(), //
SampleEntity.builder().id("id-three").build()) // SampleEntity.builder().id("id-three").build()) //
.block(); .block();
repository.findAllById(asList("can't", "touch", "this")) // repository.findAllById(Arrays.asList("can't", "touch", "this")) //
.as(StepVerifier::create)// .as(StepVerifier::create)//
.verifyComplete(); .verifyComplete();
} }
@ -267,14 +267,14 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519, DATAES-767, DATAES-822 @Test // DATAES-519, DATAES-767, DATAES-822
public void countShouldErrorWhenIndexDoesNotExist() { void countShouldErrorWhenIndexDoesNotExist() {
repository.count() // repository.count() //
.as(StepVerifier::create) // .as(StepVerifier::create) //
.expectError(ElasticsearchStatusException.class); .expectError(ElasticsearchStatusException.class);
} }
@Test // DATAES-519 @Test // DATAES-519
public void countShouldCountDocuments() { void countShouldCountDocuments() {
bulkIndex(SampleEntity.builder().id("id-one").build(), // bulkIndex(SampleEntity.builder().id("id-one").build(), //
SampleEntity.builder().id("id-two").build()) // SampleEntity.builder().id("id-two").build()) //
@ -284,7 +284,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void existsByIdShouldReturnTrueIfExists() { void existsByIdShouldReturnTrueIfExists() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -298,7 +298,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void existsByIdShouldReturnFalseIfNotExists() { void existsByIdShouldReturnFalseIfNotExists() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -312,7 +312,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void countShouldCountMatchingDocuments() { void countShouldCountMatchingDocuments() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -326,7 +326,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void existsShouldReturnTrueIfExists() { void existsShouldReturnTrueIfExists() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -340,7 +340,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void existsShouldReturnFalseIfNotExists() { void existsShouldReturnFalseIfNotExists() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -354,7 +354,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void deleteByIdShouldCompleteIfNothingDeleted() { void deleteByIdShouldCompleteIfNothingDeleted() {
bulkIndex(SampleEntity.builder().id("id-one").build(), // bulkIndex(SampleEntity.builder().id("id-one").build(), //
SampleEntity.builder().id("id-two").build()) // SampleEntity.builder().id("id-two").build()) //
@ -364,14 +364,14 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519, DATAES-767, DATAES-822, DATAES-678 @Test // DATAES-519, DATAES-767, DATAES-822, DATAES-678
public void deleteByIdShouldCompleteWhenIndexDoesNotExist() { void deleteByIdShouldCompleteWhenIndexDoesNotExist() {
repository.deleteById("does-not-exist") // repository.deleteById("does-not-exist") //
.as(StepVerifier::create) // .as(StepVerifier::create) //
.verifyComplete(); .verifyComplete();
} }
@Test // DATAES-519 @Test // DATAES-519
public void deleteByIdShouldDeleteEntry() { void deleteByIdShouldDeleteEntry() {
SampleEntity toBeDeleted = SampleEntity.builder().id("id-two").build(); SampleEntity toBeDeleted = SampleEntity.builder().id("id-two").build();
bulkIndex(SampleEntity.builder().id("id-one").build(), toBeDeleted) // bulkIndex(SampleEntity.builder().id("id-one").build(), toBeDeleted) //
@ -383,19 +383,19 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-976 @Test // DATAES-976
public void deleteAllByIdShouldDeleteEntry() { void deleteAllByIdShouldDeleteEntry() {
SampleEntity toBeDeleted = SampleEntity.builder().id("id-two").build(); SampleEntity toBeDeleted = SampleEntity.builder().id("id-two").build();
bulkIndex(SampleEntity.builder().id("id-one").build(), toBeDeleted) // bulkIndex(SampleEntity.builder().id("id-one").build(), toBeDeleted) //
.block(); .block();
repository.deleteAllById(asList(toBeDeleted.getId())).as(StepVerifier::create).verifyComplete(); repository.deleteAllById(Collections.singletonList(toBeDeleted.getId())).as(StepVerifier::create).verifyComplete();
assertThat(documentWithIdExistsInIndex(toBeDeleted.getId()).block()).isFalse(); assertThat(documentWithIdExistsInIndex(toBeDeleted.getId()).block()).isFalse();
} }
@Test // DATAES-519 @Test // DATAES-519
public void deleteShouldDeleteEntry() { void deleteShouldDeleteEntry() {
SampleEntity toBeDeleted = SampleEntity.builder().id("id-two").build(); SampleEntity toBeDeleted = SampleEntity.builder().id("id-two").build();
bulkIndex(SampleEntity.builder().id("id-one").build(), toBeDeleted) // bulkIndex(SampleEntity.builder().id("id-one").build(), toBeDeleted) //
@ -407,7 +407,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void deleteAllShouldDeleteGivenEntries() { void deleteAllShouldDeleteGivenEntries() {
SampleEntity toBeDeleted = SampleEntity.builder().id("id-one").build(); SampleEntity toBeDeleted = SampleEntity.builder().id("id-one").build();
SampleEntity hangInThere = SampleEntity.builder().id("id-two").build(); SampleEntity hangInThere = SampleEntity.builder().id("id-two").build();
@ -416,7 +416,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
bulkIndex(toBeDeleted, hangInThere, toBeDeleted2) // bulkIndex(toBeDeleted, hangInThere, toBeDeleted2) //
.block(); .block();
repository.deleteAll(asList(toBeDeleted, toBeDeleted2)).as(StepVerifier::create).verifyComplete(); repository.deleteAll(Arrays.asList(toBeDeleted, toBeDeleted2)).as(StepVerifier::create).verifyComplete();
assertThat(documentWithIdExistsInIndex(toBeDeleted.getId()).block()).isFalse(); assertThat(documentWithIdExistsInIndex(toBeDeleted.getId()).block()).isFalse();
assertThat(documentWithIdExistsInIndex(toBeDeleted2.getId()).block()).isFalse(); assertThat(documentWithIdExistsInIndex(toBeDeleted2.getId()).block()).isFalse();
@ -424,7 +424,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void deleteAllShouldDeleteAllEntries() { void deleteAllShouldDeleteAllEntries() {
bulkIndex(SampleEntity.builder().id("id-one").build(), // bulkIndex(SampleEntity.builder().id("id-one").build(), //
SampleEntity.builder().id("id-two").build(), // SampleEntity.builder().id("id-two").build(), //
@ -440,7 +440,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void derivedFinderMethodShouldBeExecutedCorrectly() { void derivedFinderMethodShouldBeExecutedCorrectly() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -454,7 +454,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void derivedFinderMethodShouldBeExecutedCorrectlyWhenGivenPublisher() { void derivedFinderMethodShouldBeExecutedCorrectlyWhenGivenPublisher() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -468,7 +468,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void derivedFinderWithDerivedSortMethodShouldBeExecutedCorrectly() { void derivedFinderWithDerivedSortMethodShouldBeExecutedCorrectly() {
bulkIndex(SampleEntity.builder().id("id-one").message("test").rate(3).build(), // bulkIndex(SampleEntity.builder().id("id-one").message("test").rate(3).build(), //
SampleEntity.builder().id("id-two").message("test test").rate(1).build(), // SampleEntity.builder().id("id-two").message("test test").rate(1).build(), //
@ -484,7 +484,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void derivedFinderMethodWithSortParameterShouldBeExecutedCorrectly() { void derivedFinderMethodWithSortParameterShouldBeExecutedCorrectly() {
bulkIndex(SampleEntity.builder().id("id-one").message("test").rate(3).build(), // bulkIndex(SampleEntity.builder().id("id-one").message("test").rate(3).build(), //
SampleEntity.builder().id("id-two").message("test test").rate(1).build(), // SampleEntity.builder().id("id-two").message("test test").rate(1).build(), //
@ -500,7 +500,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void derivedFinderMethodWithPageableParameterShouldBeExecutedCorrectly() { void derivedFinderMethodWithPageableParameterShouldBeExecutedCorrectly() {
bulkIndex(SampleEntity.builder().id("id-one").message("test").rate(3).build(), // bulkIndex(SampleEntity.builder().id("id-one").message("test").rate(3).build(), //
SampleEntity.builder().id("id-two").message("test test").rate(1).build(), // SampleEntity.builder().id("id-two").message("test test").rate(1).build(), //
@ -515,7 +515,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void derivedFinderMethodReturningMonoShouldBeExecutedCorrectly() { void derivedFinderMethodReturningMonoShouldBeExecutedCorrectly() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -529,7 +529,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void annotatedFinderMethodShouldBeExecutedCorrectly() { void annotatedFinderMethodShouldBeExecutedCorrectly() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -543,7 +543,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
@Test // DATAES-519 @Test // DATAES-519
public void derivedDeleteMethodShouldBeExecutedCorrectly() { void derivedDeleteMethodShouldBeExecutedCorrectly() {
bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), //
SampleEntity.builder().id("id-two").message("test message").build(), // SampleEntity.builder().id("id-two").message("test message").build(), //
@ -561,7 +561,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
} }
Mono<Void> bulkIndex(SampleEntity... entities) { Mono<Void> bulkIndex(SampleEntity... entities) {
return operations.saveAll(asList(entities), IndexCoordinates.of(INDEX)).then(); return operations.saveAll(Arrays.asList(entities), IndexCoordinates.of(INDEX)).then();
} }
interface ReactiveSampleEntityRepository extends ReactiveCrudRepository<SampleEntity, String> { interface ReactiveSampleEntityRepository extends ReactiveCrudRepository<SampleEntity, String> {

View File

@ -76,7 +76,7 @@ import org.springframework.test.context.ContextConfiguration;
*/ */
@SpringIntegrationTest @SpringIntegrationTest
@ContextConfiguration(classes = { SimpleElasticsearchRepositoryIntegrationTests.Config.class }) @ContextConfiguration(classes = { SimpleElasticsearchRepositoryIntegrationTests.Config.class })
public class SimpleElasticsearchRepositoryIntegrationTests { class SimpleElasticsearchRepositoryIntegrationTests {
@Configuration @Configuration
@Import({ ElasticsearchRestTemplateConfiguration.class }) @Import({ ElasticsearchRestTemplateConfiguration.class })
@ -91,7 +91,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
private IndexOperations indexOperations; private IndexOperations indexOperations;
@BeforeEach @BeforeEach
public void before() { void before() {
indexOperations = operations.indexOps(SampleEntity.class); indexOperations = operations.indexOps(SampleEntity.class);
IndexInitializer.init(indexOperations); IndexInitializer.init(indexOperations);
} }
@ -102,7 +102,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDoBulkIndexDocument() { void shouldDoBulkIndexDocument() {
// given // given
String documentId1 = nextIdAsString(); String documentId1 = nextIdAsString();
@ -129,7 +129,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldSaveDocument() { void shouldSaveDocument() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -147,7 +147,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void throwExceptionWhenTryingToInsertWithVersionButWithoutId() { void throwExceptionWhenTryingToInsertWithVersionButWithoutId() {
// given // given
SampleEntity sampleEntity = new SampleEntity(); SampleEntity sampleEntity = new SampleEntity();
@ -159,7 +159,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldFindDocumentById() { void shouldFindDocumentById() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -178,7 +178,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldReturnCountOfDocuments() { void shouldReturnCountOfDocuments() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -196,7 +196,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldFindAllDocuments() { void shouldFindAllDocuments() {
// when // when
Iterable<SampleEntity> results = repository.findAll(); Iterable<SampleEntity> results = repository.findAll();
@ -206,7 +206,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDeleteDocument() { void shouldDeleteDocument() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -225,7 +225,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldSearchDocumentsGivenSearchQuery() { void shouldSearchDocumentsGivenSearchQuery() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -246,7 +246,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldSearchDocumentsGivenElasticsearchQuery() { void shouldSearchDocumentsGivenElasticsearchQuery() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -265,7 +265,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test // DATAES-82 @Test // DATAES-82
public void shouldFindAllByIdQuery() { void shouldFindAllByIdQuery() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -290,7 +290,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldSaveIterableEntities() { void shouldSaveIterableEntities() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -316,7 +316,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldReturnTrueGivenDocumentWithIdExists() { void shouldReturnTrueGivenDocumentWithIdExists() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -334,7 +334,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test // DATAES-363 @Test // DATAES-363
public void shouldReturnFalseGivenDocumentWithIdDoesNotExist() { void shouldReturnFalseGivenDocumentWithIdDoesNotExist() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -347,7 +347,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldReturnResultsForGivenSearchQuery() { void shouldReturnResultsForGivenSearchQuery() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -366,7 +366,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDeleteAll() { void shouldDeleteAll() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -386,7 +386,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDeleteById() { void shouldDeleteById() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -407,7 +407,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test //DATAES-976 @Test //DATAES-976
public void shouldDeleteAllById() { void shouldDeleteAllById() {
// given // given
String id1 = nextIdAsString(); String id1 = nextIdAsString();
@ -441,7 +441,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDeleteByMessageAndReturnList() { void shouldDeleteByMessageAndReturnList() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -477,7 +477,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDeleteByListForMessage() { void shouldDeleteByListForMessage() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -510,7 +510,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDeleteByType() { void shouldDeleteByType() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -542,7 +542,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDeleteEntity() { void shouldDeleteEntity() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -562,7 +562,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldReturnIterableEntities() { void shouldReturnIterableEntities() {
// given // given
String documentId1 = nextIdAsString(); String documentId1 = nextIdAsString();
@ -587,7 +587,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldDeleteIterableEntities() { void shouldDeleteIterableEntities() {
// given // given
String documentId1 = nextIdAsString(); String documentId1 = nextIdAsString();
@ -614,7 +614,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldIndexEntity() { void shouldIndexEntity() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -632,7 +632,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldIndexWithoutRefreshEntity() { void shouldIndexWithoutRefreshEntity() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -655,7 +655,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldSortByGivenField() { void shouldSortByGivenField() {
// given // given
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -678,7 +678,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test @Test
public void shouldReturnSimilarEntities() { void shouldReturnSimilarEntities() {
// given // given
String sampleMessage = "So we build a web site or an application and want to add search to it, " String sampleMessage = "So we build a web site or an application and want to add search to it, "
@ -699,7 +699,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test // DATAES-142 @Test // DATAES-142
public void shouldIndexNotEmptyList() { void shouldIndexNotEmptyList() {
// given // given
List<SampleEntity> list = new ArrayList<>(); List<SampleEntity> list = new ArrayList<>();
String documentId = nextIdAsString(); String documentId = nextIdAsString();
@ -720,7 +720,7 @@ public class SimpleElasticsearchRepositoryIntegrationTests {
} }
@Test // DATAES-142 @Test // DATAES-142
public void shouldNotFailOnIndexingEmptyList() { void shouldNotFailOnIndexingEmptyList() {
Iterable<SampleEntity> savedEntities = repository.saveAll(Collections.emptyList()); Iterable<SampleEntity> savedEntities = repository.saveAll(Collections.emptyList());
assertThat(savedEntities).hasSize(0); assertThat(savedEntities).hasSize(0);