Search in sources :

Example 1 with AliasActionParameters

use of org.springframework.data.elasticsearch.core.index.AliasActionParameters in project spring-data-elasticsearch by spring-projects.

the class RequestFactory method indicesAliasesRequest.

public IndicesAliasesRequest indicesAliasesRequest(AliasActions aliasActions) {
    IndicesAliasesRequest request = new IndicesAliasesRequest();
    aliasActions.getActions().forEach(aliasAction -> {
        IndicesAliasesRequest.AliasActions aliasActionsES = null;
        if (aliasAction instanceof AliasAction.Add) {
            AliasAction.Add add = (AliasAction.Add) aliasAction;
            IndicesAliasesRequest.AliasActions addES = IndicesAliasesRequest.AliasActions.add();
            AliasActionParameters parameters = add.getParameters();
            addES.indices(parameters.getIndices());
            addES.aliases(parameters.getAliases());
            addES.routing(parameters.getRouting());
            addES.indexRouting(parameters.getIndexRouting());
            addES.searchRouting(parameters.getSearchRouting());
            addES.isHidden(parameters.getHidden());
            addES.writeIndex(parameters.getWriteIndex());
            Query filterQuery = parameters.getFilterQuery();
            if (filterQuery != null) {
                elasticsearchConverter.updateQuery(filterQuery, parameters.getFilterQueryClass());
                QueryBuilder queryBuilder = getFilter(filterQuery);
                if (queryBuilder == null) {
                    queryBuilder = getQuery(filterQuery);
                }
                addES.filter(queryBuilder);
            }
            aliasActionsES = addES;
        } else if (aliasAction instanceof AliasAction.Remove) {
            AliasAction.Remove remove = (AliasAction.Remove) aliasAction;
            IndicesAliasesRequest.AliasActions removeES = IndicesAliasesRequest.AliasActions.remove();
            AliasActionParameters parameters = remove.getParameters();
            removeES.indices(parameters.getIndices());
            removeES.aliases(parameters.getAliases());
            aliasActionsES = removeES;
        } else if (aliasAction instanceof AliasAction.RemoveIndex) {
            AliasAction.RemoveIndex removeIndex = (AliasAction.RemoveIndex) aliasAction;
            IndicesAliasesRequest.AliasActions removeIndexES = IndicesAliasesRequest.AliasActions.removeIndex();
            AliasActionParameters parameters = removeIndex.getParameters();
            removeIndexES.indices(parameters.getIndices()[0]);
            aliasActionsES = removeIndexES;
        }
        if (aliasActionsES != null) {
            request.addAliasAction(aliasActionsES);
        }
    });
    return request;
}
Also used : MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) AliasActionParameters(org.springframework.data.elasticsearch.core.index.AliasActionParameters)

Example 2 with AliasActionParameters

use of org.springframework.data.elasticsearch.core.index.AliasActionParameters in project spring-data-elasticsearch by spring-projects.

the class RequestFactory method putIndexTemplateRequest.

public PutIndexTemplateRequest putIndexTemplateRequest(PutTemplateRequest putTemplateRequest) {
    PutIndexTemplateRequest request = new PutIndexTemplateRequest(putTemplateRequest.getName()).patterns(Arrays.asList(putTemplateRequest.getIndexPatterns()));
    if (putTemplateRequest.getSettings() != null) {
        request.settings(putTemplateRequest.getSettings());
    }
    if (putTemplateRequest.getMappings() != null) {
        request.mapping(putTemplateRequest.getMappings());
    }
    request.order(putTemplateRequest.getOrder()).version(putTemplateRequest.getVersion());
    AliasActions aliasActions = putTemplateRequest.getAliasActions();
    if (aliasActions != null) {
        aliasActions.getActions().forEach(aliasAction -> {
            AliasActionParameters parameters = aliasAction.getParameters();
            String[] parametersAliases = parameters.getAliases();
            if (parametersAliases != null) {
                for (String aliasName : parametersAliases) {
                    Alias alias = new Alias(aliasName);
                    if (parameters.getRouting() != null) {
                        alias.routing(parameters.getRouting());
                    }
                    if (parameters.getIndexRouting() != null) {
                        alias.indexRouting(parameters.getIndexRouting());
                    }
                    if (parameters.getSearchRouting() != null) {
                        alias.searchRouting(parameters.getSearchRouting());
                    }
                    if (parameters.getHidden() != null) {
                        alias.isHidden(parameters.getHidden());
                    }
                    if (parameters.getWriteIndex() != null) {
                        alias.writeIndex(parameters.getWriteIndex());
                    }
                    Query filterQuery = parameters.getFilterQuery();
                    if (filterQuery != null) {
                        elasticsearchConverter.updateQuery(filterQuery, parameters.getFilterQueryClass());
                        QueryBuilder queryBuilder = getFilter(filterQuery);
                        if (queryBuilder == null) {
                            queryBuilder = getQuery(filterQuery);
                        }
                        alias.filter(queryBuilder);
                    }
                    request.alias(alias);
                }
            }
        });
    }
    return request;
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) AliasActionParameters(org.springframework.data.elasticsearch.core.index.AliasActionParameters) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions)

Example 3 with AliasActionParameters

use of org.springframework.data.elasticsearch.core.index.AliasActionParameters in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchTemplateIntegrationTests method shouldReturnInformationListOfAllIndices.

// #1646, #1718
@Test
@DisplayName("should return a list of info for specific index")
void shouldReturnInformationListOfAllIndices() {
    String indexName = indexNameProvider.indexName();
    String aliasName = indexName + "-alias";
    ReactiveIndexOperations indexOps = operations.indexOps(EntityWithSettingsAndMappingsReactive.class);
    // beforeEach uses SampleEntity, so recreate the index here
    indexOps.delete().then(indexOps.createWithMapping()).block();
    AliasActionParameters parameters = AliasActionParameters.builder().withAliases(aliasName).withIndices(indexName).withIsHidden(false).withIsWriteIndex(false).withRouting("indexrouting").withSearchRouting("searchrouting").build();
    indexOps.alias(new AliasActions(new AliasAction.Add(parameters))).block();
    indexOps.getInformation().as(StepVerifier::create).consumeNextWith(indexInformation -> {
        assertThat(indexInformation.getName()).isEqualTo(indexName);
        assertThat(indexInformation.getSettings().get("index.number_of_shards")).isEqualTo("1");
        assertThat(indexInformation.getSettings().get("index.number_of_replicas")).isEqualTo("0");
        assertThat(indexInformation.getSettings().get("index.analysis.analyzer.emailAnalyzer.type")).isEqualTo("custom");
        assertThat(indexInformation.getAliases()).hasSize(1);
        AliasData aliasData = indexInformation.getAliases().get(0);
        assertThat(aliasData.getAlias()).isEqualTo(aliasName);
        assertThat(aliasData.isHidden()).isEqualTo(false);
        assertThat(aliasData.isWriteIndex()).isEqualTo(false);
        assertThat(aliasData.getIndexRouting()).isEqualTo("indexrouting");
        assertThat(aliasData.getSearchRouting()).isEqualTo("searchrouting");
        String expectedMappings = "{\"properties\":{\"email\":{\"type\":\"text\",\"analyzer\":\"emailAnalyzer\"}}}";
        try {
            JSONAssert.assertEquals(expectedMappings, indexInformation.getMapping().toJson(), false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }).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) AliasData(org.springframework.data.elasticsearch.core.index.AliasData) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) AliasActionParameters(org.springframework.data.elasticsearch.core.index.AliasActionParameters) JSONException(org.json.JSONException) StepVerifier(reactor.test.StepVerifier) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

AliasActionParameters (org.springframework.data.elasticsearch.core.index.AliasActionParameters)3 AliasActions (org.springframework.data.elasticsearch.core.index.AliasActions)3 MoreLikeThisQueryBuilder (org.elasticsearch.index.query.MoreLikeThisQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 AliasAction (org.springframework.data.elasticsearch.core.index.AliasAction)2 Boolean (java.lang.Boolean)1 Long (java.lang.Long)1 Object (java.lang.Object)1 ConnectException (java.net.ConnectException)1 LocalDate (java.time.LocalDate)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1