Search in sources :

Example 1 with ReindexRequest

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;
}
Also used : Remote(org.springframework.data.elasticsearch.core.reindex.Remote) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.springframework.data.elasticsearch.core.document.Document) Source(org.springframework.data.elasticsearch.core.reindex.ReindexRequest.Source) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) BytesReference(org.elasticsearch.common.bytes.BytesReference) Script(org.elasticsearch.script.Script) Dest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest.Dest) Duration(java.time.Duration) IOException(java.io.IOException) SliceBuilder(org.elasticsearch.search.slice.SliceBuilder) Slice(org.springframework.data.elasticsearch.core.reindex.ReindexRequest.Slice) XContentBuilder(org.elasticsearch.xcontent.XContentBuilder)

Example 2 with ReindexRequest

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) StepVerifier(reactor.test.StepVerifier) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) RestStatusException(org.springframework.data.elasticsearch.RestStatusException) Version(org.springframework.data.annotation.Version) Autowired(org.springframework.beans.factory.annotation.Autowired) Order(org.junit.jupiter.api.Order) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) JSONException(org.json.JSONException) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) Long(java.lang.Long) Map(java.util.Map) Document(org.springframework.data.elasticsearch.annotations.Document) Assertions(org.assertj.core.api.Assertions) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) AliasData(org.springframework.data.elasticsearch.core.index.AliasData) org.springframework.data.elasticsearch.core.query(org.springframework.data.elasticsearch.core.query) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) PageRequest(org.springframework.data.domain.PageRequest) UUID(java.util.UUID) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) Explanation(org.springframework.data.elasticsearch.core.document.Explanation) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) FieldType(org.springframework.data.elasticsearch.annotations.FieldType) List(java.util.List) LocalDate(java.time.LocalDate) SortOrder(org.elasticsearch.search.sort.SortOrder) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) IndexNameProvider(org.springframework.data.elasticsearch.utils.IndexNameProvider) Id(org.springframework.data.annotation.Id) IntStream(java.util.stream.IntStream) DataAccessException(org.springframework.dao.DataAccessException) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Field(org.springframework.data.elasticsearch.annotations.Field) AtomicReference(java.util.concurrent.atomic.AtomicReference) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) ReactiveElasticsearchRestTemplateConfiguration(org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchRestTemplateConfiguration) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Mapping(org.springframework.data.elasticsearch.annotations.Mapping) Boolean(java.lang.Boolean) ConnectException(java.net.ConnectException) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Nullable(org.springframework.lang.Nullable) Setting(org.springframework.data.elasticsearch.annotations.Setting) AliasActionParameters(org.springframework.data.elasticsearch.core.index.AliasActionParameters) Import(org.springframework.context.annotation.Import) Mono(reactor.core.publisher.Mono) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) ReactiveElasticsearchClient(org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient) DisplayName(org.junit.jupiter.api.DisplayName) DateTimeFormatter(java.time.format.DateTimeFormatter) Object(java.lang.Object) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 3 with ReindexRequest

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) StepVerifier(reactor.test.StepVerifier) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) RestStatusException(org.springframework.data.elasticsearch.RestStatusException) Version(org.springframework.data.annotation.Version) Autowired(org.springframework.beans.factory.annotation.Autowired) Order(org.junit.jupiter.api.Order) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) JSONException(org.json.JSONException) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) Long(java.lang.Long) Map(java.util.Map) Document(org.springframework.data.elasticsearch.annotations.Document) Assertions(org.assertj.core.api.Assertions) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) AliasData(org.springframework.data.elasticsearch.core.index.AliasData) org.springframework.data.elasticsearch.core.query(org.springframework.data.elasticsearch.core.query) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) PageRequest(org.springframework.data.domain.PageRequest) UUID(java.util.UUID) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) Explanation(org.springframework.data.elasticsearch.core.document.Explanation) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) FieldType(org.springframework.data.elasticsearch.annotations.FieldType) List(java.util.List) LocalDate(java.time.LocalDate) SortOrder(org.elasticsearch.search.sort.SortOrder) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) IndexNameProvider(org.springframework.data.elasticsearch.utils.IndexNameProvider) Id(org.springframework.data.annotation.Id) IntStream(java.util.stream.IntStream) DataAccessException(org.springframework.dao.DataAccessException) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Field(org.springframework.data.elasticsearch.annotations.Field) AtomicReference(java.util.concurrent.atomic.AtomicReference) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) ReactiveElasticsearchRestTemplateConfiguration(org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchRestTemplateConfiguration) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Mapping(org.springframework.data.elasticsearch.annotations.Mapping) Boolean(java.lang.Boolean) ConnectException(java.net.ConnectException) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Nullable(org.springframework.lang.Nullable) Setting(org.springframework.data.elasticsearch.annotations.Setting) AliasActionParameters(org.springframework.data.elasticsearch.core.index.AliasActionParameters) Import(org.springframework.context.annotation.Import) Mono(reactor.core.publisher.Mono) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) ReactiveElasticsearchClient(org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient) DisplayName(org.junit.jupiter.api.DisplayName) DateTimeFormatter(java.time.format.DateTimeFormatter) Object(java.lang.Object) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 4 with ReindexRequest

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();
}
Also used : ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 5 with ReindexRequest

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);
}
Also used : ReindexResponse(org.springframework.data.elasticsearch.core.reindex.ReindexResponse) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Aggregations

ReindexRequest (org.springframework.data.elasticsearch.core.reindex.ReindexRequest)7 Test (org.junit.jupiter.api.Test)6 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)4 Boolean (java.lang.Boolean)2 Long (java.lang.Long)2 Object (java.lang.Object)2 ConnectException (java.net.ConnectException)2 LocalDate (java.time.LocalDate)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Assertions (org.assertj.core.api.Assertions)2