Search in sources :

Example 1 with AliasData

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

the class ResponseConverter method getTemplateData.

// endregion
// region templates
@Nullable
public static TemplateData getTemplateData(GetIndexTemplatesResponse getIndexTemplatesResponse, String templateName) {
    for (IndexTemplateMetadata indexTemplateMetadata : getIndexTemplatesResponse.getIndexTemplates()) {
        if (indexTemplateMetadata.name().equals(templateName)) {
            Settings settings = new Settings();
            org.elasticsearch.common.settings.Settings templateSettings = indexTemplateMetadata.settings();
            templateSettings.keySet().forEach(key -> settings.put(key, templateSettings.get(key)));
            Map<String, AliasData> aliases = new LinkedHashMap<>();
            ImmutableOpenMap<String, AliasMetadata> aliasesResponse = indexTemplateMetadata.aliases();
            Iterator<String> keysIt = aliasesResponse.keysIt();
            while (keysIt.hasNext()) {
                String key = keysIt.next();
                aliases.put(key, ResponseConverter.toAliasData(aliasesResponse.get(key)));
            }
            return // 
            TemplateData.builder().withIndexPatterns(indexTemplateMetadata.patterns().toArray(new String[0])).withSettings(// 
            settings).withMapping(// 
            Document.from(indexTemplateMetadata.mappings().getSourceAsMap())).withAliases(// 
            aliases).withOrder(// 
            indexTemplateMetadata.order()).withVersion(indexTemplateMetadata.version()).build();
        }
    }
    return null;
}
Also used : AliasData(org.springframework.data.elasticsearch.core.index.AliasData) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.elasticsearch.client.indices.IndexTemplateMetadata) Settings(org.springframework.data.elasticsearch.core.index.Settings) LinkedHashMap(java.util.LinkedHashMap) Nullable(org.springframework.lang.Nullable)

Example 2 with AliasData

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

the class ResponseConverter method getIndexInformations.

/**
 * get the index informations from a {@link org.elasticsearch.action.admin.indices.get.GetIndexResponse} (transport
 * client)
 *
 * @param getIndexResponse the index response, must not be {@literal null}
 * @return list of {@link IndexInformation}s for the different indices
 */
public static List<IndexInformation> getIndexInformations(org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse) {
    List<IndexInformation> indexInformationList = new ArrayList<>();
    for (String indexName : getIndexResponse.getIndices()) {
        Settings settings = settingsFromGetIndexResponse(getIndexResponse, indexName);
        Document mappings = mappingsFromGetIndexResponse(getIndexResponse, indexName);
        List<AliasData> aliases = aliasDataFromIndexResponse(getIndexResponse, indexName);
        indexInformationList.add(IndexInformation.of(indexName, settings, mappings, aliases));
    }
    return indexInformationList;
}
Also used : AliasData(org.springframework.data.elasticsearch.core.index.AliasData) ArrayList(java.util.ArrayList) Document(org.springframework.data.elasticsearch.core.document.Document) Settings(org.springframework.data.elasticsearch.core.index.Settings)

Example 3 with AliasData

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

the class ReactiveIndexOperationsTest method shouldGetTemplate.

// DATAES-612
@Test
void shouldGetTemplate() throws JSONException {
    ReactiveIndexOperations indexOps = operations.indexOps(Entity.class);
    org.springframework.data.elasticsearch.core.document.Document mapping = indexOps.createMapping(TemplateClass.class).block();
    Settings settings = indexOps.createSettings(TemplateClass.class).block();
    AliasActions aliasActions = new AliasActions(new AliasAction.Add(AliasActionParameters.builderForTemplate().withAliases("alias1", "alias2").build()));
    PutTemplateRequest putTemplateRequest = // 
    PutTemplateRequest.builder("test-template", "log-*").withSettings(// 
    settings).withMappings(// 
    mapping).withAliasActions(// 
    aliasActions).withOrder(// 
    11).withVersion(// 
    42).build();
    Boolean acknowledged = indexOps.putTemplate(putTemplateRequest).block();
    assertThat(acknowledged).isTrue();
    GetTemplateRequest getTemplateRequest = new GetTemplateRequest(putTemplateRequest.getName());
    TemplateData templateData = indexOps.getTemplate(getTemplateRequest).block();
    assertThat(templateData).isNotNull();
    assertThat(templateData.getIndexPatterns()).containsExactlyInAnyOrder(putTemplateRequest.getIndexPatterns());
    assertEquals(settings.toJson(), templateData.getSettings().toJson(), false);
    assertEquals(mapping.toJson(), templateData.getMapping().toJson(), false);
    Map<String, AliasData> aliases = templateData.getAliases();
    assertThat(aliases).hasSize(2);
    AliasData alias1 = aliases.get("alias1");
    assertThat(alias1.getAlias()).isEqualTo("alias1");
    AliasData alias2 = aliases.get("alias2");
    assertThat(alias2.getAlias()).isEqualTo("alias2");
    assertThat(templateData.getOrder()).isEqualTo(putTemplateRequest.getOrder());
    assertThat(templateData.getVersion()).isEqualTo(putTemplateRequest.getVersion());
}
Also used : PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) AliasData(org.springframework.data.elasticsearch.core.index.AliasData) TemplateData(org.springframework.data.elasticsearch.core.index.TemplateData) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) GetTemplateRequest(org.springframework.data.elasticsearch.core.index.GetTemplateRequest) Settings(org.springframework.data.elasticsearch.core.index.Settings) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 4 with AliasData

use of org.springframework.data.elasticsearch.core.index.AliasData 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)

Example 5 with AliasData

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

the class ResponseConverter method getIndexInformations.

// endregion
// region index informations
/**
 * get the index informations from a {@link GetIndexResponse}
 *
 * @param getIndexResponse the index response, must not be {@literal null}
 * @return list of {@link IndexInformation}s for the different indices
 */
public static List<IndexInformation> getIndexInformations(GetIndexResponse getIndexResponse) {
    Assert.notNull(getIndexResponse, "getIndexResponse must not be null");
    List<IndexInformation> indexInformationList = new ArrayList<>();
    for (String indexName : getIndexResponse.getIndices()) {
        Settings settings = settingsFromGetIndexResponse(getIndexResponse, indexName);
        Document mappings = mappingsFromGetIndexResponse(getIndexResponse, indexName);
        List<AliasData> aliases = aliasDataFromIndexResponse(getIndexResponse, indexName);
        indexInformationList.add(IndexInformation.of(indexName, settings, mappings, aliases));
    }
    return indexInformationList;
}
Also used : AliasData(org.springframework.data.elasticsearch.core.index.AliasData) ArrayList(java.util.ArrayList) Document(org.springframework.data.elasticsearch.core.document.Document) Settings(org.springframework.data.elasticsearch.core.index.Settings)

Aggregations

AliasData (org.springframework.data.elasticsearch.core.index.AliasData)6 Settings (org.springframework.data.elasticsearch.core.index.Settings)5 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 AliasAction (org.springframework.data.elasticsearch.core.index.AliasAction)3 AliasActions (org.springframework.data.elasticsearch.core.index.AliasActions)3 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)3 Nullable (org.springframework.lang.Nullable)3 LocalDate (java.time.LocalDate)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Assertions (org.assertj.core.api.Assertions)2 JSONException (org.json.JSONException)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 JSONAssert (org.skyscreamer.jsonassert.JSONAssert)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Boolean (java.lang.Boolean)1 Long (java.lang.Long)1 Object (java.lang.Object)1