use of org.springframework.data.elasticsearch.core.reindex.ReindexRequest in project spring-data-elasticsearch by spring-projects.
the class RequestFactory method reindexRequest.
/**
* @since 4.4
*/
public org.elasticsearch.index.reindex.ReindexRequest reindexRequest(ReindexRequest reindexRequest) {
final org.elasticsearch.index.reindex.ReindexRequest request = new org.elasticsearch.index.reindex.ReindexRequest();
if (reindexRequest.getConflicts() != null) {
request.setConflicts(reindexRequest.getConflicts().getEsName());
}
if (reindexRequest.getMaxDocs() != null) {
request.setMaxDocs(reindexRequest.getMaxDocs());
}
// region source build
final Source source = reindexRequest.getSource();
request.setSourceIndices(source.getIndexes().getIndexNames());
// source query will build from RemoteInfo if remote exist
if (source.getQuery() != null && source.getRemote() == null) {
request.setSourceQuery(getQuery(source.getQuery()));
}
if (source.getSize() != null) {
request.setSourceBatchSize(source.getSize());
}
if (source.getRemote() != null) {
Remote remote = source.getRemote();
QueryBuilder queryBuilder = source.getQuery() == null ? QueryBuilders.matchAllQuery() : getQuery(source.getQuery());
BytesReference query;
try {
XContentBuilder builder = XContentBuilder.builder(QUERY_CONTENT_TYPE).prettyPrint();
query = BytesReference.bytes(queryBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS));
} catch (IOException e) {
throw new IllegalArgumentException("Error parsing the source query content", e);
}
request.setRemoteInfo(new //
RemoteInfo(//
remote.getScheme(), //
remote.getHost(), //
remote.getPort(), //
remote.getPathPrefix(), //
query, //
remote.getUsername(), //
remote.getPassword(), //
Collections.emptyMap(), remote.getSocketTimeout() == null ? DEFAULT_SOCKET_TIMEOUT : //
timeValueSeconds(remote.getSocketTimeout().getSeconds()), remote.getConnectTimeout() == null ? DEFAULT_CONNECT_TIMEOUT : //
timeValueSeconds(remote.getConnectTimeout().getSeconds())));
}
final Slice slice = source.getSlice();
if (slice != null) {
request.getSearchRequest().source().slice(new SliceBuilder(slice.getId(), slice.getMax()));
}
final SourceFilter sourceFilter = source.getSourceFilter();
if (sourceFilter != null) {
request.getSearchRequest().source().fetchSource(sourceFilter.getIncludes(), sourceFilter.getExcludes());
}
// endregion
// region dest build
final Dest dest = reindexRequest.getDest();
request.setDestIndex(dest.getIndex().getIndexName()).setDestRouting(dest.getRouting()).setDestPipeline(dest.getPipeline());
final org.springframework.data.elasticsearch.annotations.Document.VersionType versionType = dest.getVersionType();
if (versionType != null) {
request.setDestVersionType(VersionType.fromString(versionType.name().toLowerCase(Locale.ROOT)));
}
final IndexQuery.OpType opType = dest.getOpType();
if (opType != null) {
request.setDestOpType(opType.name().toLowerCase(Locale.ROOT));
}
// endregion
// region script build
final ReindexRequest.Script script = reindexRequest.getScript();
if (script != null) {
request.setScript(new Script(DEFAULT_SCRIPT_TYPE, script.getLang(), script.getSource(), Collections.emptyMap()));
}
// endregion
// region query parameters build
final Duration timeout = reindexRequest.getTimeout();
if (timeout != null) {
request.setTimeout(timeValueSeconds(timeout.getSeconds()));
}
if (reindexRequest.getRefresh() != null) {
request.setRefresh(reindexRequest.getRefresh());
}
if (reindexRequest.getRequireAlias() != null) {
request.setRequireAlias(reindexRequest.getRequireAlias());
}
if (reindexRequest.getRequestsPerSecond() != null) {
request.setRequestsPerSecond(reindexRequest.getRequestsPerSecond());
}
final Duration scroll = reindexRequest.getScroll();
if (scroll != null) {
request.setScroll(timeValueSeconds(scroll.getSeconds()));
}
if (reindexRequest.getWaitForActiveShards() != null) {
request.setWaitForActiveShards(ActiveShardCount.parseString(reindexRequest.getWaitForActiveShards()));
}
if (reindexRequest.getSlices() != null) {
request.setSlices(reindexRequest.getSlices());
}
// endregion
return request;
}
use of org.springframework.data.elasticsearch.core.reindex.ReindexRequest in project spring-data-elasticsearch by spring-projects.
the class ReactiveElasticsearchTemplateIntegrationTests method shouldWorkSubmitReindexTask.
// #1529
@Test
void shouldWorkSubmitReindexTask() {
String sourceIndexName = indexNameProvider.indexName();
indexNameProvider.increment();
String destIndexName = indexNameProvider.indexName();
operations.indexOps(IndexCoordinates.of(destIndexName)).create();
final ReindexRequest reindexRequest = ReindexRequest.builder(IndexCoordinates.of(sourceIndexName), IndexCoordinates.of(destIndexName)).build();
operations.submitReindex(reindexRequest).as(StepVerifier::create).consumeNextWith(task -> assertThat(task).isNotBlank()).verifyComplete();
}
use of org.springframework.data.elasticsearch.core.reindex.ReindexRequest in project spring-data-elasticsearch by spring-projects.
the class ReactiveElasticsearchTemplateIntegrationTests method shouldWorkReindexForExistingIndex.
// #1529
@Test
void shouldWorkReindexForExistingIndex() {
String sourceIndexName = indexNameProvider.indexName();
SampleEntity sampleEntity = randomEntity("abc");
operations.save(sampleEntity).block();
indexNameProvider.increment();
String destIndexName = indexNameProvider.indexName();
operations.indexOps(IndexCoordinates.of(destIndexName)).create();
final ReindexRequest reindexRequest = ReindexRequest.builder(IndexCoordinates.of(sourceIndexName), IndexCoordinates.of(destIndexName)).withRefresh(true).build();
operations.reindex(reindexRequest).as(StepVerifier::create).consumeNextWith(postReindexResponse -> assertThat(postReindexResponse.getTotal()).isEqualTo(1L)).verifyComplete();
operations.count(operations.matchAllQuery(), SampleEntity.class, IndexCoordinates.of(destIndexName)).as(StepVerifier::create).expectNext(1L).verifyComplete();
}
use of org.springframework.data.elasticsearch.core.reindex.ReindexRequest in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldWorkSubmitReindexTask.
// #1529
@Test
void shouldWorkSubmitReindexTask() {
String sourceIndexName = indexNameProvider.indexName();
indexNameProvider.increment();
String destIndexName = indexNameProvider.indexName();
operations.indexOps(IndexCoordinates.of(destIndexName)).create();
final ReindexRequest reindexRequest = ReindexRequest.builder(IndexCoordinates.of(sourceIndexName), IndexCoordinates.of(destIndexName)).build();
String task = operations.submitReindex(reindexRequest);
assertThat(task).isNotBlank();
}
use of org.springframework.data.elasticsearch.core.reindex.ReindexRequest in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldWorkReindexForExistingIndex.
// #1529
@Test
void shouldWorkReindexForExistingIndex() {
String sourceIndexName = indexNameProvider.indexName();
String documentId = nextIdAsString();
SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message("abc").build();
operations.save(sampleEntity);
indexNameProvider.increment();
String destIndexName = indexNameProvider.indexName();
operations.indexOps(IndexCoordinates.of(destIndexName)).create();
final ReindexRequest reindexRequest = ReindexRequest.builder(IndexCoordinates.of(sourceIndexName), IndexCoordinates.of(destIndexName)).withRefresh(true).build();
final ReindexResponse reindex = operations.reindex(reindexRequest);
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
assertThat(reindex.getTotal()).isEqualTo(1);
assertThat(operations.count(searchQuery, IndexCoordinates.of(destIndexName))).isEqualTo(1);
}
Aggregations